Input a number and check whether all digits are even or not using while loop in Java
while Loop - Question 12
In this question, we will see how to input a number and check if all digits in the number are even digit or not in Java programming using while loop. To know more about while loop click on the while loop lesson.
Q12) Write a program in Java to input a number and check if all digits in the number are even digit or not using while loop.
Program
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");
}
}
}
Output
Enter any number 6248 All digits are even digit