How to check where a number is special number in C#

1 Answer

0 votes
// Special number = sum of the factorial of digits is equal to the number

using System;

public class Program
{
	internal static int factorial(int num) {
		int fact = 1;

		while (num != 0) {
			fact = fact * num;
			num--;
		}

		return fact;
	}

	internal static bool isSpecial(int num) {
		int sum = 0, tmp = num;

		while (tmp != 0) {
			sum += factorial(tmp % 10);
			tmp = tmp / 10;
		}

		return sum == num;
	}
	
	public static void Main(string[] args)
	{
		int num = 145; // 1! + 4! + 5! = 1 + 24 + 120 = 145

		if (isSpecial(num)) {
			Console.WriteLine("yes");
		}
		else {
			Console.WriteLine("no");
		}
	}
}




/*
run:
 
yes
 
*/

 



answered Nov 26, 2023 by avibootz
edited Nov 26, 2023 by avibootz

Related questions

1 answer 107 views
1 answer 122 views
1 answer 140 views
1 answer 96 views
1 answer 106 views
1 answer 119 views
1 answer 155 views
...