Strong number program

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


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







© 2021, kalkicode.com, All rights reserved