CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    1

    Could someone know how this code work?

    we need to make a pseudo code of this code


    #include <stdio.h>

    int main()
    {
    int n,r[10],i;
    printf("Enter a number to get its hexadecimal equivalent\n");
    scanf("%d",&n);
    for(i=0; n!=0; i++)
    {
    r[i]=n%16;
    n=n/16;
    }
    i--;

    for(; i>=0; i--)
    {
    if(r[i]==10) printf("A");
    else if(r[i]==11) printf("B");
    else if(r[i]==12) printf("C");
    else if(r[i]==13) printf("D");
    else if(r[i]==14) printf("E");
    else if(r[i]==15) printf("F");
    else printf("%d",r[i]);
    }
    printf("\n");
    }

  2. #2
    Join Date
    Jun 2012
    Location
    India
    Posts
    5

    Re: Could someone know how this code work?

    The key is in the operators % and /. The first one is modules operator and the second one is division.

    When you do n%16, the number is divided by 16 and reminder is stored in your array of capacity 10. Then you move to next by dividing it 16 and storing it in n. N will become zero and you will come out of the first loop leaving all base 16 number system in your array.

    Next you enter one more loop and print the array content by replacing 10-15 with hexadecimal value. Enough

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured