Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

C++ Program to find the Strong Number

Write a C++ Program to find the Strong Number with an example. Any number can be strong if the sum of the factorial of individual digits in that number equal to the actual number. For instance, factorial of the individual digits in 145  = 1! + 4! + 5! = 1 + 24 + 120 = 145. So, 145 is a perfect number.

In this C++ strong number example, we used reminder = temp % 10 to get the last digit of a number. Next, we are finding the factorial of that number using for loop. Finally, we used the If condition to check whether the number equal to the sum of factorials and print the result.

#include<iostream>
using namespace std;

int main()
{
	int i, number, temp, reminder;
	long sum = 0, factorial = 1;
	
	cout << "\nPlease Enter the Number to Check for Strong Number =  ";
	cin >> number;
	
	for(temp = number; temp > 0; temp =  temp / 10 )
	{
		factorial = 1;  
		reminder = temp % 10;
		
		for (i = 1; i <= reminder; i++)
		{
			factorial = factorial * i;
		}
		cout << "\nThe Factorial of "<< reminder << " = " << factorial;
		sum = sum + factorial;
	}	
	cout << "\n\nThe Sum of the Factorials of " << number << " is = " << sum << "\n\n";
			
	if(number == sum)
	{
		cout << number << " is a Strong Number";
	}
	else
	{
		cout << number << " is Not a Strong Number";
	}

 	return 0;
}
C++ Program to find the Strong Number 1

C++ Program to find the Strong Number using a While loop

#include<iostream>
using namespace std;

int main()
{
	int i, number, temp, reminder;
	long sum = 0, factorial = 1;
	
	cout << "\nPlease Enter the Number to Check for Strong Number =  ";
	cin >> number;
	
	temp = number;
	
	while(temp > 0)
	{
		factorial = 1;
		i = 1;  
		reminder = temp % 10;
		
		while (i <= reminder)
		{
			factorial = factorial * i;
			i++;
		}
		cout << "\nThe Factorial of "<< reminder << " = " << factorial;
		sum = sum + factorial;
		temp =  temp / 10;
	}	
	cout << "\nThe Sum of the Factorials of " << number << " is = " << sum << "\n\n";
			
	if(number == sum)
	{
		cout << number << " is a Strong Number";
	}
	else
	{
		cout << number << " is Not a Strong Number";
	}

 	return 0;
}
C++ Program to find the Strong Number 2

C++ Program to find the Strong Number using Recursive Functions

In this C++ example, the long factorialOfNumber(int number) method finds the factorial of a number.

#include<iostream>
using namespace std;

long factorialOfNumber(int number)
{
	if (number == 0 || number == 1)  
    	return 1;
	else
		return number * factorialOfNumber(number - 1);
}

int main()
{
	int number, temp, reminder;
	long sum = 0, factorial = 1;
	
	cout << "\nPlease Enter the Number to Check for Strong Number =  ";
	cin >> number;
	
	for(temp = number; temp > 0; temp =  temp / 10 )
	{
		factorial = 1;  
		reminder = temp % 10;
		
		factorial = factorialOfNumber(reminder);
		cout << "\nThe Factorial of "<< reminder << " = " << factorial;
		sum = sum + factorial;
	}	
	cout << "\n\nThe Sum of the Factorials of " << number << " is = " << sum << "\n\n";
			
	if(number == sum)
	{
		cout << number << " is a Strong Number";
	}
	else
	{
		cout << number << " is Not a Strong Number";
	}

 	return 0;
}
C++ Program to find the Strong Number 3

C++ Program to Print Strong Numbers between 0 and N

This C++ strong number example allows the user to enter the minimum and maximum values. Next, it prints the strong numbers between min and max value.

#include<iostream>
using namespace std;

long factorialOfNumber(int number)
{
	if (number == 0 || number == 1)  
    	return 1;
	else
		return number * factorialOfNumber(number - 1);
}

