Thread: How to write a program for...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    29

    How to write a program for...

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried? Can you say, print all the integers between 1 and 100 using a while loop or for loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    Quote Originally Posted by laserlight View Post
    What have you tried? Can you say, print all the integers between 1 and 100 using a while loop or for loop?

    Yup I can print all integers between 1 and 100. here is the program for that.

    Code:
    void main()
    {
         
         int n=1; 
              
         while (n<=100)
               {
               
               printf ("%d\t",n);
               n++; 
                       
               }
               
         
         getch();
    }
    But now how do i print only odd nos now

    cheers
    babyboy

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    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.

  6. #6
    Registered User aLiNuSh's Avatar
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Posts
    7
    Quote Originally Posted by babyboy View Post
    if i use n=n+2 i am getting an output.

    but isn't there any way to use the n&#37;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);

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    Thank you " laserlight" and " aLiNuSh" once again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  2. how could i write this program: cat < apa | wc | wc > bepa
    By strugglingman in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 04:40 PM
  3. Is there another way to write this program?
    By agentxx04 in forum C Programming
    Replies: 1
    Last Post: 11-23-2004, 09:28 PM
  4. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM