Wednesday, 17 July 2024

SPY Number ICSE Computer Science

A number is called SPY number if the sum of the digits equals the number of the digits.

Example:

1021 is a SPY number

1+0+2+1 = 4

Number of digits = 4

Let's start the code of determining if a number is SPY or not.

Below is the code for determing if a number is SPY.

import java.util.Scanner;
class Spy
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        int num = sc.nextInt();
        int sum = 0,temp = num, count = 0;
        while(temp>0)
        {
            int digit=temp%10;
            sum = sum+digit;
            count++;
            temp/=10;
        }
        if(sum == count)
        {
            System.out.println(num+" is a Spy number");
        }
        else
        {
            System.out.println(num+" is not a Spy number");
        }
    }
}


Now, write this program and test it with various numbers and increase your knowledge.

Happy Coding.......


3 comments:

Note: only a member of this blog may post a comment.