Thread: printf problem

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    printf problem

    A friend ask me to help with his code. the input will be as following:

    1st input -> number of loop
    2nd input -> given number
    3rd input -> number of rotation

    example:

    1
    123045
    3

    The result is 045123

    He told me that he can only get 45123 as the result. the 0 is missing. Is there any way to display it ?? i'm not very familiar with C or C++ though

    here is his code. thx in advance

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    long int num,n,value, digitNum, ctr, NumX;
    int left, right;
    long b;
    
    scanf("%ld", &b);
    
    while (b != 0)
    {
          
    scanf("%d %d", &num, &n);
    
    value = num;
    NumX = 1;
    
         while (value!= 0)
         {
          NumX = NumX * 10;
          value = value / 10;
         }
         
         NumX = NumX / 10;
         
         ctr = 0;
         
         while (ctr != n)
         {
               left = num / 10;
               right = num % 10;
               num = right * NumX + left;
               ctr++;
               
               }
               
               printf("%d", num);
               
    printf("\n");
    b--;
    }
    
    fflush(stdin);getch();
    return 0;
    }
    Last edited by blackmisery; 11-01-2006 at 02:01 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Maybe it is easyer to convert number to string and work with the string?

    As for the original question - yes using format modifiers the number can be printed with zeroes filling it up to desired length
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    can u explain more about this format modifiers ??? is it something like

    Code:
    {
        ("%.2f", variable) ;
    }
    i'm not very sure as well.

    and for the code, my friend told me that his so called "Genius" lecture insist of using number instead of string. This because my friend is only in his 2nd month of his new semester.

    i tried to help him by using array but still this "genius" lecture reject it. he still insist to use number without array or anything.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes it is format modifier
    http://msdn.microsoft.com/library/de..._functions.asp

    read about width and preceiding zeroes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Code:
    {
        ("%.2f", variable) ;
    }
    as far as i knw abt this if the statement is like as follows

    Code:
     printf  ("%.2f", variable) ;
    the value after the float will be ristricted to two digits

    the answer wud be sumthing like
    10.22

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    What about something like ...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        unsigned int i, num, num_digits, num_loops, num_rots;
        int t;
    
        printf("Enter number of loops : ");
    
        if ( scanf("%u", &num_loops) != 1 )
        {
            /* Error */
        }
    
        for (i = 0; i < num_loops; i++)
        {
            printf("Enter number to rotate : ");
    
            if ( scanf("%u", &num) != 1 )
            {
                /* Error */
            }
    
            if ( num == 0 ) num_digits = 1;
            else            num_digits = 1 + (int) log10( (double) num );
    
            printf("Enter number of rotations : ");
            if ( scanf("%u", &num_rots) != 1 )
            {
                /* Error */
            }
    
            printf("Rotated number is : ");
            for (i = num_digits; i > 0; i--)
            {
                t = i + num_rots - 1;
                if ( t >= num_digits) t -= num_digits;
                printf("%u", (num / (int)pow(10,t)) % 10);
            }
            printf("\n");
        }
    
        getch();
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM