Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Algorithm or program in basic c language to print the reverse of a number...please help?
5 Answers
- KevMartLv 48 years agoFavorite Answer
satish's answer is incorrect as it always outputs a 0 as the last digit instead off the first digit of the original number. For example, take 12345, the algorithm he/she has proposed will output 54320.
A quicker (shorter) approach would be to use the modulus.
1. We take the number's remainder when divided by 10 by using "remainder = number % 10" - without quotes.
2. We divide the original number by 10.
3. Print the remainder out.
4. If number is zero, then terminate, otherwise, go to step 1.
void printReverseOfNumber(int number)
{
int remainder;
while(number != 0)
{
remainder = number % 10;
number /= 10;
printf("%d", remainder);
}
}
int main(int argc, char* argv[])
{
printReverseOfNumber(12345); // outputs 54321
return 0;
}
- satishLv 48 years ago
let us say the number is 12345
first you divide the number with 10
you get 1234 now multipluy this with 10 '
you get 12340 subtract this from 12345 you get 5 which is the last number.
now repeat the same with
1234 this time divide with 10
123 multiply witn 10 1230
subtract
1234-1230 = 4
we do this till the quoitiet is 0
int printrev(int number)
{
int temp;
while(1)
{
int temp = (number/10)*10;
if (temp ==0)
{
printf("%d",temp);
break;
}
temp = number -temp;
printf("%d\t",temp);'
number = number/10;
}
}
- How do you think about the answers? You can sign in to vote the answer.