Start Practicing LeetCode with Programming Chaska. I have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new.
Special Number Program in C
Special Number: A natural number which is equal to the sum of the factorial of each digit is called a Special Number.
For Example: Number - - > 145=> 1 4 5
= !1 + !4 + !5
= 1 + 4*3*2*1 + 5*4*3*2*1
= 1 + 24 + 120
= 145 = NumberSo, 145 is a Special Number.Some of the Special numbers are 1,2,145 and 40585.
= !1 + !4 + !5
= 1 + 4*3*2*1 + 5*4*3*2*1
= 1 + 24 + 120
= 145 = Number
Algorithm to check whether a given number is a Special number or not:
Step 1: Accept a number.
Step 2: Find the factorial of each digit of a number and store it in a variable say sum.
Step 3: If the number is equal to the sum then display "Special number" else display "Not Special Number".
Step 4: Stop
The above Algorithm can be implemented in C Programming in several ways. Some of the C Program codes are given below:
Code 1: This is the general code to check for a Special number in C.
Code 2: Recursion approach to check whether a given number is a Special number or not.
Code 3: This code takes a number as a string and then converts each character to an integer, finds factorial and adds it to the sum, and at last converts string to an integer by using atoi function and check if it is equal to the sum or not.
Start Practicing LeetCode with Programming Chaska. I have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new.
Code 4: This is optimized code to check special numbers. Here we are storing factorial of numbers from 0 to 9 so that we don't have to calculate factorial.
Time complexity - O(n)
Start Practicing LeetCode with Programming Chaska. I have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new.
0 Comments