Posted on by Kalkicode
Code Number

Strong number program

In mathematics, a strong number is a number whose sum of the factorials of its digits is equal to the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 145. In this article, we will write a C program to determine if a given number is a strong number or not.

Problem Statement

We need to implement a program that takes an integer as input and checks whether it is a strong number or not. The program should calculate the sum of the factorials of the digits of the number and compare it with the original number to decide if it is a strong number.

Example

Let's consider the number 145. The sum of the factorials of its digits will be: 1! + 4! + 5! = 1 + 24 + 120 = 145 Since the sum (145) is equal to the original number, 145 is a strong number.

Pseudocode

// Function to calculate the factorial of a given number
    function factorial(num)
        if num = 0
            return 1
        result = 1
        n = num
        while n ≠ 0
            result = result * n
            n = n - 1
        return result
    
    // Function to check if a number is a Strong Number
    function isStrongNo(num)
        n = num
        sum = 0
        while n ≠ 0
            digit = n % 10   // Get the last digit of the number
            sum = sum + factorial(digit)   // Add the factorial of the digit to the sum
            n = n / 10   // Remove the last digit from the number
        end while
    
        // Check if the sum is equal to the original number
        if sum = num
            print "Is Strong Number"
        else
            print "Is Not Strong Number"
        end if
    
    // Main function
    function main()
        // Test cases
        isStrongNo(12)   // Output: Is Not Strong Number
        isStrongNo(145)  // Output: Is Strong Number
        isStrongNo(1)    // Output: Is Strong Number
    end function
    
    // Call the main function to start the program
    main()
  1. Define a function factorial that takes an integer num and returns the factorial of num.
  2. Inside the factorial function, initialize a variable result to 1.
  3. Run a loop from num down to 1 (inclusive) and in each iteration, multiply result by the current value of num.
  4. Return the result after the loop ends.
  5. Define a function isStrongNo that takes an integer num.
  6. Inside the isStrongNo function, initialize variables n, digit, and sum to store the original number, the last digit, and the sum of factorials, respectively.
  7. Run a loop until n is not equal to 0.
  8. Inside the loop, get the last digit of n and store it in the digit variable.
  9. Add the factorial of digit to the sum variable using the factorial function.
  10. Remove the last digit from n.
  11. After the loop ends, check if sum is equal to the original number.
  12. If sum is equal to num, print that it is a "Strong Number"; otherwise, print that it is "Not a Strong Number."

Algorithm

  1. Define the factorial function as described in the pseudocode.
  2. Define the isStrongNo function as described in the pseudocode.
  3. Inside the main function, call isStrongNo for different test cases, such as 12, 145, and 1.
  4. Print the result for each test case.

Here given code implementation process.

//  C program 
//  Strong number program
#include <stdio.h>

