How to write a program for printing all odd numbers between 1 and 100 using while loop?
I know the logic but I am not able to put it into the while loop and get an output.
Please help. Thanks in advance.
Cheers
babyboy
How to write a program for printing all odd numbers between 1 and 100 using while loop?
I know the logic but I am not able to put it into the while loop and get an output.
Please help. Thanks in advance.
Cheers
babyboy
What have you tried? Can you say, print all the integers between 1 and 100 using a while loop or for loop?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That's good. Now think: at the moment you are printing an integer, and then going on to the next integer with n++. Change n++ such that it "skips" an integer.
Incidentally, use int main(), not void main(), and getchar(), not getch(). main() should then return 0 for a successful exit.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
if i use n=n+2 i am getting an output.
but isn't there any way to use the n%2 logic within this while loop.
Yes it is, but skipping the numbers in this case is faster...
If you insist on using n%2, you can use the loop that prints the first 100 integers and just before calling printf() check if n is odd like this...
Code:// Only prints n it's odd (if n modulo 2 is not zero) if (n%2) printf ("%d\t", n);
Thank you " laserlight" and " aLiNuSh" once again