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; } } }