Saturday, January 30, 2010

CHAP 1[H]m Incrementing by 1 for every digit of a number

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.

void main()
{
float a,b,c,d,e,t,s;
int a1,b1,c1,d1,e1,a2,b2,c2,d2,e2;
printf("\nInput a 5 digit number");
scanf("%f",&t);

a=t/10000.0;
a1=a;
b=(t-(a1*10000.0))/1000;
b1=b;
c=(t-(a1*10000.0)-(b1*1000))/100;
c1=c;
d=(t-(a1*10000.0)-(b1*1000)-(c1*100))/10;
d1=d;
e=(t-(a1*10000.0)-(b1*1000)-(c1*100)-(d1*10))/1;
e1=e;


a2=a1+1;
b2=b1+1;
c2=c1+1;
d2=d1+1;
e2=e1+1;

if (a2==10)
a2=0;
if (b2==10)
b2=0;
if (c2==10)
c2=0;
if (d2==10)
d2=0;
if (e2==10)
e2=0;


s=(a2*10000.0)+(b2*1000.0)+(c2*100.0)+(d2*10.0)+(e2);


printf("\nThe required result is %f",s);
}

19 comments:

  1. who idiot write this typical prog...

    problem is for 5 dig no.
    so simply add 11111 to the no.

    ReplyDelete
  2. never mind the idiot who commented first.


    i want to know why you multiplied the integers with 10000.0

    a=t/10000.0;
    a1=a;
    b=(t-(a1*10000.0))/1000;
    b1=b;
    c=(t-(a1*10000.0)-(b1*1000))/100;
    c1=c;
    d=(t-(a1*10000.0)-(b1*1000)-(c1*100))/10;
    d1=d;
    e=(t-(a1*10000.0)-(b1*1000)-(c1*100)-(d1*10))/1;
    e1=e;

    ReplyDelete
  3. This is a more streamlined version:
    void main()
    {
    float s,t;
    int a,b,c,d,e;
    printf("\nInput a 5 digit number");
    scanf("%f",&t);

    a=t/10000.0;
    b=(t-(a*10000.0))/1000;
    c=(t-(a*10000.0)-(b*1000))/100;
    d=(t-(a*10000.0)-(b*1000)-(c*100))/10;
    e=(t-(a*10000.0)-(b*1000)-(c*100)-(d*10))/1;

    a+=1;
    b+=1;
    c+=1;
    d+=1;
    e+=1;

    if (a==10)
    a=0;
    if (b==10)
    b=0;
    if (c==10)
    c=0;
    if (d==10)
    d=0;
    if (e==10)
    e=0;

    s=(a*10000.0)+(b*1000.0)+(c*100.0)+(d*10.0)+(e);

    printf("\nThe required result is %f",s);
    }

    ReplyDelete
  4. For this part "a*10000.0", i put a ".0" because i want it to be a float. If i had put "a*10000", then an Int multiplied by an Int will give a resultant Int.

    Since the max value for a 16 Bit Int (which is the int size in the Borland Compiler) is 32767, if your 5 digit number is more than 39999, then the first digit "a" when multiplied by "10000" will surely have a value more than 32767 and when stored in a resultant Int leads to a rubbish value.

    When i referring to the resultant Int, i am not referring to variables a,b,c,d or e, what i am referring to is the intermediate calculated value. Therefore i want the intermediate value to be a float and writing it this way "a*10000.0" (Int multiplying with a Float , where the float is "10000.0") will allow the resultant large value to be stored correctly in an intermediate float value.

    You can try compiling the program by editing all the "a*10000.0" to "a*10000" and you will realise any value larger than 39999 and the program will not work correctly.

    ReplyDelete
  5. this prog can be done more fast using % operator and a loop... And adding 11111 is not a soln. Coz it gives wrong result in case of a '9'.

    ReplyDelete
  6. Think less complicated always:
    Try this


    main( )
    {

    int total,next,add,result;
    printf("Enter The 5 digit number to add 1 with each digit=");
    scanf("%d",&total);
    //1st digit
    result=(total/10000)%10+1;
    //2nd digit
    next=(total/1000)%10+1;
    result=(result*10)+next;
    //3rd digit
    next=(total/100)%10+1;
    result=(result*10)+next;
    //4th digit
    next=(total/10)%10+1;
    result=(result*10)+next;
    //5th digit
    next=total%10+1;
    result=(result*10)+next;

    printf("After Addition your number %d becomes %d\nProgrammed by JEET",total,result);
    }

    ReplyDelete
    Replies
    1. Jeet Can u please explain me?
      Your program is working,But when I try to do the same manually for understanding purpose I am not getting the same value.
      Here / is used to get the quotient.
      % for remainder.
      But ,I am not getting 23502.
      Are you using float value while doing it manually ,or integer iteself? Please explain am so damn stucked at this question.

      Delete
    2. this program is wrong. as u don't get the right answer with any 9 included in five digit number.If we take the above example of 12391,we should get 23402.but u get 23502 as u are just adding even the number exceeds 9 which changes 10's place by 1.

      Delete
    3. try this....
      //4th digit
      next=((total/10)%10+1)%10;
      result=(result*10)+next;

      Delete
  7. /*If a five-digit number is input through the keyboard,
    write a program to reverse the number*/
    //Author::Mudasir Yaqoob.....

    #include
    #include

    int main()
    {

    long number,t;
    int i=0;
    long temp[5];
    printf("Enter the five digit number:\n\n");
    scanf("%ld",&number);
    while(i<=4)
    {

    t=number%10+1;
    temp[i]=t;
    number=number/10;
    i++;

    }
    printf("Reverse number\n\n\n\n");
    for(i=4;i>=0;i--)
    {
    printf("%ld",temp[i]);
    }
    getch();
    }

    ReplyDelete
  8. possibly the best ans which increment each digit of any integer number.....
    #include
    #include
    void main()
    {
    long int nn=0,a,b,num,i=1;
    clrscr();
    printf("enter any num to increment each digit\n");
    scanf("%ld",&num);
    while(num!=0)
    {
    a=num/10;
    b=num%10;
    num=a;
    if(b==9) b=0;
    else b=b+1;
    nn=nn+b*i;
    i*=10;
    }
    printf("%ld",nn);
    getch();
    }

    ReplyDelete
  9. What if the typed number is 99999?

    ReplyDelete
  10. is this the solution of the book let us C??

    ReplyDelete
  11. #include
    #include
    main()
    {
    int num,nu1,mas=0,sum=0;
    printf("Enter the number");
    scanf("%d",&num);
    nu1=num;
    while(nu1>0)
    {
    nu1=nu1/10;
    mas=mas*10+1;
    }
    sum=sum+num+mas;
    printf("\n%d",sum);
    getch();
    }

    ReplyDelete
  12. This is the basic and easy concept of that question and U can generalize it

    ReplyDelete
  13. http://letuscalllessons.blogspot.in/

    ReplyDelete