Thread: Need help on exercise!

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    2

    Need help on exercise!

    Hello to everyone. I am new to programming, recently started with some small exercises where i am supposed to have my user do this



    * user to enter the number of digits
    * user to enter the n-digit number
    Note: The n-digit number is read in using a scanf statement and you are
    not allowed to read in the n-digit number as individual digits or characters
    * to seperate the digits from the n-digit number entered using repetition statements
    (i.e. you should use while or for loop in your program)
    * Hint: you can use a combination of integer division (/) and reminder operation (%).


    * The final result is to display the individual digits with 3 spaces in
    between the digits.
    For e.g., if n is 5 and the number entered is 54321, the printout will be
    5 4 3 2 1

    my code is roughly there, i can produce all the outputs except for a few exception. Every input that has a '0' at the end of the number will have the 0 omitted, for e.g. 10 will have an output of 1, same as with 100, 1000 so forth. How can i fix this? Any help would be appreciated




    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    
    
    {
    
    
    	int testInteger;
    	int x;
    	int digit;
    	int counter;
    	
    	
    		printf("Please enter the number of digit(s)\n");
    		scanf("%d",&digit);
    	
    	if (digit < 0 && digit > 9) {						
    			
    		printf("Invalid digit, please try a new value\n");
    		scanf("%d",&digit);
    		
    		}	
    		
    			
    	else{
    	
    	counter = pow(10,digit -1);		
    	
    		printf("Enter an integer\n");
    		scanf("%d", &testInteger);
    		printf("Number = %d\n", testInteger);
    	
    	
    	
    	while(testInteger>0 )
    	{
    		
    		x = testInteger/counter;			 
    		printf("%d   ",x);		
    		testInteger = testInteger%counter;  
    		counter = counter/10;	
    	}
    	
    	
    	}
    	}

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Perhaps:
    Code:
    #include <stdio.h>
     
    typedef unsigned long long ull;
     
    int main()
    {
        ull n;
        scanf("%llu", &n);
     
        ull mag = 1;
        for (ull m = n; (m /= 10) != 0; mag *= 10)
            ;
     
        for ( ; mag > 1; mag /= 10)
            printf("%llu   ", n / mag % 10);
        printf("%llu\n", n % 10);
     
        return 0;
    }
    Or maybe:
    Code:
    #include <stdio.h>
     
    typedef unsigned long long ull;
     
    void print_digits(ull n)
    {
        if (n > 9) print_digits(n / 10);
        printf("%llu   ", n % 10);
    }
     
    int main()
    {
        ull n;
        scanf("%llu", &n);
        print_digits(n);
        putchar('\n');
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    2
    Hey johnc,

    I thank you for your work! Wow that is some pretty complicated stuff, it may be a tad too difficult for me to comprehend now, i was thinking of perhaps if i could add an if statement in the while loop to make the 0 appear as it seems as though the missing zero comes from a division by zero error but that i'm not really sure how to execute the steps. I'll keep your solution for future references though! Thanks




    Quote Originally Posted by john.c View Post
    Perhaps:
    Code:
    #include <stdio.h>
     
    typedef unsigned long long ull;
     
    int main()
    {
        ull n;
        scanf("%llu", &n);
     
        ull mag = 1;
        for (ull m = n; (m /= 10) != 0; mag *= 10)
            ;
     
        for ( ; mag > 1; mag /= 10)
            printf("%llu   ", n / mag % 10);
        printf("%llu\n", n % 10);
     
        return 0;
    }
    Or maybe:
    Code:
    #include <stdio.h>
     
    typedef unsigned long long ull;
     
    void print_digits(ull n)
    {
        if (n > 9) print_digits(n / 10);
        printf("%llu   ", n % 10);
    }
     
    int main()
    {
        ull n;
        scanf("%llu", &n);
        print_digits(n);
        putchar('\n');
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It really isn't very complicated.
    Asking the user for the number of digits in the input number is not a good solution since the program can calculate that (which is what mag is for).
    Here it is with just ints if that makes it look easier:
    Code:
    #include <stdio.h>
     
    int main()
    {
        int n;
        scanf("%d", &n);
     
        // determine the magnitude
        int mag = 1;
        for (int m = n; (m /= 10) != 0; mag *= 10)
            ;
     
        // strip off the digits from high-order to low-order
        for ( ; mag > 1; mag /= 10)
            printf("%d   ", n / mag % 10);
        printf("%d\n", n % 10);
     
        return 0;
    }
    Using unsigned long long instead of int allows longer numbers.
    Max 32-bit signed int is 2147483648.Max 64-bit unsigned long long is 18446744073709551616.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RND exercise
    By Lag in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2019, 08:39 AM
  2. exercise
    By telonio in forum C Programming
    Replies: 3
    Last Post: 10-18-2015, 10:01 AM
  3. Exercise 5-17 k&R
    By BinaryProgamer in forum C Programming
    Replies: 1
    Last Post: 06-22-2013, 06:51 AM
  4. K&R Exercise 1-9
    By Anarchy in forum C Programming
    Replies: 6
    Last Post: 01-10-2010, 12:42 PM
  5. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM

Tags for this Thread