Write a program in Java to input a number. Display all the digits of the number
by stating whether it is an even or an odd digit by using do while loop (Use
Scanner class).
Sample input: 891
Sample output:
1: an odd digit
9: an odd digit
8: an even digit
Answers & Comments
Answer:
Explanation:
import java.util.Scanner;
public class DigitEvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int digit;
System.out.println("Digits of the number:");
do {
digit = number % 10;
number = number / 10;
if (digit % 2 == 0) {
System.out.println(digit + ": an even digit");
} else {
System.out.println(digit + ": an odd digit");
}
} while (number != 0);
scanner.close();
}
}
I am doing with While loop
import java.util.Scanner;
public class Q12
{
public static void main(String args[])
{
int n,d,td=0,ed=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any number ");
n=sc.nextInt();
while(n>0)
{
d=n%10;
if(d%2==0)
{
ed=ed+1; // counting the even digits
}
td=td+1; // counting the total number of digits
n=n/10;
}
if(td==ed)
{
System.out.println("All digits are even digit");
}
else
{
System.out.println("All digits are not even digit");
}
}
}