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
This is a discussion on How to write a program for... within the C Programming forums, part of the General Programming Boards category; How to write a program for printing all odd numbers between 1 and 100 using while loop ? I know ...
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?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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![]()