Write a program to check whether a number is a Strong Number or not


  • The first line of code imports the Scanner class from the java.util package. This class is used to read input from the user.
  • The program declares a public class named "StrongNumber" which contains the main method.
  • Inside the main method, a new Scanner object named input is created to read input from the user.
  • The program prompts the user to enter a number by printing the string "Enter Strong Number : " using the System.out.print method.
  • The input from the user is read as an integer using the nextInt method of the Scanner class and stored in the variable n.
  • Several integer variables, n1, s1, fact, i, and j, are declared and initialized to 0 or 1.
  • The variable n1 is assigned the value of n so that the original number can be printed later.
  • A for loop is used to calculate the sum of the factorials of the digits in the number. The loop starts from j=n and continues until j is greater than 0. The loop variable j is divided by 10 in each iteration to remove the last digit of the number.
  • Inside the for loop, the factorial of the last digit of the number is calculated using a nested for loop.
  • The variable s1 is calculated as the sum of the factorials of each digit in the number.
  • After the for loop is complete, the program checks if s1 is equal to n1. If they are equal, the program prints "Strong Number :" followed by the value of n1. Otherwise, the program prints "Not Strong Number :" followed by the value of n1.

Source Code

import java.util.Scanner;
class StrongNumber
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter Strong Number : ");
		int n = input.nextInt();
		int i, n1, s1=0,j;  
		int fact; 
		n1 = n; 
		for(j=n;j>0;j=j/10) 
		{  	  
			fact = 1;  
			for(i=1; i<=j % 10; i++)  
			{  
				fact = fact * i;  
			}  
			 s1 = s1 + fact;  	 
		}
		if(s1==n1)  
		{  
			System.out.print("Strong Number :"+ n1);  
		}
		else  
		{  
			System.out.print("Not Strong Number :"+n1);  
		}
	}
}

Output

Enter Strong Number : 145
Strong Number :145

Example Programs