Description

Today we are going to write a program which is to check if given number is spy or not and yes the most important thing is what is spy number, you will get the answer of all these on this blog.

Before writing a program , we should know what a spy number is, only then we will get the a program , if we do not know that we have asked to go, then how will we travel exactly like that, we should know what is spy number.

What is spy Number?

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 the product of its digits is 2 * 1 * 4 * 1 = 8 is equal to sum.

Not let's coding...

#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;

}

 Output : 

Enter the number : 2141
2141 is a Spy Number.

How it's work?

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 digit of the number. After declaring the variable, we printed a message that tells the user, enter a 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 statements 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 = produce * 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 spy no number

This is how the program is working

Algorithem to check spy number 

Step 1 : CREATE n, last_digit

Step 2 : SET temp, sum = 0 , product = 1

Step 3 : Take input ( n )

Step 4 : REPEATE Step 5, Step 6 , Step 7 , Step 8 while ( n > 0 )

Step 5 : last_digit = n % 10;

Step 6 : sum = sum + last_digit;

Step 7 : product = product * last_digit;

Step 8 : n = n / 10;

[ End of Loop ]

Step 9 : if sum == product PRINT "SPY NUMBER" OTHER WISE CONDITION false GO TO STEP 10

Step 10 : PRINT "NOT SPY NUMBER"

Step 11 : Exit

Comments

Login is mandatory to comment Please login

Recommended Posts

Display Name of the month by accepting digit of the month | c programming

We will print the month name from the month number given by the user which will give us the user input. For Example : User Entered 5 then print

Happy Numbers | with Example And Programs

Replace the number by the sum of the squares of its digits, and repeat the process. At the end, if the number is equals to 1 then it is a Happy Number, or it loops endlessly in a cycle that does not include 1 (if it is not a happy number then this process will end at 4).

Sunny Number in programming

A number is a sunny number. If 1 is added to that number and the square root of it becomes a whole number.

Program to check whether number is Disarium Number

A Number is a disarium number if the sum of the digits powered with their respective positions is equal to the number itself.

Floyd Triangle | Programs | Algorithm

A Floyd Triangle is right angled triangle consisting of consecutive natural numbers starting with one and then going down through the rows.

Write a program to reverse a number

Write a program to reverse a number. What is reverse number?