You're online now.

Hurray! you are online now.

Spy Number | c programming

A spy number is a number whose sum is equal to the product of its digits. For Example: 2141 is a spy number, because the sum of its digits is 2 + 1 + 4 + 1 = 8 and product of its digits is 2 * 1 * 4 * 1 = 8 is equal to sum.

``````// Spy number c program
#include<stdio.h>

int main(){
int n;
int sum = 0, product = 1, last_digit;
printf("Enter the Number : ");
scanf("%d", &n);
int temp = n;

while( n > 0 ){
last_digit = n % 10;
sum = sum + last_digit;
product = product * last_digit;
n = n / 10;
}

if(sum == product){
printf("%d is a Spy Number", temp);
}
else{
printf("%d is not a Spy Number", temp);
}

return 0;

}``````

In this program we have checked the spy number whether the given number is a spy number or not. Now let's talk about how this program is working. In this we first declare a variable whose job is to do the input given by the user, create some more variables which have their own different function like sum variable stores the sum of each digit of the given number and so on. The product variable stores the product. The last_digit variable stores the last digits of the number, after entering the number we created a variable called temp which would store the copy of the given number in this variable whose we can use it later. Then a while loop is started whose termination condition is (n > 0) as long as this condition is true then the statement inside the loop will continue to execute as soon as the condition is false then the loop terminates will go.

Now we talk about the statement written inside the loop, first we will find the last digit inside the loop, as soon as we have calculated the last digit, we will calculate the sum with the help of this statement (sum = sum + last_digit), and product and we (product = product * last_digit) and later we will remove that last digit (n = n / 10).

As soon as the loop is finished then we will compare both sum and product if sum and produce are equal then we will print that given number is spy number and if both are not equal then we will print that given number is not spy number.

Algorithm to check spy number

1. CREATE n, last_digit
2. SET temp, sum = 0, product = 1
3. Take input(n)
4. REPEATE step 5, step 6, step 7, step 8 while ( n > 0)
5. last_digit = n % 10
6. sum = sum + last_digit
7. n = n / 10
[END OF LOOP]
8. if sum == product PRINT “SPY NUMBER” otherwise
9. PRINT “Not SPY Number”
10. EXIT
🖤 0

Oops!