(Arithmetic) Write a program that asks the user to enter two numbers, obtains the two
numbers from the user and prints the sum, product, difference, and quotient of the two numbers.
Answers
Explanation:
#include <iostream>
using namespace std;
int main()
{
int number1;
int number2;
int sum;
int product;
int difference;
float quotient;
cout << "Enter two integers: ";
cin >> number1 >> number2;
sum=number1+number2;
cout<<"Sum is: "<<sum<<endl;
product=number1*number2;
cout<<"Product is: "<<product<<endl;
difference=number1-number2;
cout<<"Dofference is: "<<difference<<endl;
if(number2!=0){
quotient=(float)number1/number2;
cout<<"Quotient is: "<<quotient<<endl;
}
}
1. Using function argument :-
public class Numbers
{
public static void main (int a, int b)
{
System. out.println("Value of a is:" + a);
System. out.println ("Value of b is:" + b);
int sum = a + b;
System. out.println(" Sum of two numbers is:"+sum);
int product = a * b;
System. out.println( "Product of two numbers is:"+product);
int difference = a - b;
System. out.println( "Difference of two numbers is:" + difference);
double quotient = a/b;
System. out.println( "Quotient of two numbers is:" + quotient);
}
}
input
a = 5
b = 2
Output
Value of a is: 5
Value of b is: 2
Sum of two numbers is: 7
Product of two numbers is: 10
Difference of two numbers is: 3
Quotient of two numbers is: 2.5
2. Using scanner class :-
import java.util.*;
public class Numbers
{
public static void main (Sting[]args)
{
Scanner sc = new Scanner (System.in);
System. out.println("Enter value of a:" + a);
int a = sc.nextInt();
System. out.println ("Enter value of b:" + b);
int b = sc.nextInt();
int sum = a + b;
System. out.println(" Sum of two numbers is:"+sum);
int product = a * b;
System. out.println( "Product of two numbers is:"+product);
int difference = a - b;
System. out.println( "Difference of two numbers is:" + difference);
double quotient = a/b;
System. out.println( "Quotient of two numbers is:" + quotient);
}
}
input
Enter value of a :
5
Enter value of b :
2
Output
Enter value of a :
5
Enter value of b :
2
Sum of two numbers is: 7
Product of two numbers is: 10
Difference of two numbers is: 3
Quotient of two numbers is: 2.5
3. Using InputStreamReader Class :-
import java.io.*;
public class Numbers
{
public static void main (Sting[]args)throws IOException
{
InputStreamReader read = new InputStreamReader (System.in);
BufferReader in = new BufferReader(read);
System. out.println("Enter value of a:" + a);
int a = Integer.praseInt(in.readLine());
System. out.println ("Enter value of b:" + b);
int b = Integer.praseInt(in.readLine());
int sum = a + b;
System. out.println(" Sum of two numbers is:"+sum);
int product = a * b;
System. out.println( "Product of two numbers is:"+product);
int difference = a - b;
System. out.println( "Difference of two numbers is:" + difference);
double quotient = a/b;
System. out.println( "Quotient of two numbers is:" + quotient);
}
}
input
Enter value of a :
5
Enter value of b :
2
Output
Enter value of a :
5
Enter value of b :
2
Sum of two numbers is: 7
Product of two numbers is: 10
Difference of two numbers is: 3
Quotient of two numbers is: 2.5
Note:
[can use any one method out of these three]