Showing posts with label Java_Tutorial. Show all posts
Showing posts with label Java_Tutorial. Show all posts

Java Program to Make a Simple Calculator Using if else

In this program we going to make a calculator with simple if else program to do operations like addition, subtraction, multiplication, division.




Program: 


import java.util.Scanner;
public class Calculat {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Enter Data 1 and Data 2 to calc");

double a= in.nextDouble();
double b=in.nextDouble();
System.out.println("Enter sum=0/sub=1/mul=2/div=3/rem=4 operation to do");
double c = 0;
int  ope= in.nextInt();
if(ope==0) c=a+b;
else if(ope==1) c=a-b;
else if(ope==2) c=a*b;
else if(ope==3) c=a/b;
else if(ope==4) c=a%b;
else System.out.println("invalid symbol so incorrect output");
System.out.println("The " +ope +" of " +a +" " +b +" is " +c);
}
}


In this program we are getting 2 datas from user as well as the operation to do and finally we are calculating result with if else statement.

Java Eclipse Sum of 2 integers from user data

In this program we are getting 2 Integer values from user and adding it then printing that into console.

Program: 

import java.util.Scanner; //Library to use scanning user data

public class DataFromUser { //Default Class
public static void main(String[] args) { //Main Function
Scanner in = new Scanner(System.in); //defining In to scan

System.out.println("Enter Data 1 and Data 2 to Add");

//getting datas from use
     int a = in.nextInt();
     int b = in.nextInt();
   
     //Calculating addition
     int c= a+b;
     //printing result
     System.out.println("The Sum of entered numbers is "+c);
}
}

Output: 

Enter Data 1 and Data 2 to Add
4
8
The Sum of entered numbers is 12

Java Eclipse: How to accept input from User / Print User Input

In this program we gonna get String, Int and Float values from user and displays in console of IDE.


Program:

import java.util.Scanner; //Library to use scanning

public class DataFromUser { //Default Class
public static void main(String[] args) { //Main Function
Scanner in = new Scanner(System.in); //defining In to scan

System.out.println("Enter string, Integer, Float wanted to Print");

//getting datas from user
     String s = in.nextLine();
     int a = in.nextInt();
     float b = in.nextFloat();
   
     //printing received data
     System.out.println("You entered string  : "+s);
     System.out.println("You entered integer : "+a);   
     System.out.println("You entered float : "+b);
}
}

Output: 

Enter string, Integer, Float wanted to Print
Udaya
123
1.502
You entered string : Udaya
You entered integer : 123
You entered float : 1.502




Hello World: How to Create Your First Java Program

Hello Everyone.! In this Article we gonna create our first Java program to print "Hello World" using Eclipse IDE.


Requirements:
 1. Java JDK  
 2.  Programming Environment IDE (Suggested: Eclipse)


Create New Java Project 








Java Program To print Hello World


public class HelloWorld { //Default Class
public static void main(String[] args) {                  //Main Function
System.out.println("Hello World!"); //Print statement to print Hello World
}
}



Output: 

Hello World!