Java Number Programs (ICSE Classes 9 / 10)
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
122 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
Answered By
44 Likes