开发者

C write number digits, one by one and in extension, in a new line

开发者 https://www.devze.com 2023-02-14 20:11 出处:网络
I\'m new in C and I can\'t do a simple exercise for school. I want to do something like this: Please insert a number: 12345

I'm new in C and I can't do a simple exercise for school.

I want to do something like this:

 Please insert a number: 12345
 five
 four
 three
 two
 one

Basically, the user inputs a number and them the program writes, in a new line, the number from the last significant number to the most.

This is to do with the switch function and only basic programming skills.

I have this:

#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 I use a number between 0 and 9 it works ok but a number bigger then that it does not do nothing.

The first problem i cant solve is to, in a number, get the digit position. I believe that In my code, the break isn't doing nothing...

Sorry if I can't explain me better but English is not my native language.

Regards,

Favolas

############################## In Progress Solution (does not work if number % 10 gives 0¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
#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;
    }
} 


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

You need to isolate the individual digits from the input.

Hint: use the / and % operators

 12345 % 10 ==> 5
 12345 / 10 ==> 1234

 1234 % 10 ==> 4
 1234 / 10 ==> 123

 123 ... ... ...


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

So you have to "traverse" the number digit by digit. You can extract the last digit if you take modulo 10: 12345 % 10 == 5. Moreover, you can cut off the last digit by dividing by 10: 2345 / 10 == 234 (due to integral division).

So, you have to write a loop that extract the single digits from num and then apply your case distinction for each digit.


In base 10, the number 12345 is in fact 1 * 10000 + 2 * 1000 + 3 * 100 + 4 * 10 + 5 * 1. So you can get the last digit of a number by computing the remainder of the division of the number by 10. And the dividend will be the most significant par of the number. So, '12345 = 1234 * 10 + 5. InC, the%operator is used to compute the remainder of the division, so12345 % 10is equal to5`.

Then, you just have to loop while the number is non-zero, taking the remainder at each step, displaying the digit and then dividing the number.


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

精彩评论

暂无评论...
验证码 换一张
取 消