Take 10 numbers in an array and show the numbers having more odd digits in it compared to even digits in the same number
E.G: input values 23 205 5134, 135,788 ……
Output would be 5134, 135
import java.io.*;
class ab
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[]=new int [10];
void show() throws Exception
{
int n;
for(int i=0;i<10;i++)
{
System.out.println("\nEnter Value: ");
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("\nOriginal Value");
for(int i=0;i<10;i++)
{
System.out.print(" "+arr[i]);
}
System.out.println("\nValues with more odd digits");
for(int i=0;i<10;i++)
{
n=arr[i];
if(odd(n))
System.out.print(" "+arr[i]);
}
}
private boolean odd(int n)
{
int oddD=0, evenD=0,x;
while(n>0)
{
x=n%10;
n=n/10;
if(x%2==0)
evenD++;
else
oddD++;
}
if(evenD>=oddD)
return false;
else
return true;
}
public static void main(String args[]) throws Exception
{
ab obj=new ab();
obj.show();
}
}
No comments:
Post a Comment