Write a program in java to accept a number from user and check whether the number us a boring number or not.
Boring number is a positive number having all the digits at even position in the number as even and the digits at odd position odd
import java.util.*;
class BoringNumber
{
int n,rev,f;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter The Number:");
n=sc.nextInt();
display();
}
private void display()
{
rev=0;
f=1;
System.out.print("\nEntered Number is:"+n);
while(n>0)
{
rev=rev*10+n%10;
n=n/10;
}
while(rev>0)
{
int d=rev%10;
if(f%2!=d%2)
break;
rev=rev/10;
f++;
}
if(rev>0)
System.out.print("\nEntered Number is not a boring number");
else
System.out.print("\nEntered Number is a boring number");
}
public static void main(String args[])
{
BoringNumber ob=new BoringNumber();
ob.take();
}
}
class BoringNumber
{
int n,rev,f;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter The Number:");
n=sc.nextInt();
display();
}
private void display()
{
rev=0;
f=1;
System.out.print("\nEntered Number is:"+n);
while(n>0)
{
rev=rev*10+n%10;
n=n/10;
}
while(rev>0)
{
int d=rev%10;
if(f%2!=d%2)
break;
rev=rev/10;
f++;
}
if(rev>0)
System.out.print("\nEntered Number is not a boring number");
else
System.out.print("\nEntered Number is a boring number");
}
public static void main(String args[])
{
BoringNumber ob=new BoringNumber();
ob.take();
}
}
Sample Input Output
Enter The Number:1234
Entered Number is:1234
Entered Number is a boring number
Entered Number is:1234
Entered Number is a boring number
Enter The Number:1321
Entered Number is:1321
Entered Number is not a boring number
No comments:
Post a Comment