Write a Java program to input an integer from the user. Check and display if the number
input is a two digit number, a three- digit number or a four digit number, else display
“number not in range”.
Input: 200
Output: It is a three-digit number
Answers
Answer:
C implementation:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n,r,even=0,odd=0;
printf("Enter a 3 digit number : ");
scanf("%d",&n);
if(n<100 || n>999)
{
printf("\nInvalid Number !!\nProgram is Terminating ...\n\n");
exit(1);
}
while(n>0)
{
r=n%10;
if(r%2==0)
even++;
else
odd++;
n=n/10;
}
if(even==3)
printf("The number contains all even digits\n\n");
else if(odd==3)
printf("The number contains all odd digits\n\n");
else
printf("The number contains both even and odd digits\n\n");
return 0;
}
Python 2.x Implementation:
import sys
n=input("Enter a 3 digit number : ")
if n<100 or n>999:
sys.exit("\nInvalid Number !!\nProgram is Terminating ...\n\n")
even=0
odd=0
while n>0:
r=n%10
n=n/10
if r%2==0:
even+=1
else:
odd+=1
if even==3:
print "The number contains all even digits"
elif odd==3:
print "The number contains all odd digits"
else:
print "The number contains both even and odd digits"
Java Implementation:
import java.io.* ;
class EODigit{
public static void main(String arg[]){
//Console mycon = System.console();
System.out.print("Enter a 3 Digit number : ");
int n = Integer.parseInt(System.console().readLine());
int even=0,odd=0;
if(n<100 || n>999){
System.out.println("\nInvalid Number !!\nProgram is Terminating ...\n");
System.exit(1);
}
while(n>0){
if((n%10)%2==0)
even++;
else
odd++;
n/=10;
}
if(even==3)
System.out.println("The number contains all even digits\n");
else if(odd==3)
System.out.println("The number contains all odd digits\n");
else
System.out.println("The number contains both even and odd digits\n");
}
}