prg in java to check whether number contains only odd digits or not.
please give answer fast
Answers
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,a,f=0;
System.out.println("Enter the number:");
n=in.nextInt();
while(n!=0)
{
a=n%10;
if(a%2==0)
{
f=1;//if digits at any iteration is found so f is 1 and the loop is terminated
break;
}
n=n/10;
}
if(f==0)
System.out.println("The number contains only odd number of digits");
else
System.out.println("The number does not contain odd number of digits");
}
}
Output is attached:
- 135 contains all odd digits so it prints all odd digits.
- 123 does not contains all odd digits so it prints it doesn't have odd digits.
Answer:
import java.util.*;
class Num{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
int n=sc.nextInt(); //Accepting input
int c=0,C1=0;
while(n!=0){
C1++;
if((n%10)%2!=0)
c++;
n/=10;
}
if(c==C1)
System.out.println("Yes");
else
System.out.println("No");
}
}