KnowledgeBoat Logo

Computer Applications

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

125 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");
    }
}

Variable Description Table

Program Explanation

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

45 Likes


Related Questions