Sunday 16 August 2015

If a 5 digit no is input through the keyboard,WAP in java to print a new no by adding 1 to each of its digit.For eg. If input is 12391 then output should be 23402.

//Add 1 to each digit

import java.util.Scanner;


class No_add1 {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a 5 digit no:");
int no=sc.nextInt();

if(no<10000 || no>99999)
System.out.println("INVALID INPUT");
else
{
int a,b,c,d,e;

a=no%10;
no=no/10;

b=no%10;
no=no/10;

c=no%10;
no=no/10;

d=no%10;
no=no/10;

e=no;

if(a==9)
a=0;
else
a=a+1;


if(b==9)
b=0;
else
b=b+1;

if(c==9)
c=0;
else
c=c+1;

if(d==9)
d=0;
else
d=d+1;

if(e==9)
e=0;
else
e=e+1;

no=a+(b*10)+(c*100)+(d*1000)+(e*10000);

System.out.println("New no:"+no);
}
}


}

No comments:

Post a Comment