Computer Applications

Define a class to accept a number and check whether the number is valid number or not. A valid number is a number in which the eventual sum of digits of the number is equal to 1.

e.g., 352 = 3 + 5 + 2 = 10 = 1 + 0 = 1

Then 352 is a valid number.

Java

Java Iterative Stmts

1 Like

Answer

import java.util.Scanner;

public class KboatValidNum
{

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter number : ");
        int num = in.nextInt();
        int n = num;

        while (n > 9) {
            int sum = 0;
            while (n != 0) {
                int d = n % 10;
                n /= 10;
                sum += d;
            }
            n = sum;
        }

        if (n == 1)
            System.out.println(num + " is Valid Number");
        else
            System.out.println(num + " is not Valid Number");

    }
}

Output

Answered By

1 Like


Related Questions