Runescapian321
2008-02-11 21:52:00
I have to 'Write a program in C that displays a reversed number' (if you input 4567 the output should be 7654). I googled this and found that using strings or arrays would make it easy, but I haven't learned how to use those yet so I suppose I'm not supposed to use them. Here is what I have so far…
I'm having trouble with the loop. I'm not doing it for any specific amount of digits, so I can't do this
I can't figure out how to make it so that it'll work for any amount of digits….
int main()
{
int num1, reversed, num3, original, num5, lastnum;
printf("Please enter an integer: ");
num1 = GetInteger();
original = num1;
num5 = numdigits(num1); //numdigits is a function that finds the amount of digits
reversed = 0;
lastnum = num1 - ((num1/10)*10);
reversed = lastnum;
while(num5 > 1)
{
num1 = (original / 10) - ((original / 100) * 10);
reversed = (reversed * 10) + num1;
num5 = num5 - 1;
}
printf("The reverse of %d is: %d\n", original, reversed);
}I'm having trouble with the loop. I'm not doing it for any specific amount of digits, so I can't do this
while(num5 > 1)
{
num1 = (original / 10) - ((original / 100) * 10);
reversed = (reversed * 10) + num1;
num1 = (original /100) - ((original /1000) * 100);
reversed = (reversed * 100) + num1;
//etc..............
num5 = num5 - 1;
}I can't figure out how to make it so that it'll work for any amount of digits….