/* To take two numbers as input then count number of evens present within those two
numbers where display will be done in main() method
Using following method prototype:
int evenCount() */
(user Defined method)
Answers
The given code is written in Java.
import java.util.*;
public class CountEven{
static int evenCount(int a,int b){
int count=0,d;
for(;a!=0;a/=10){
d=a%10;
if(d%2==0)
count++;
}
for(;b!=0;b/=10){
d=b%10;
if(d%2==0)
count++;
}
return count;
}
public static void main(String args[]){
Scanner _=new Scanner(System.in);
System.out.print("Enter first number: ");
int a=_.nextInt();
System.out.print("Enter second number: ");
int b=_.nextInt();
_.close();
System.out.printf("Number of even digits in %d and %d is %d.",a,b,evenCount(a,b));
}
}
- Here, I have assumed that we have to count the number of even digits. To do this, we will initialize count = 0 and divide the number repeatedly and get the last digit. If that digit is even, add 1 to count. At last, return count.
See the attachment for output.
Answer:
Program-
import java.util.*;
public class Main
{
int evenCount1(int a)
{
int r=0,c=0;
while(a!=0)
{
r=a%10;
if(r%2==0)
c++;
a=a/10;
}
return c;
}
int evenCount2(int b)
{
int t=0,e=0;
while(b!=0)
{
t=b%10;
if(t%2==0)
e++;
b=b/10;
}
return e;
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
Main ob=new Main();
int a1,b1;
System.out.println("Enter the numbers");
a1=in.nextInt();
b1=in.nextInt();
System.out.println("The numbers of even digits in "+a1+"="+ob.evenCount1(a1));
System.out.println("The numbers of even digits in "+b1+"="+ob.evenCount2(b1));
}
}
Note:-
- I didn't wrote using 1 function evenCount as you can only return only 1 function.
- In order to calculate even numbers within both the numbers you need to create 2 methods.
Output is attched.