Results 1 to 4 of 4

Thread: Loop counting odd and even numbers

  1. #1
    Join Date
    Mar 2008
    Posts
    672

    Loop counting odd and even numbers

    I want a way (a code in c or c ++) to display the result of given number,The best way to see if a given number is odd or even. Any recommendations and suggestions are appreciated.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Loop counting odd and even numbers

    Code:
    int main()
    {
    int in, evn=0, od=0;
    do
    {
    cout << "Please enter a number:"<< ' ';
    cin>> input;
    if ( (evn%2 == 0) && (in >= 0) )
      even++;
    else if (in >= 0)
      od++;
    }
    while (in>=0);
     
    cout << "You have entered" << ' '<< od << ' ' <<"odd numbers." << endl;
    cout << "And" << ' ' << evn << ' ' <<"even numbers." << endl;
    system("PAUSE");
    return 0;
    }

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Loop counting odd and even numbers

    Code:
    long oddno =1;
     long maxno;
     unsigned long lproduct;
     cout<<"enter the odd number";
     cin >> maxno;
     while( oddno<= maxno )
     {
           long tempno = oddno + 2;
           lpro = tempno *oddno;
           cout<<"the product of two numbers first number"<<oddno<<"second number"<<tempno<<"equals"<<lpro<<"\n";
           oddno = tempno;
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Loop counting odd and even numbers

    Use the AND operation.
    Code:
    for(int i=0;i<=100;i++)
    {
    if(i&1)
    {
    printf("Odd\n");
    }
    else
    {
    printf("Even\n");
    }
    }

Similar Threads

  1. How to change negative numbers to positive numbers in Excel
    By Shaina Na in forum Microsoft Project
    Replies: 3
    Last Post: 08-01-2012, 05:35 PM
  2. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  3. How to check Bingo game numbers in loop
    By Afznotermi in forum Software Development
    Replies: 3
    Last Post: 29-10-2009, 02:19 PM
  4. Help with sh script loop counting
    By Brake Fail in forum Software Development
    Replies: 3
    Last Post: 10-01-2009, 06:48 PM
  5. Counting numbers and deleting array
    By SalVatore in forum Software Development
    Replies: 2
    Last Post: 25-08-2008, 01:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,501,891.45092 seconds with 17 queries