-
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 :devil:
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;
}
}
}
-
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;
}
-
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
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;
}
-
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.