Subscribe

RSS Feed (xml)

Friday, April 16, 2010

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.

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

9 comments:

  1. cant we add 11111 simply to the no....lolz

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Why all these lines of C code?
    Just add 11111 to the given number..
    Keep it simple...
    The task must be like increment each digit by 1..
    then 12391 becomes 23401...
    "if digit is '9' then result = 0" [ a sample algorithm...]

    ReplyDelete
  4. 11111 can be added simply to the inputted no. y all this crap??

    ReplyDelete
  5. @shriram you can improve how your blog looks, the black background is bad! have a look at
    mytechbooks.blogspot.com
    its my blog and its the new look of blogger

    ReplyDelete
  6. #include
    int main()
    {
    int a,b=0,c=0,d=0,e=0;
    printf("Enter te no: ");
    scanf("%d",&a);
    b=a;
    while(b>0)
    {
    b=b/10;
    ++c;
    }
    printf("No. of digits %d ",c);
    while(c!=0)
    {
    d=d*10+1;
    c--;
    }
    e=a+d;
    printf(" %d",e);

    getch();
    }

    ReplyDelete