How to write a program for...

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 ...

  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
    21,452
    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

  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
    21,452
    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

  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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21