Check strong numbers in a range | Cpp program to print strong numbers in a given range

Program to check strong numbers in a given range

Code

#include<bits/stdc++.h>
using namespace std;


int main(){


    cout<<"Enter a Range"<<endl;         //Requesting the range
    int m,n;                                               
    cin>>m>>n;                                       

    for(int i=m;i<=n;i++){              //to go through given range
    
int sum=0;                             //to store the sum of factorials
int p=0;                                //to store individual digit
int temp=i;                         //copy number to temp for future reference

while(temp!=0){                            //loop to separate each digit
p=temp%10;                         //last digit is stored in p
temp=temp/10;                         //deleting last digit of original number
int fact=1;                               //to store factorial

for(int j=1; j<=p; j++){                       //loop to calculate factorial
fact=fact*j;
}
sum=sum+fact;                         //adding factorial to sum
}

if(sum==i){                             //if sum matches original number it's a strong number
cout<<i<<" is a Strong Number"<<endl;
}
else{
cout<<i<<
" is not a Strong Number"<<endl;
}

}


return 0;
}


Output

Enter a Range
1 10
1 is a Strong Number
2 is a Strong Number
3 is not a Strong Number
4 is not a Strong Number
5 is not a Strong Number
6 is not a Strong Number
7 is not a Strong Number
8 is not a Strong Number
9 is not a Strong Number
10 is not a Strong Number

Post a Comment

0 Comments