Write a Java program to add, subtract, multiply, divide and find the modulus of any two double numbers
Answers
The given problem is solved using language - Java.
import java.util.*;
public class Number{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
double a,b;
System.out.print("Enter first number: ");
a=sc.nextDouble();
System.out.print("Enter second number: ");
b=sc.nextDouble();
sc.close();
System.out.println("Sum: "+(a+b));
System.out.println("Difference: "+(a-b));
System.out.println("Product: "+a*b);
System.out.println("Division: "+a/b);
System.out.println("Remainder: "+a%b);
}
}
- Line 1: Imports scanner class for taking input.
- Line 2: Class declaration.
- Line 3: Main() method declaration.
- Line 4: Creating object of scanner class.
- Line 5: Declare two variables a and b.
- Line 6-9: Accept the numbers from the user.
- Line 10: Close of scanner.
- Line 11-15: Display the sum, difference, product, quotient and remainder respectively.
- Line 16: End of main().
- Line 17: End of class.
See attachment for output.
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
double a,b,sum,diff,quo,rem;
System.out.println("Enter the numbers:");
a=in.nextInt();
b=in.nextInt();
sum=a+b;
diff=a-b;
quo=a/b;
rem=a%b;
System.out.println("The sum of the two numbers="+sum);
System.out.println("The difference of the two numbers="+diff);
System.out.println("The quotient="+quo);
System.out.println("The remainder between the two numbers="+rem);
}
}