write a program to read two numbers and perform all mathematical operations and print result of those operations
Answers
JAVA CODE
import java.util.*;
class subtract_numbers
{
public static void main(String args[ ])
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter 2 numbers to carry out subtraction");
int a=sc.nextInt();
int b=sc.nextInt();
int c= a-b;
System.out.println("Difference="+c);
int d=a+b;
System.out.println("Sum="+d);
int e=a*b;
System.out.println("Product="+e);
int f=a/b;
System.out.println("Quotient="+f);
}//end of main
}//end of class
-------------------------------------------------------
Hope it helps !
______________________________________________________________
Sample Program in C++:
#include<iostream>
using namespace std;
int main()
{
char result;
float num1,num2;
cout << "Enter an operator\n";
cin >> result;
cout << "Enter two numbers:\n";
cin >> num1 >> num2;
switch(result)
{
case '+':
cout << num1 + num2;
break;
case '-':
cout << num1 - num2;
break;
case '*':
cout << num1 * num2;
break;
case '/':
cout << num1 / num2;
break;
default:
printf("Incorrect");
}
return 0;
}
Note: In some cases, num1 > num2.. You can write it using if else condition.
Output:
Please select an Operator:
+
Enter two numbers:
10 20
30.
Hope it helps!