KnowledgeBoat Logo

Java Number Programs (ISC Classes 11 / 12)

Write a program to input a number and count the number of digits. The program further checks whether the number contains odd number of digits or even number of digits.

Sample Input: 749
Sample Output: Number of digits=3
The number contains odd number of digits.

Java

Java Iterative Stmts

ICSE

95 Likes

Answer

import java.util.Scanner;

public class KboatDigitCount
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number: ");
        int n = in.nextInt();
        int dc = 0;
        
        while (n != 0) {
            dc++;
            n /= 10;
        }
        
        System.out.println("Number of digits = " + dc);
        
        if (dc % 2 == 0)
            System.out.println("The number contains even number of digits");
        else
            System.out.println("The number contains odd number of digits");
    }
}

Output

BlueJ output of Write a program to input a number and count the number of digits. The program further checks whether the number contains odd number of digits or even number of digits. Sample Input: 749 Sample Output: Number of digits=3 The number contains odd number of digits.

Answered By

38 Likes