this is a sample program to compute two numbers and get the sum.
i hope you learn and enjoy....
import java.io.*;
import java.util.Scanner;
public class Computation
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num1,num2,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.println("The sum is:" + sum);
}
}
Showing posts with label Compute the sum. Show all posts
Showing posts with label Compute the sum. Show all posts
Thursday, November 25, 2010
Tuesday, August 31, 2010
Choose operators using if statement
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);
}
}
}
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);
}
}
}
Thursday, June 24, 2010
Flowchart problem #1
Problem # 1: Create a flowchart and algorithm that compute two numbers and get the sum.
i will post the solution of this problem tommorow.
i will post the solution of this problem tommorow.
Labels:
Compute the sum,
create a flowchart,
flowchart
Subscribe to:
Posts (Atom)