In this tutorial you will learn about the Program to check Strong Number In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Strong Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Strong Number
A strong number is a number which sum of the factorial of the digits is equal to the number itself.
Example:-
145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
So we can say that the 145 is a strong number.
Program to check Strong Number In Java
In our program we will find given number is a strong number or not in java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the age value .Later we calculate the age is valid for vote or not let’s have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// Java progra to find Strong number.. import java.util.Scanner; public class Strong_Num { public static boolean Strong(int number) { // declaring variables int sum = 0, last = 0; int temp = number; // traversing through all the digits while(temp != 0) { last= temp % 10; sum += facto(last); temp /= 10; } // comparing sum and number if(sum == number) return true; return false; } // calculate factorial of given number.. public static long facto(int n) { long fact = 1; for(int i=1; i<=n; i++) { fact *= i; } return fact; } public static void main(String[] args) { // declaring variables... int number = 0; boolean result = false; // Taking values from user... Scanner scan = new Scanner(System.in); System.out.print("Enter any number:: "); number = scan.nextInt(); // checking number is strong number or not result = Strong(number); if(result) System.out.println(number + " is a strong number."); else System.out.println(number + " is not a strong number"); scan.close(); } } |
Output
Strong number.
Not a strong number.
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number
- shrt= it will hold value person short by how much.
After declaring variables in the next statement user will be prompted to enter a value and which will be assigned to variable ‘number’.
And after that we calculate the value given number is strong or not by passing the number to user define function Strong(number) .
within the function here first of all we are going to find factors of given number and also the factorial of each factor of a number.
after that we going add the values and check if given number and sum of factor of given area same as shown in image
If both are same number is strong
If not Not a strong number