0

before starting Pset1, I was trying out the exercise at the end of every chapter. In chapter six there is a question where you have to input a number say 1234 and the program must output the same number in english as One Two Three Four. I have written this program but cant seem to find why its not working for a single digit, 3 digit and 5 digit numbers

include

include

int main (void) { int number, leftdigit; do { printf("Enter a number max limit 6 digits \n"); number = GetInt(); } while (number > 999999);

/* to start a loop from max position of six digits
the logic is if a number is six digit on first iteration i 
wud be 100000 so number divided 100000 wud give the left most digit(point)something*/
for (int i = 100000; i >= 1; i = i / 10)
{
    /* if number is less than 6 digits the for loop is made to go for second 
    iteration, if its less than 6 digits it would give a 0 sicne exmple 23/100
    wud give 0.23 but integer arithmetic wud give a 0*/

    if (number / i == 0)
    {
        i = i /10;
    }
    else
    {
       /* logic is ex number is 2345.... 2345/1000 = 2.345 but as its a int
       2 would be stored and 2%10 = 2, for second iteration 2345/100 = 23.45
       and 23%10= 3 */

       leftdigit = (number / i) % 10;

       switch (leftdigit)
       {
            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;
            default:
                printf("unknown character");
                break;



       }
    }
}
printf("\n");

}

1 Answer 1

0

I'm sorry, I should have read your post more carefully, I thought the while loop encompassed your whole code, turns out you were using it for handling errors (in case input has more than 6 digits).

It seems to me that the problem lies in the line

i = i /10 

of your if statement. If number is 54321, for example, first iteration of i will have value i=100,000, since number/i==0, that line will be executed: i=i/10=10,000. It then goes back to your for loop, where it is divided by 10 again to yield i=1,000, thereby skipping 10,000 altogether. Notice it works for even numbers of digits but not for odd numbers.

I suggest replacing i=i/10 in your if statement to the command "continue". Continue will cause the next iteration of your for loop, so instead of dividing i by 10 twice, you're only doing it once.

2
  • if I change the while condition to number < 999999, then the loop is not running at all....you plz run the code yourself too, im getting correct outputs for 2 , 4 & 6 digit numbers, but with the others the left most digit get missed May 8, 2016 at 15:42
  • Please see edit above.
    – ronga
    May 9, 2016 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .