write a program to input an integer and print all the even digits from it for example input=2567 output=2,6
Answers
The following codes have been written using Python.
n = int(input("Enter an integer: "))
el = list()
for i in str(n):
if int(i)%2 == 0:
el.append(i)
if el == []:
print("There are no even digits in the given integer.")
else:
for i in el:
print(i)
Once you input the integer, you create another list for the even digits [if any]. You then traverse through the integer and test its divisibility by 2. If it's divisible, you append it to the list. By logic, if the list is empty, it will mean that there's no even digit(s) in the given integer. You test the list's nature using an if-else clause.
Answer:
//Java program to print the even digits
import java.util.Scanner;
public class EvePrint{
public static void main (String ar []){
int n=sc.nextInt();
while(n!=0){
if((n%10)%2==0)
System.out.print(n%10+" ");
n/=10;
}
}
}