Spy Number in C
In this article, we will discuss the spy numbers in C with its example.
A spy number is where the sum of all the digits in the number equals the product of all the digits.
Let's see an example to understand it better. Consider the number 112. If we add up its digits (1 + 1 + 2), we get 4. If we multiply those digits together (1 * 1 * 2), we still get 4! Isn't that fascinating?
Another example is the number 123. If we add up its digits (1 + 2 + 3), we get 6. When we multiply those digits (1 * 2 * 3), we again get 6. So, any number with this special property having the same sum and product of its digits is called a spy number. It's like a little number detective with a secret code.
Example 1:
Let's take an example to check whether the given number is a spy number or not.
Code:
#include<stdio.h>
int main(){
int number;
printf("Enter the number you want to check \n");
scanf(" %d", &number);
int original = number;
// initialize sum to zero to calculate the digit sum
//initialize product to 1 to calculate the digit sum
int sum=0,product=1;
while(number>0){
// Taking the reminder
int remainder = number % 10;
sum = sum + remainder;
product = product * remainder;
number = number/10;
}
if(sum == product || original ==0){
printf("%d is a spy number \n",original);
}else{
printf("%d is a not spy number \n",original);
}
}
Output:
![Spy Number in C](https://static.tutorialandexample.com/c/spy-number-in-c1.png)
Explanation:
In this example, the code takes an integer input from the user and stores it in the number variable. After that, it initializes two variables, sum, and product, to 0 and 1, respectively.
Using a loop, it calculates the sum and product of the digits of the input number and updates the sum and product accordingly. After the loop, it checks if the sum and product are equal or the original number is zero. If either condition is true, the number is declared a spy number.
If the number satisfies the spy number property, it prints a message saying it is a spy number. Otherwise, it prints a message saying it is not a spy number.
The code checks if a number is a spy number based on the sum-product property of its digits. A spy number has the unique property that the sum of its digits is equal to the product of its digits.
Example 2:
Let's take a program to get all the spy numbers that are present in the given range.
Code:
#include<stdio.h>
int find_spy_number(int number){
int original = number;
int sum=0,product=1;
while(number>0){
int remainder = number % 10;
sum = sum + remainder;
product = product * remainder;
number = number/10;
}
if(sum == product || original ==0){
return original;
}else{
return 0;
}
}
int main(){
int lower_boundary=0;
int upper_boundary=0;
printf("Enter the lower boundary ");
scanf("%d", &lower_boundary);
printf("Enter the upper boundary ");
scanf("%d",&upper_boundary);
printf("The spy numbers in between the lower boundary and the upper boundary \n");
for(int i=lower_boundary;i<=upper_boundary;i++){
int temp = find_spy_number(i);
if(temp!=0){
printf("%d ",temp);
}
}
return 0;
}
Output:
![Spy Number in C](https://static.tutorialandexample.com/c/spy-number-in-c2.png)
Explanation:
In this example, we take two functions:
1. find_spy_number function:
This function takes an integer number as input. It creates two variables, original and sum, and sets sum to 0 and product to 1. After that, the function enters a loop to calculate the sum and product of the digits of the input number.
Inside the loop, it extracts the rightmost digit using the number % 10 and updates the sum and product variables accordingly.
After the loop, it checks if the sum equals the product or the original number (original) is zero. If either condition is true, the original number is returned as a spy number; otherwise, it returns 0.
2. Main() function:
The main function begins by declaring two integer variables, lower_boundary and upper_boundary, and initializes them to 0. After that, it asks the user to enter the lower_boundary and upper_boundary values using printf and scanf.
Next, it prints a message indicating that it will display the spy numbers between the lower_boundary and upper_boundary. Using a for loop, it iterates through all numbers from lower_boundary to upper_boundary. It calls the find_spy_number function for each number and stores the result in the temp variable.
If the temp is not equal to 0 (meaning the number is a spy number), it prints the temp value (spy number) using printf. Finally, the main() function returns 0, indicating successful execution.
In summary, the code takes a range of numbers as input (lower_boundary and upper_boundary) and then iterates through all the numbers within that range. It checks if each number is a spy number using the find_spy_number function. If it finds a spy number, it prints output to the console. The find_spy_number function checks if a number is a spy number by calculating the sum and product of its digits and comparing them. If they are equal or the original number is zero, it considers the number a spy number.