C enter numeric digits, one after the other and in the extension, in a new line

I am new to C and I cannot do a simple exercise for school.

I want to do something like this:

Please insert a number: 12345 five four three two one 

In principle, the user enters a number and their program writes in a new line a number from the last significant number to the largest.

This is due to the switching function and basic programming skills.

I have it:

 #include <stdio.h> #include <stdlib.h> int main() { int num; printf("Please insert a number: "); scanf("%d", &num); switch(num){ case 1:printf("One\n");break; case 2:printf("Two\n");break; case 3:printf("Three\n");break; case 4:printf("Four\n");break; case 5:printf("Five\n");break; case 6:printf("Six\n");break; case 7:printf("Seven\n");break; case 8:printf("Eight\n");break; case 9:printf("Nine\n");break; case 0:printf("Zero\n");break; } } 

I use a number from 0 to 9, it works fine, but the number is greater than the fact that it does nothing.

The first problem that I cannot solve is to get a digital position in the number. I believe that in my code the gap does nothing ...

Sorry if I can’t explain me better, but English is not my first language.

Hi,

Favolas

################################ In Progress Solution (does not work if the number% 10 gives 0¬¬¬¬¬¬¬ ¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ look
 #include <stdio.h> #include <stdlib.h> int main() { int num; printf("Please insert a number: "); scanf("%d", &num); int i,digit; for (i = 0; num%10!=0;i++){ digit = num % 10; switch(num){ case 1:printf("One\n");break; case 2:printf("Two\n");break; case 3:printf("Three\n");break; case 4:printf("Four\n");break; case 5:printf("Five\n");break; case 6:printf("Six\n");break; case 7:printf("Seven\n");break; case 8:printf("Eight\n");break; case 9:printf("Nine\n");break; case 0:printf("Zero\n");break; } num = num / 10; } } 
+7
source share
4 answers

The number 123 is 100 + 20 + 3 or 1*10^2 + 2*10^1 + 3*10^0 .

You need to isolate individual numbers from input.

Hint: use the / and % operators

  12345 % 10 ==> 5 12345 / 10 ==> 1234 1234 % 10 ==> 4 1234 / 10 ==> 123 123 ... ... ... 
+8
source

If you enter a larger number, num will contain exactly that number, i.e. none of your cases ( 0 to 9 ) matches the num value.

Thus, you need to “cross” a digit by digit by digit. You can extract the last digit if you use modulo 10: 12345 % 10 == 5 . Moreover, you can cut off the last digit by dividing it by 10: 2345 / 10 == 234 (due to integral division).

So, you need to write a loop that extracts individual digits from num , and then applies the difference in each case.

+1
source

In base 10, the number 12345 is actually 1 * 10000 + 2 * 1000 + 3 * 100 + 4 * 10 + 5 * 1 . Thus, you can get the last digit of a number by calculating the remainder of dividing the number by 10 . And the dividend will be the most significant number. So, '12345 = 1234 * 10 + 5 . In . In C , the % operator is used to compute the remainder of the division, so 12345% 10 is equal to 5`.

Then you just need to execute the loop until the number is non-zero, taking the remainder at each step, displaying the digit and dividing the number.

0
source
 void main (void){ int num; int factor=10; printf("Please insert a number: "); scanf("%d", &num); while(num>0){ switch(num%factor){ case 1:printf("One\n");break; case 2:printf("Two\n");break; case 3:printf("Three\n");break; case 4:printf("Four\n");break; case 5:printf("Five\n");break; case 6:printf("Six\n");break; case 7:printf("Seven\n");break; case 8:printf("Eight\n");break; case 9:printf("Nine\n");break; case 0:printf("Zero\n");break; } num=num/factor; } } 
-one
source

All Articles