long strongNumber(int number)
{
	int temp, reminder;
	long sum = 0, factorial;
	temp = number;
	for(temp = number; temp > 0; temp =  temp / 10 )
	{
		factorial = 1;  
		reminder = temp % 10;
		
		factorial = factorialOfNumber(reminder);
		sum = sum + factorial;
	}
	return sum;
}

int main()
{
	int i, number, temp, minimum, maximum 
	long sum = 0;
	
	cout << "\nPlease Enter Minimum and Maximum for Strong Numbers =  ";
	cin >> minimum >> maximum;
	
	for(number = minimum; number <= maximum; number++ )
	{
		temp = number;
		sum = strongNumber(temp);
		
		if(number == sum)
		{
			cout << number << " is a Strong Number" << endl;
		}
	}	

 	return 0;
}
C++ Program to find the Strong Number 4

Filed Under: CPP Examples

  • C++ Print Hello World
  • C++ Add Two Numbers
  • C++ Count Notes in Amount
  • C++ Find Last Digit of a Number
  • C++ Largest of Two Nums
  • C++ Largest of Three Nums
  • C++ LCM of Two Numbers
  • C++ Leap Year
  • C++ Number Divisible by 5 & 11
  • C++ Palindrome Number
  • C++ Perfect Number
  • C++ Prime Factors of a Number
  • C++ Print 1 to 100
  • C++ Print Alphabets from A and Z
  • C++ Print Alphabets from a to z
  • C++ Print Even Numbers
  • C++ Print Multiplication Table
  • C++ Print N Natural Numbers
  • C++ Print Odd Numbers
  • C++ Product of Digits in a Num
  • C++ Profit or Loss Program
  • C++ Reverse a Number
  • C++ Simple Interest
  • C++ Square of a Number
  • C++ Square Root of a Number
  • C++ Standard Deviation
  • C++ Strong Number
  • C++ Student Marks Program
  • C++ Student Grade Program
  • C++ Sum of Digits in Number
  • C++ Sum of Even Numbers
  • C++ Sum of Even & Odd
  • C++ Sum of Odd Numbers
  • C++ Swap Two Numbers
  • C++ Natural Nums in Reverse
  • C++ Sum of Natural Numbers
  • C++ Sum of number 1st, last digit
  • C++ Swap of 1st & last digit
  • C++ Sum of Series 1²+2²+3²+n²
  • C++ Sum of Series 1³+2³+3³+n³
  • C++ ASCII Values of all Chars
  • C++ Sum of Char array ASCII val
  • C++ Sum of String ASCII values
  • C++ String Length
  • C++ Convert String to Lowercase
  • C++ Convert String to Uppercase
  • C++ Toggle String Char Cases
  • C++ Convert CM to Meters & KM
  • C++ Convert KM to M, CM & MM
  • C++ Convert days to year & Week
  • C++ Add Two Arrays
  • C++ Add Two Matrixes
  • C++ Array Arithmetic Operations
  • C++ Check 2 Matrixes are Equal
  • C++ Determinant of a Matrix
  • C++ Identity Matrix
  • C++ Matrix Arithmetic Operation
  • C++ Multiply Two Arrays
  • C++ Subtract two Matrixes
  • C++ Matrix Diagonal Interchange
  • C++ Matrix Lower Triangle
  • C++ Matrix Scalar Multiplication
  • C++ Matrix Transpose
  • C++ Matrix row & column sum
  • C++ Matrix Opp Diagonal sum
  • C++ Multiply two Matrixes
  • C++ Matrix Upper Triangle
  • C++ Sparse Matrix
  • C+ Symmetric Matrix
  • C++ Sum of Matrix Diagonal
  • C++ Inverted Star Pyramid
  • C++ Square Star Pattern
  • C++ Triangle is Valid using Sides
  • C++ Perimeter of a Rectangle
  • C++ Max Occur String Character
  • C++ Print Pascal Triangle
  • C++ Rectangle Star Pattern
  • C++ Square Number Pattern

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy