Program to check number whose Sum of its digits is equal to the product of its digits ( Spy Number) | Java, Python C & C++ code to check number whose Sum of its digits is equal to the product of its digits



Program to check number whose Sum of its digits is equal to the product of its digits | Java, Python C & C++ code to check number whose Sum of its digits is equal to the product of its digits



Explanation of the Code: Program to check number whose Sum of its digits is equal to the product of its digits

A Spy Number is a number where the sum of its digits is equal to the product of its digits. For example, the number 1124 is a spy number because:

  • Sum of digits: 1 + 1 + 2 + 4 = 8
  • Product of digits: 1 * 1 * 2 * 4 = 8 Since the sum and the product are equal, 1124 is a spy number.

Program to check number whose Sum of its digits is equal to the product of its digits ( Spy Number) | Java, Python C & C++ code to check number whose Sum of its digits is equal to the product of its digits
Program to check number whose Sum of its digits is equal to the product of its digits ( Spy Number) | Java, Python C & C++ code to check number whose Sum of its digits is equal to the product of its digits



Java Code : Program to check number whose Sum of its digits is equal to the product of its digits


import java.util.*;

public class spyNumber {
public static void isSpyNumber(int n)
{
int num=n;
int digit;
int sum=0;
int product=1;
while(num!=0)
{
digit=num%10;
sum=sum+digit;
product=product*digit;
num=num/10;


}
if(sum==product)
{
System.out.println(n+" is a Spy Number.");
}
else
{
System.out.println(n+" is not a Spy Number.");
}
}
public static void main(String[] args) {
Scanner read=new Scanner(System.in);
System.out.print("Enter a number : ");
int n=read.nextInt();
read.close();
isSpyNumber(n);
}
}



Python Code : Program to check number whose Sum of its digits is equal to the product of its digits def is_spy_number(n):

 num = n
   sum_digits = 0
    product_digits = 1

    while num != 0:
        digit = num % 10
        sum_digits += digit
        product_digits *= digit
        num //= 10

    if sum_digits == product_digits:
        print(f"{n} is a Spy Number.")
    else:
        print(f"{n} is not a Spy Number.")

# Input and output
n = int(input("Enter a number: "))
is_spy_number(n)



C: Program to check number whose Sum of its digits is equal to the product of its digits


#include <stdio.h>

void isSpyNumber(int n) {
    int num = n;
    int sum = 0;
    int product = 1;

    while (num != 0) {
        int digit = num % 10;
        sum += digit;
        product *= digit;
        num = num / 10;
    }

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

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    isSpyNumber(n);
    return 0;
}

C++: Program to check number whose Sum of its digits is equal to the product of its digits

#include <iostream>
using namespace std;

void isSpyNumber(int n) {
    int num = n;
    int sum = 0;
    int product = 1;

    while (num != 0) {
        int digit = num % 10;
        sum += digit;
        product *= digit;
        num = num / 10;
    }

    if (sum == product) {
        cout << n << " is a Spy Number." << endl;
    } else {
        cout << n << " is not a Spy Number." << endl;
    }
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    isSpyNumber(n);
    return 0;
}

Algorithm: Program to check number whose Sum of its digits is equal to the product of its digits 

1.The function isSpyNumber(n) receives the input number n.
2. It initializes two variables, sum for the sum of digits and product for the product of digits.
3. It then uses a while loop to extract each digit from the number n:
  • The last digit is extracted using num % 10.
  • The sum of the digits is calculated using sum += digit.
  • The product of the digits is calculated using product *= digit.
  • The number is reduced by removing the last digit using num = num / 10.
4.  After extracting all the digits, if the sum is equal to the product, the function prints that the number is a Spy Number; otherwise, it prints that it is not a Spy Number.

Time Complexity:



1. The while loop runs as long as there are digits in the number n. The number of digits in n is proportional to log n (since the number of digits is roughly log n in base 10).

2. In each iteration, we perform constant-time operations (calculating the sum and product).

Therefore, the time complexity is:

  Time Complexity: O(d), where d is the number of digits in the number n, or O(log n) since the number of digits is proportional to log n.

Space Complexity:


1. The function only uses a few integer variables (sum, product, num, digit) to store intermediate values and does not use any additional memory-dependent data structures.


2. Therefore, the space complexity is constant.


Space Complexity: O(1).


Summary: Program to check number whose Sum of its digits is equal to the product of its digits

  • Time Complexity: O(log n) where n is the input number.
  • Space Complexity: O(1) (constant space usage).

Thank You for reading this post. You can check out more articles like this on www.physicswallah.in 

If you have any doubts then you can post a comment below.

No comments:

Post a Comment