This is related to the ranges to integers code I have used in several of your examples.
C doesn't implement a separate true/false (a.k.a. Boolean) data type. It uses integers for that. 0 is false and any other value is true.
So
is short hand for and completely equivalent to
There are MANY ways to skin this cat. The temporary variable "Result" pianojoe used is one way. Below is another based on that code that gives the exact same output without it.
Code:
#include <stdio.h>
int main (void) {
int Input;
printf ("Enter a number: ") ;
scanf ("%d", &Input) ;
printf("Result: ");
while(Input) {
printf("%d", (Input % 10));
Input = Input / 10;
}
printf("\n");
return 0;
}
Just for fun, I'll develop the multi loop version that was my first thought.
EDIT: Here's another way to do it that presumes knowing how many digits are in the number is useful as I originally assumed. Sometimes your assumptions need to be revisited/thrown out.
Code:
#include <stdio.h>
int main (void) {
int input,copy,reverse = 0;
int done = 0, digits = 0, place = 1, newplace = 1;
int count;
int digit;
printf ("Enter a number: ") ;
scanf ("%d", &input) ;
if (input>0) {
copy = input;
// Figure out how many digits in the input number and build max placeholder
while(copy) {
copy /= 10;
digits++;
place *= 10;
}
place /= 10;
// Reverse the input pulling number off from most-significant to least
copy = input;
for (count = 1; count <= digits ; count++) {
digit = (copy/place);
reverse += digit*newplace;
copy -= digit*place;
newplace *= 10;
place /= 10;
}
printf ("Reversed number: %d\n", reverse);
}
else
printf ("ERROR: Please enter a positive integer\n");
return 0;
}
B
Thanks for your input balamw. You did that 2nd one "just for fun?" That one is a bit too complicated for me at this point. However, your other one is probably closer to what I could come up with if I could come up with it. Plainjoe is next. I studied his code during lunch. I'm trying to go through the steps again of what I was trying to accomplish, and see if I can see where it would lead to his/your code.
I forgot to think of the while (as true or false). I was just thinking of it simply on a conditional basis, while (this =that),or while this value > that value) In other words, thinking of it as it reads. If x is bigger than y, do stuff. I wasn't thinking in terms of 0,1 false and true. I didn't know that a number was equal and could be used in place of != 0. I've written != 0 before, I'm going to go back and look at that old code to see what number I would use in place of != 0.
I'm going to write some notes out, then post to see if they lead me in the right direction.
Okay, so here goes.
What am I trying to do?
1) I want to take out the last digit of userNumber with %
2) Then I want to divide userNumber /10 and return the new number to the loop.
3) Then I want to take out the last digit of userNumber with %
4) Then I want to divide userNumber /10 and return the new number to the loop.
I want to continue this pattern until the userNumber is no longer > 0.
User enters 348
I do 348 % 10 takes out the 8 <--------------------8
then I do 348 / 10 removes the 8, return 34 to loop.
I do 34 % 10 takes out the 4 <--------------------4
then I do 34 / 10 removes the 4, return 3 to the loop.
I do 3 % 10 takes out the 3 <--------------------3
then I do 3 / 10 values is less than 0, loop terminates, I have my 843 above.
Am I exactly right in this evaluation? Is this exactly what I should have come up with last night? At this point, would I possibly write some pseudo code?
Let me know, I want to go through the correct steps, make sure I'm on track.
FYI, one of the reasons I struggled with this idea last night is because I thought that once I did 348 % 10, that taking out that 8 was the same thing as removing it. So the way I thought last night was that if I did....
348 % 10 takes out the 8, sends only 34 to be divided by 10 in the next line
34 / 10 leaves me with only 3 (doesn't work)
Then last night I thought about doing the
348 % 10 before the loop started, it will get the 8 out for me
then put 34 in the loop to be remm'ed by 10, so 34 % 10, gets the 4 out for me
then divide 34/10, leaves me with 3, send 3 back to the loop.
3 % 10 takes out the 3...........843. Would that have worked too?
rough example, something like.....I don't know, just curious here....
(userNumber % 10) //this takes out the first number I need then sends it on..
(userNumber / 10 ) // sends the new number into the loop
while (userNumber != 0)
userNumber % 10 //takes out last number again
userNumber / 10 //sends the "new" number back up into the loop.
Lastly, I've almost forgotten about the book, but it did ask me to use a do loop to complete this exercise. Do balamw or plainjoe codes qualify as "do" loops? I only see a while in there.