Wednesday, January 21, 2015

Finding out remainder after getting data from keyboard

The java program given below is a program that will find out the remainder of the mathematical operation division. When we divide a number by another then we get it. The fifteenth line remainder = num1%num2; is being used here to calculate the remainder.

After executing the program, you will be asked to provide the largest number first that will be stored into the variable num1. Soon you will be asked to provide the smallest number that will be stored into the variable num2.

Later the mathematical operation will be performed in the fifteenth line and the remainder will be stored into the variable remainder. Finally the result will be displayed by the line System.out.println("The remainder is: "+remainder);
import java.util.Scanner;
public class RemainderIn{
 public static void main(String [] args){
 //declaring three integer type variables
 int num1, num2, remainder;
 //getting input from keyboard
 Scanner in = new Scanner(System.in);
 System.out.print("Enter the largest number: ");
 num1 = in.nextInt();
 System.out.print("Enter the smallest number: ");
 num2 = in.nextInt();
 //calculating the remainder
 remainder = num1%num2;
 //displaying the result
 System.out.println("The remainder is: "+remainder);
 }
}


No comments:

Post a Comment