Java program to find multiples of 100 & upto N numbers – For any problem, the first step is to understand the problem thoroughly and see if any constraints are mentioned. Then, we need to decide on the inputs required by us to arrive at the solution along with the expected output of our problem. Lastly, we decide the logic and set of instructions used to get the expected output.
In here, our problem statement is to find whether the given number is a multiples of 100 or not. We call a number a multiple of other when, it is divisible by the other number and leaves a remainder zero. Or the product of the other number with something else should give us this number.
To solve this, we will need an input integer number (n) for which we need to check. Our expected output would be either true or false.
To read this required input at runtime, Scanner class can be used. This class is well known in Java for reading any primitive datatype input at runtime. To make use of this class, we must first create an object instantiating it. Then, using this object, we can make the method call of the required method based on our input datatype. Since our required input is an integer, we make a method call for nextInt().
1 2 3 4 5 6 7 |
int n; Scanner sc=new Scanner(System.in); System.out.println("Enter any number:"); n=sc.nextInt(); |
Since we have read our input, we now decide the logic to be used. For making this logic reusable, we place it in a static method (checkMultiple).
By making method call for this, we take the execution to this method. In this method, we pass our input integer as argument and it returns a boolean value true or false which will be stored in another variable (status) in main method.
boolean status=checkMultiple(n);
In this method (checkMultiple), we check whether the argument passed is divisible by 100 or not. For this, we make use of a modulus operator to find the remainder. If the remainder when the argument (n) is divided by 100 is zero, it means that the number is a multiple of 100. So, we return true. Else, it means the number is not divisible so, we return false.
if(n%100==0)
return true;
return false;
This returned value can then be displayed on the console screen by making use of a System.out method named println() as follows:
System.out.println(“Is entered number is multiple of 100 :”+status);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class Multiple { public static void main(String arg[]) { int n; Scanner sc=new Scanner(System.in); System.out.println("Enter any number:"); n=sc.nextInt(); boolean status=checkMultiple(n); System.out.println("Is entered number is multiple of 100 :"+status); } static boolean checkMultiple(int n) { if(n%100==0) return true; return false; } } |
Output – 1:
Our input is n=1.
n%100 = 1%100 =1
Since this is not equal to zero, it is not a multiple. So, we return “false”.
1 2 3 |
Enter any number: 1 Is entered number is multiple of 100 :false |
Output – 2:
Here, n=100
n%100 = 100%100 = 0
Since the remainder is zero, it is a multiple of 100. Therefore, we return “true”.
1 2 3 |
Enter any number: 100 Is entered number is multiple of 100 :true |