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; } }
Favolas
source share