Hi all,
I'm new to programming and going through Stephen Kochan's book, Programming in Objective - C. I have a question about exercise #6 in chapter 6, for anyone who is familiar with the book. Mr. Kochan said that that exercise is hard, and for me it is... I haven't been able to figure it out yet. Below is what I have so far, and what I have prints out the number entered at the terminal in english, like it is supposed to. But it does it in reverse order. It seems to me that there should be able to be some way to initially reverse the entered number, and then run the rest of the program I have, but I can't figure out how. Help? Thanks!
// Program to print the numbers entered in english
#import <stdio.h>
#import <objc/Object.h>
int main (int argc, char *argv[])
{
int number, right_digit;
printf ("Enter your number.\n");
scanf ("%i", &number);
//seems like I should reverse the entered number here, but I can't figure out how.
do { //gets the last number
right_digit = number % 10;
//prints the last number as a word
if (right_digit == 0) {
printf("zero");
}
else if (right_digit == 1){
printf("one ");
}
else if (right_digit == 2){
printf("two ");
}
else if (right_digit == 3){
printf("three ");
}
else if (right_digit == 4){
printf("four ");
}
else if (right_digit == 5){
printf("five ");
}
else if (right_digit == 6){
printf("six ");
}
else if (right_digit == 7){
printf("seven ");
}
else if (right_digit == 8){
printf("eight ");
}
else if (right_digit == 9){
printf("nine ");
}
// lose last number from number entered
number /= 10;
} while (number != 0);
return 0;
}
I'm new to programming and going through Stephen Kochan's book, Programming in Objective - C. I have a question about exercise #6 in chapter 6, for anyone who is familiar with the book. Mr. Kochan said that that exercise is hard, and for me it is... I haven't been able to figure it out yet. Below is what I have so far, and what I have prints out the number entered at the terminal in english, like it is supposed to. But it does it in reverse order. It seems to me that there should be able to be some way to initially reverse the entered number, and then run the rest of the program I have, but I can't figure out how. Help? Thanks!
// Program to print the numbers entered in english
#import <stdio.h>
#import <objc/Object.h>
int main (int argc, char *argv[])
{
int number, right_digit;
printf ("Enter your number.\n");
scanf ("%i", &number);
//seems like I should reverse the entered number here, but I can't figure out how.
do { //gets the last number
right_digit = number % 10;
//prints the last number as a word
if (right_digit == 0) {
printf("zero");
}
else if (right_digit == 1){
printf("one ");
}
else if (right_digit == 2){
printf("two ");
}
else if (right_digit == 3){
printf("three ");
}
else if (right_digit == 4){
printf("four ");
}
else if (right_digit == 5){
printf("five ");
}
else if (right_digit == 6){
printf("six ");
}
else if (right_digit == 7){
printf("seven ");
}
else if (right_digit == 8){
printf("eight ");
}
else if (right_digit == 9){
printf("nine ");
}
// lose last number from number entered
number /= 10;
} while (number != 0);
return 0;
}