Note: If a five digit number is input through keyboard ,write a program to print a new number by adding one to each of tis digits for example a number is input is 12391 then the output should be displayed as 23502 .
#include< stdio.h>
#include< conio.h>
int main()
{
long int no,sum=0;
int rem,check=0;
clrscr( );
printf("Enter the required number:");
scanf("%ld",&no);
printf("\nGiven Number: %d",no);
while(no>0) // to store the number in reverse by adding one to each digit
{
rem=no%10;
if(rem!=9)
{
if(check==0) // imp , try to take few minutes here
sum=(10*sum)+(rem+1);
else{
sum=(10*sum)+(rem+2);
check=0;
}
} //if closed
else{
sum=(10*sum)+0;
check=1;
}
no=no/10;
} // while closed
// Afftr adding , again to print the reverse number in origianl form.
no=sum; sum=0;
while(no>0)
{
rem=no%10;
sum=(10*sum)+rem;
no=no/10;
}
printf("\nAfter Adding one: %ld",sum);
getch( );
return 0;
}
Output:
Enter the reuired Number: 12391
Given Number: 12391
After Adding one to each of its digits: 23502
Friday, April 16, 2010
Subscribe to:
Posts (Atom)