Tuesday, April 14, 2009

Print a five digit number by adding one to each of its digits

/*If a five-digit number is input through the keyboard, write a
program to print a new number by adding one to each of its
digits. For example if the number that is input is 12391 then
the output should be displayed as 23402.*/


Solution Code :-
=========

#include <stdio.h>
int main(void)
{
int i,temp = 0;
printf("Enter a +ve 5 digit number :");
scanf("%d",&i);
if (( i <= 99999 ) && ( i > 9999 ))
{
printf(" %d",(i/10000) + 1);
printf(" %d",((( i/100) % 100)/10) + 1);
printf(" %d",(( i/100) % 10 ) + 1);
printf(" %d",((i % 100) / 10) + 1 );
printf(" %d",(i % 10) + 1);
}
else
printf("\n Pl.enter +ve five digit number");
return 0;
}

4 comments:

Anonymous said...

Hi,

I think the more generic approach would be by using following code i wrote.. you can use this also :


int mask=1,i=0,output=0;
printf("Enter the number \n ");
scanf("%d",&i);
while(mask<i)
{
output += ((((i/mask)%10+1)%10)*mask);
mask *=10;
}

printf("The output number is = %d \n",output);

Anonymous said...

exception at 99999

Anonymous said...

exception at 99999

Unknown said...
This comment has been removed by a blog administrator.