Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

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

#include <iostream>

int factorial(int num) {
	int fact = 1;

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

	return fact;
}

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

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

	return sum == num;
}

int main() 
{
	int num = 145; // 1! + 4! + 5! = 1 + 24 + 120 = 145

	if (isSpecial(num)) {
		std::cout << "yes" << std::endl;
	}
	else {
		std::cout << "no" << std::endl;
	}
}




/*
run:
 
yes
 
*/

 



answered Nov 25, 2023 by avibootz
edited Nov 25, 2023 by avibootz

Related questions

1 answer 83 views
1 answer 89 views
1 answer 103 views
1 answer 127 views
1 answer 92 views
1 answer 95 views
...