// Return the factorial of given number
int factorial(int num)
{
	if (num == 0)
	{
		// Base case 
		return 1;
	}
	int n = num;
	int result = 1;
	while (n != 0)
	{
		result *= n;
		n--;
	}
	return result;
}
void isStrongNo(int num)
{
	int n = num;
	int digit = 0;
	int sum = 0;
	// Execute loop until when the value of n is not zero
	while (n != 0)
	{
		// get last digit
		digit = n % 10;
		// Sum of digit factorial
		sum += factorial(digit);
		// Remove last digit
		n /= 10;
	}
	// Display given number
	printf("\n Number : %d", num);
	if (sum == num)
	{
		printf("\n Is Strong Number \n");
	}
	else
	{
		printf("\n Is Not Strong Number\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test case
	//  12 (1!+2!)
	//  12 != 3
	isStrongNo(12);
	//  145 (1! + 4! + 5!) (1 +  +)
	isStrongNo(145);
	// 19   (1+9)   => 10 (1+0) => 1
	isStrongNo(1);
	return 0;
}

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
/*
  Java program
  Strong number program
*/
public class StrongNumber
{
	// Return the factorial of given number
	public int factorial(int num)
	{
		if (num == 0)
		{
			// Base case 
			return 1;
		}
		int n = num;
		int result = 1;
		while (n != 0)
		{
			result *= n;
			n--;
		}
		return result;
	}
	public void isStrongNo(int num)
	{
		int n = num;
		int digit = 0;
		int sum = 0;
		// Execute loop until when the value of n is not zero
		while (n != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += factorial(digit);
			// Remove last digit
			n /= 10;
		}
		// Display given number
		System.out.print("\n Number : " + num);
		if (sum == num)
		{
			System.out.print("\n Is Strong Number \n");
		}
		else
		{
			System.out.print("\n Is Not Strong Number\n");
		}
	}
	public static void main(String[] args)
	{
		StrongNumber task = new StrongNumber();
		// Test case
		//  12 (1!+2!)
		//  12 != 3
		task.isStrongNo(12);
		//  145 (1! + 4! + 5!) (1 +  +)
		task.isStrongNo(145);
		// 19   (1+9)   => 10 (1+0) => 1
		task.isStrongNo(1);
	}
}

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Strong number program
*/

class StrongNumber
{
	public:
		// Return the factorial of given number
		int factorial(int num)
		{
			if (num == 0)
			{
				// Base case
				return 1;
			}
			int n = num;
			int result = 1;
			while (n != 0)
			{
				result *= n;
				n--;
			}
			return result;
		}
	void isStrongNo(int num)
	{
		int n = num;
		int digit = 0;
		int sum = 0;
		// Execute loop until when the value of n is not zero
		while (n != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += this->factorial(digit);
			// Remove last digit
			n /= 10;
		}
		// Display given number
		cout << "\n Number : " << num;
		if (sum == num)
		{
			cout << "\n Is Strong Number \n";
		}
		else
		{
			cout << "\n Is Not Strong Number\n";
		}
	}
};
int main()
{
	StrongNumber task = StrongNumber();
	// Test case
	//  12 (1!+2!)
	//  12 != 3
	task.isStrongNo(12);
	//  145 (1! + 4! + 5!) (1 +  +)
	task.isStrongNo(145);
	// 19   (1+9)   => 10 (1+0) => 1
	task.isStrongNo(1);
	return 0;
}

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
// Include namespace system
using System;
/*
  C# program
  Strong number program
*/
public class StrongNumber
{
	// Return the factorial of given number
	public int factorial(int num)
	{
		if (num == 0)
		{
			// Base case
			return 1;
		}
		int n = num;
		int result = 1;
		while (n != 0)
		{
			result *= n;
			n--;
		}
		return result;
	}
	public void isStrongNo(int num)
	{
		int n = num;
		int digit = 0;
		int sum = 0;
		// Execute loop until when the value of n is not zero
		while (n != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += factorial(digit);
			// Remove last digit
			n /= 10;
		}
		// Display given number
		Console.Write("\n Number : " + num);
		if (sum == num)
		{
			Console.Write("\n Is Strong Number \n");
		}
		else
		{
			Console.Write("\n Is Not Strong Number\n");
		}
	}
	public static void Main(String[] args)
	{
		StrongNumber task = new StrongNumber();
		// Test case
		//  12 (1!+2!)
		//  12 != 3
		task.isStrongNo(12);
		//  145 (1! + 4! + 5!) (1 +  +)
		task.isStrongNo(145);
		// 19   (1+9)   => 10 (1+0) => 1
		task.isStrongNo(1);
	}
}

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
<?php
/*
  Php program
  Strong number program
*/
class StrongNumber
{
	// Return the factorial of given number
	public	function factorial($num)
	{
		if ($num == 0)
		{
			// Base case
			return 1;
		}
		$n = $num;
		$result = 1;
		while ($n != 0)
		{
			$result *= $n;
			$n--;
		}
		return $result;
	}
	public	function isStrongNo($num)
	{
		$n = $num;
		$digit = 0;
		$sum = 0;
		// Execute loop until when the value of n is not zero
		while ($n != 0)
		{
			// Get last digit
			$digit = $n % 10;
			// Sum of digit factorial
			$sum += $this->factorial($digit);
			// Remove last digit
			$n = intval($n / 10);
		}
		// Display given number
		echo "\n Number : ". $num;
		if ($sum == $num)
		{
			echo "\n Is Strong Number \n";
		}
		else
		{
			echo "\n Is Not Strong Number\n";
		}
	}
}

function main()
{
	$task = new StrongNumber();
	// Test case
	//  12 (1!+2!)
	//  12 != 3
	$task->isStrongNo(12);
	//  145 (1! + 4! + 5!) (1 +  +)
	$task->isStrongNo(145);
	// 19   (1+9)   => 10 (1+0) => 1
	$task->isStrongNo(1);
}
main();

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
/*
  Node Js program
  Strong number program
*/
class StrongNumber
{
	// Return the factorial of given number
	factorial(num)
	{
		if (num == 0)
		{
			// Base case
			return 1;
		}
		var n = num;
		var result = 1;
		while (n != 0)
		{
			result *= n;
			n--;
		}
		return result;
	}
	isStrongNo(num)
	{
		var n = num;
		var digit = 0;
		var sum = 0;
		// Execute loop until when the value of n is not zero
		while (n != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += this.factorial(digit);
			// Remove last digit
			n = parseInt(n / 10);
		}
		// Display given number
		process.stdout.write("\n Number : " + num);
		if (sum == num)
		{
			process.stdout.write("\n Is Strong Number \n");
		}
		else
		{
			process.stdout.write("\n Is Not Strong Number\n");
		}
	}
}

function main()
{
	var task = new StrongNumber();
	// Test case
	//  12 (1!+2!)
	//  12 != 3
	task.isStrongNo(12);
	//  145 (1! + 4! + 5!) (1 +  +)
	task.isStrongNo(145);
	// 19   (1+9)   => 10 (1+0) => 1
	task.isStrongNo(1);
}
main();

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
#   Python 3 program
#   Strong number program

class StrongNumber :
	#  Return the factorial of given number
	def factorial(self, num) :
		if (num == 0) :
			#  Base case 
			return 1
		
		n = num
		result = 1
		while (n != 0) :
			result *= n
			n -= 1
		
		return result
	
	def isStrongNo(self, num) :
		n = num
		digit = 0
		sum = 0
		#  Execute loop until when the value of n is not zero
		while (n != 0) :
			#  Get last digit
			digit = n % 10
			#  Sum of digit factorial
			sum += self.factorial(digit)
			n = int(n /
				#  Remove last digit
				10)
		
		#  Display given number
		print("\n Number : ", num, end = "")
		if (sum == num) :
			print("\n Is Strong Number ")
		else :
			print("\n Is Not Strong Number")
		
	

def main() :
	task = StrongNumber()
	#  Test case
	#   12 (1!+2!)
	#   12 != 3
	task.isStrongNo(12)
	#   145 (1! + 4! + 5!) (1 +  +)
	task.isStrongNo(145)
	#  19   (1+9)   => 10 (1+0) => 1
	task.isStrongNo(1)

if __name__ == "__main__": main()

Output

 Number :  12
 Is Not Strong Number

 Number :  145
 Is Strong Number

 Number :  1
 Is Strong Number
#   Ruby program
#   Strong number program

class StrongNumber 
	#  Return the factorial of given number
	def factorial(num) 
		if (num == 0) 
			#  Base case 
			return 1
		end

		n = num
		result = 1
		while (n != 0) 
			result *= n
			n -= 1
		end

		return result
	end

	def isStrongNo(num) 
		n = num
		digit = 0
		sum = 0
		#  Execute loop until when the value of n is not zero
		while (n != 0) 
			#  Get last digit
			digit = n % 10
			#  Sum of digit factorial
			sum += self.factorial(digit)
			#  Remove last digit
			n /= 10
		end

		#  Display given number
		print("\n Number : ", num)
		if (sum == num) 
			print("\n Is Strong Number \n")
		else 
			print("\n Is Not Strong Number\n")
		end

	end

end

def main() 
	task = StrongNumber.new()
	#  Test case
	#   12 (1!+2!)
	#   12 != 3
	task.isStrongNo(12)
	#   145 (1! + 4! + 5!) (1 +  +)
	task.isStrongNo(145)
	#  19   (1+9)   => 10 (1+0) => 1
	task.isStrongNo(1)
end

main()

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number 

 Number : 1
 Is Strong Number 
/*
  Scala program
  Strong number program
*/
class StrongNumber
{
	// Return the factorial of given number
	def factorial(num: Int): Int = {
		if (num == 0)
		{
			// Base case
			return 1;
		}
		var n: Int = num;
		var result: Int = 1;
		while (n != 0)
		{
			result *= n;
			n -= 1;
		}
		return result;
	}
	def isStrongNo(num: Int): Unit = {
		var n: Int = num;
		var digit: Int = 0;
		var sum: Int = 0;
		// Execute loop until when the value of n is not zero
		while (n != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += this.factorial(digit);
			// Remove last digit
			n = (n / 10).toInt;
		}
		// Display given number
		print("\n Number : " + num);
		if (sum == num)
		{
			print("\n Is Strong Number \n");
		}
		else
		{
			print("\n Is Not Strong Number\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: StrongNumber = new StrongNumber();
		// Test case
		//  12 (1!+2!)
		//  12 != 3
		task.isStrongNo(12);
		//  145 (1! + 4! + 5!) (1 +  +)
		task.isStrongNo(145);
		// 19   (1+9)   => 10 (1+0) => 1
		task.isStrongNo(1);
	}
}

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number
/*
  Swift 4 program
  Strong number program
*/
class StrongNumber
{
	// Return the factorial of given number
	func factorial(_ num: Int)->Int
	{
		if (num == 0)
		{
			// Base case
			return 1;
		}
		var n: Int = num;
		var result: Int = 1;
		while (n  != 0)
		{
			result *= n;
			n -= 1;
		}
		return result;
	}
	func isStrongNo(_ num: Int)
	{
		var n: Int = num;
		var digit: Int = 0;
		var sum: Int = 0;
		// Execute loop until when the value of n is not zero
		while (n  != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += self.factorial(digit);
			// Remove last digit
			n /= 10;
		}
		// Display given number
		print("\n Number : ", num, terminator: "");
		if (sum == num)
		{
			print("\n Is Strong Number ");
		}
		else
		{
			print("\n Is Not Strong Number");
		}
	}
}
func main()
{
	let task: StrongNumber = StrongNumber();
	// Test case
	//  12 (1!+2!)
	//  12  != 3
	task.isStrongNo(12);
	//  145 (1! + 4! + 5!) (1 +  +)
	task.isStrongNo(145);
	// 19   (1+9)   => 10 (1+0) => 1
	task.isStrongNo(1);
}
main();

Output

 Number :  12
 Is Not Strong Number

 Number :  145
 Is Strong Number

 Number :  1
 Is Strong Number
/*
  Kotlin program
  Strong number program
*/
class StrongNumber
{
	// Return the factorial of given number
	fun factorial(num: Int): Int
	{
		if (num == 0)
		{
			// Base case
			return 1;
		}
		var n: Int = num;
		var result: Int = 1;
		while (n != 0)
		{
			result *= n;
			n -= 1;
		}
		return result;
	}
	fun isStrongNo(num: Int): Unit
	{
		var n: Int = num;
		var digit: Int ;
		var sum: Int = 0;
		// Execute loop until when the value of n is not zero
		while (n != 0)
		{
			// Get last digit
			digit = n % 10;
			// Sum of digit factorial
			sum += this.factorial(digit);
			// Remove last digit
			n /= 10;
		}
		// Display given number
		print("\n Number : " + num);
		if (sum == num)
		{
			print("\n Is Strong Number \n");
		}
		else
		{
			print("\n Is Not Strong Number\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: StrongNumber = StrongNumber();
	// Test case
	//  12 (1!+2!)
	//  12 != 3
	task.isStrongNo(12);
	//  145 (1! + 4! + 5!) (1 +  +)
	task.isStrongNo(145);
	// 19   (1+9)   => 10 (1+0) => 1
	task.isStrongNo(1);
}

Output

 Number : 12
 Is Not Strong Number

 Number : 145
 Is Strong Number

 Number : 1
 Is Strong Number

Resultant Output Explanation

In this above C program executes the isStrongNo function for three test cases: 12, 145, and 1.

  1. For the test case 12, the output is "Is Not Strong Number" because 1! + 2! = 1 + 2 = 3, which is not equal to 12.
  2. For the test case 145, the output is "Is Strong Number" because 1! + 4! + 5! = 1 + 24 + 120 = 145, which is equal to the original number.
  3. For the test case 1, the output is "Is Strong Number" because the sum of the factorials of its digits is 1, which is equal to the original number.

Time Complexity

The time complexity of calculating the factorial of a number n is O(n) since the factorial function runs a loop from n down to 1. The loop inside the isStrongNo function runs for the number of digits in the input number, which is approximately log10(num). Therefore, the overall time complexity of the program is O(d * n), where d is the number of digits in the input number and n is the input number itself. In practice, since the number of digits is usually not very large, the algorithm performs quite efficiently.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment