#20
Beautifully done!!!
This is the way I did it.. by my self..
Code:
int number,divider,divcounter,left_Num, count, cKeeper;
NSLog(@"Enter your number.");
scanf("%i", &number);
left_Num = number; //Set User number
do //No how many digits
{
left_Num /= 10;
++cKeeper;// How many time loop have 2 run
}
while (left_Num != 0);
left_Num = number; //reload user number
while (cKeeper != 0)
{
divider = 10;
count = 0;
do //No how many digits
{
left_Num /= 10;
++count; //how many time run loop to look for correct number
}
while (left_Num != 0);
count = 1; //Take one out so we get fisrt digit
left_Num = number; //need to keep 'number' intact
divcounter = cKeeper; //Set counter to increase divider
do //Get the digits to diplay number in a word.
{
if (count !=0) //Avoid division of last digits.
left_Num /= 10;
count;
for(divcounter; divcounter > 2; divcounter)
{
divider *= 10; //set divider to set correct residual to number
}
}
while (count > 0);
number %= divider; //Get new number value
switch (left_Num) //Display Correct Msg
{
case 0:NSLog(@"Zero");break;
case 1:NSLog(@"One");break;
case 2:NSLog(@"Two");break;
case 3:NSLog(@"Three");break;
case 4:NSLog(@"Four");break;
case 5:NSLog(@"Five");break;
case 6:NSLog(@"Six");break;
case 7:NSLog(@"Seven");break;
case 8:NSLog(@"Eight");break;
case 9:NSLog(@"Nine");break;
default:NSLog(@"Not a digits");break;
}
left_Num = number; //reload user number
cKeeper; //One time less
}
Dint know that you can accumulate and integer like that. Learn something new every day...

 File size:
 10.3 KB
 Views:
 20
#21
After sleep an take another look on what I did, find to many unnessesary thing so I rewrote it
this my new solution:
Code:
int number, left_Num, count, divider;
NSLog(@"Enter your number.");
scanf("%i", &number);
divider = 1; //Set divider
left_Num = number;//Set User number
count = 0; //set counter to zero
while (left_Num > 9)//stop in last digits user enter
{
divider *= 10; //Set the divider to get left num.
left_Num /= 10; // reduce value to end loop
++count; //create count for next loop
}
++count; //since above loop was set to finish 1 digits early we add it here to the count
while (count != 0)
{
left_Num = number / divider; //get the left num
number %= divider; //Get the rest of number digits (ex. if 932, result is 32)
divider /= 10; // decrease divider for update number (ex. update number is 32, divider is 100, result is divider = 10
count; // decrease counter to finish loop
switch (left_Num)//Display Correct Msg
{
case 0:NSLog(@"Zero");break;
case 1:NSLog(@"One");break;
case 2:NSLog(@"Two");break;
case 3:NSLog(@"Three");break;
case 4:NSLog(@"Four");break;
case 5:NSLog(@"Five");break;
case 6:NSLog(@"Six");break;
case 7:NSLog(@"Seven");break;
case 8:NSLog(@"Eight");break;
case 9:NSLog(@"Nine");break;
default:NSLog(@"Not a digits");break;
}
}
[pool drain];
return 0;
}