I am writing a code to display the digits of any positive integer in C language. For example, integer 345 would displayed as three four five.
The code I wrote works fine for integers with all digits greater than 0. But certain integers like 10, 304, 0 etc don't get displayed correctly. Usage of recursion and array is not allowed for the sake of challenge. Only looping and if constructs are allowed. Any suggestions?
#include<stdio.h>
int main(void)
{
int num, num_copy, accum = 1;
printf("Enter an integer ");
scanf("%i",&num);
num_copy = num;
while (num > 0){
while (num > 9){
num = num / 10;
accum *= 10;
}
switch (num){
case (1):
printf("one ");
break;
case (2):
printf("two ");
break;
case (3):
printf("three ");
break;
case (4):
printf("four ");
break;
case (5):
printf("five ");
break;
case (6):
printf("six ");
break;
case (7):
printf("seven ");
break;
case (8):
printf("eight ");
break;
case (9):
printf("nine ");
break;
}
num_copy = num_copy - (num*accum);
num = num_copy;
accum = 1;
}
return 0;
}
Ohh! This sounds like fun! No arrays and no recursion right?
So since we can't use recursion we need to build the number backwards:
#include<stdio.h>
int main(void)
{
int num, backwards = 0, digit, backupzeros = 0;
printf("Enter an integer ");
scanf("%i",&num); // get the number
while(num > 0){
digit = num % 10; // pry off the last digit
num /= 10; // take off the digit
if((backwards == 0) && (digit == 0)) // If it's a number that ends in 0
backupzeros++; // save it, we'll use that later
backwards = (backwards * 10) + digit; // glue it on backwards
}
// Now we have the number reversed. Next we need to print the digits
while (backwards > 0){
digit = backwards % 10;
backwards /= 10;
switch (digit){
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
default:
printf("zero ");
break;
}
}
while(backupzeros > 0) {
printf("zero ");
backupzeros--;
}
return 0;
}
If you write a recursive function, it would help you avoid the loops that you use to print in the reverse order:
#include<stdio.h>
void print_num(int num)
{
if(num == 0) return;
print_num(num/10);
switch (num%10){
case (1):
printf("one ");
break;
case (2):
printf("two ");
break;
case (3):
printf("three ");
break;
case (4):
printf("four ");
break;
case (5):
printf("five ");
break;
case (6):
printf("six ");
break;
case (7):
printf("seven ");
break;
case (8):
printf("eight ");
break;
case (9):
printf("nine ");
break;
case (0):
printf("zero ");
break;
}
}
int main(void)
{
int num, num_copy, accum = 1;
printf("Enter an integer ");
scanf("%i",&num);
print_num(num);
return 0;
}
Non-recursive solution which uses an array to store the numbers:
void print_num(int num)
{
int i=0,j;
int arr[256];
while(num)
{
arr[i++]=num%10;
num/=10;
}
for(j=i-1; j>=0;j--)
{
switch (arr[j]){
case (1):
printf("one ");
break;
case (2):
printf("two ");
break;
case (3):
printf("three ");
break;
case (4):
printf("four ");
break;
case (5):
printf("five ");
break;
case (6):
printf("six ");
break;
case (7):
printf("seven ");
break;
case (8):
printf("eight ");
break;
case (9):
printf("nine ");
break;
case (0):
printf("zero ");
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With