A strong number is a number whose sum of the factorial of its digits is equal to that number using while and for loop. a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line.
Example : 1
234
= 2! + 3! + 4!
= 2 + 6 + 24
= 32
So , 234 is not a strong number.
Example : 2
145
= 1! + 4! + 5!
= 1 + 24 + 120
= 145
So , 145 is not a strong number.
import java.util.Scanner; public class strong { //Write a program to check the given number is Strong number or not. public static void main(String args[]) { int num,originalNum,rem,fact,i,sum=0; Scanner in = new Scanner(System.in); System.out.println("Enter a number : "); num=in.nextInt(); originalNum=num; while (num>0)//145>0 14>0 1>0 { rem=num%10; //System.out.println("Reminder : "+rem); fact=1; for(i=1;i<=rem;i++){ fact*=i;//fact=fact*i } //System.out.println("fact : "+fact); sum+=fact; num=num/10; } if (sum == originalNum) { System.out.println(originalNum + " is STRONG NUMBER"); } else { System.out.println(originalNum + " is not a STRONG NUMBER"); } } }
Enter a number : 145 145 is STRONG NUMBER
Enter a number : 234 234 is not a STRONG NUMBERTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions