Program to check whether number is Spy Number or Not

A Number is a Spy number if the sum of its digits equal to the product of its digits.

Example:

               Given Number=123
               Sum of digits =1+2+3=6
               product of digits=1*2*3=6
                                          
Problem statement:-  Program to check whether the number is Spy Number or Not.

Data requirement:-

   Input Data:- num

   Output Data:- String output

   Additional Data:- sum, rem, mult

Program in C

Here is the source code of the C Program to check whether the number is Spy Number or Not.

Code:

//spy Number Or Not
#include<stdio.h>
int main()
{
    int num,i;
    printf("Enter the number:");
    scanf("%d",&num);
     //Sum of digit
    int sum=0,mult=1,rem;
    while(num!=0)
    {
        rem=num%10;
        sum+=rem;
        mult*=rem;
        num/=10;
    }

   if(sum==mult)
    printf("It is a Spy Number.");
   else
    printf("It is not a Spy Number.");
}

Input/Output:
Enter the number:123
It is a Spy Number.

Program in C++

Here is the source code of the C++ Program to check whether the number is Spy Number or Not.

Code:

#include <iostream>
using namespace std;
int main()
{
    int num,i;
    cout<<"Enter the number:";
    cin>>num;

   int sum=0,mult=1,rem;
    while(num!=0)
    {
        rem=num%10;
        sum+=rem;
        mult*=rem;
        num/=10;
    }

   if(sum==mult)
    cout<<"It is a spy Number.";
   else
    cout<<"It is not a spy Number.";
}

Input/Output:
Enter the number:30
It is not a spy Number.

Program in Java
  
Here is the source code of the Java Program to check whether the number is Spy Number or Not.

Code:

import java.util.Scanner;
public class SpyNumberOrNot {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
     int num;
     System.out.println("Enter a number:");
     num=cs.nextInt();
     int sum=0,mult=1,rem;
     while(num!=0)
     {
         rem=num%10;
         sum+=rem;
         mult*=rem;
         num/=10;
     }

    if(sum==mult)
      System.out.println("It is a Spy Number.");
    else
         System.out.println("It is not a Spy Number.");
  cs.close();
}
}

Input/Output:
Enter a number:
22
It is a Spy Number.

Program in Python
  
Here is the source code of the Python Program to check whether the number is Spy Number or Not.

Code:

num=int(input("Enter a number:"))
sum=0
mult=1
while num!=0:
    rem = num % 10
    sum += rem
    mult *= rem
    num //= 10

if sum==mult:
    print("It is a spy Number.")
else:
   print("It is not a spy Number.")

Input/Output:
Enter a number:65
It is not a spy Number.

More:-

C/C++/Java/Python Practice Question 

Post a Comment

0 Comments