hello programmers... this program will show to you how to use if statement. It is for computing two input numbers by the user. this is for asking the user to choose the operator they want to execute. 1- addition, 2- subtraction, 3- Multiplication, 4- Quotient. this is a very simple program using nested if statement.
here is a sample program:
import java.io.*;
import java.util.Scanner;
public class chooseoperators
{
public static void main(String args[])
{
Scanner input==new Scanner(System.in);
int add=1;
int sub=2;
int mult=3;
int div=4;
int num1;
int num2;
int oper;
int sum=0,diff=0,prod=0,quo=0;
System.out.println("1- Addition");
System.out.println("2- Subtraction");
System.out.println("3- Multiplication");
System.println("4- Division");
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
System.out.print("Chooose Operators:");
oper=input.nextInt();
if(oper==add)
{
sum=num1+num2;
System.out.println("The sum is:" + sum);
}
if(oper==sub)
{
diff=num1-num2;
System.out.println("The difference is:" +diff);
}
if(oper==mult)
{
prod=num1*num2;
System.out.println("The Product is:" + prod);
}
if(oper==div)
{
quo=num1/num2;
System.out.println("The quotient is:" + quo);
}
}
}
Showing posts with label getting the sum. Show all posts
Showing posts with label getting the sum. Show all posts
Tuesday, August 31, 2010
Sunday, April 18, 2010
Compute two numbers in java using user input
this program will ask the user to input two numbers and get the sum.
import java.io.*;
import java.util.Scanner;
public class getsum
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num1,num2;
int sum=0;
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
sum=num1+num2;
System.out.print("The sum is:" + sum);
}
}
import java.io.*;
import java.util.Scanner;
public class getsum
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num1,num2;
int sum=0;
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
sum=num1+num2;
System.out.print("The sum is:" + sum);
}
}
Subscribe to:
Posts (Atom)