i have a program that has one little glitch but i dont know how to fix it. If you can help it would be great.


[/b] 
int main()
{
int a, b, c;
b=0;
c=0;
do
{
cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
cin>> a;
if (a%2 == 0)
{
c++;
}
else
b++;
}
while (a>=0);
cout << "You have entered" << ' '<< b << ' ' <<"odd numbers." << endl;
cout << "And" << ' ' << c << ' ' <<"even numbers." << endl;
system("PAUSE");
return 0;
}
 [b]


here is the program. the problem is if i put in these numbers 3,1037,60,-43. i get a answer of 3 odd numbers and 1 even numbers. it should say 2 odd and 1 even. the program is counting the negative number as an odd where it should just end the program.

It's a good start. Let me just tell you, the first thing I noticed was you have variables a, b, and c. But what are they? What do they represent? Variable names should always be indicative of what they do. Also, comments would help ;)

Here is a start ...

int main()
{
int input, even=0, odd=0;
do
{
cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
cin>> input;
if ( (even%2 == 0) && (input >= 0) )
  even++;
else if (input >= 0)
  odd++;
}
while (input>=0);

cout << "You have entered" << ' '<< odd << ' ' <<"odd numbers." << endl;
cout << "And" << ' ' << even << ' ' <<"even numbers." << endl;
system("PAUSE");
return 0;
}

Note that my if statement has a conditional to make sure not to include the negative number. This is the easiest quick fix considering you're using a do while loop like this. You could also just use a nested if statement.

Another way to do it is to prompt the user to enter a first number. And then from then on use a while loop instead of a do while loop, where the prompt to enter a number is on the last line of the loop.

thanks for the help, I really appreciate it. I have been really struggling with this language. I look forward to being a part of your community.

Hey there. That's great. Welcome aboard.

can you help me make a code on arrays in finding even and odd numbers

Hi avaughn, welcome to DaniWeb!

If you look at the post by cscgal above, you will see all of the elements that you need. The "if number%2 == 0" test is how to tell if a number is even or odd. Then you just have to keep track of which ones are which! Please give it a try and post some code and we can help if you get stuck.

David

please help me how to determine odd and even number and how can i count all the odd or even numbers?

i need some help about the bubble sort. i dont knw how to start.

@mcroneil - Please show us your attempt and we can help you debug it.

@rheg - You should start a new thread when you have a new question.

Okay so I'm taking a computer science class and I have no idea what I'm doing! The hmwk is to design a program that iterates through integers from 1 to a value determined by user and add the odd numbers in the range and the even numbers in the range and output the sum for both. I am clueless as to how to get the program to add only the odds together and only the evens together... please help. This is what I have thus far...

#include <iostream>
using namespace std;
void main(void)
{
    int number, max, increment;

    cout << "Enter the max value: ";
    cin >> max;
    cout << "Enter the increment: ";
    cin >> increment;

    for(number = 1; number <= max; number = number + increment)
    {
        cout << number << endl; 

        if((number%2) == 0)
        {
            cout << "This number is even." << endl;
        }
        else if((number%2) == 1)
        {
            cout << "This number is odd." << endl;
        }
        else
        {
            cout << "Error." << endl;
        }
    }
}

Hi clueless215 - welcome to DaniWeb. Here are a few guidelines which will help you get the most out of the forum.

1) When you have a new question, start a new thread rather than post at the end of someone else. What you have done here is called "thread hijacking" and is frowned upon.

2) Use code tags. That is, surround your code with [ code ] your code [ /code ] (without the spaces inside the brackets. It makes it much easier to read.

3) Don't use abbreviations like 'hmwk'.

As to your question - you have done the hard part already! (Identifying that "%2 = 0" determines if the number is even.) Now you just have to make two sum variables (sumEven and sumOdd) and use them to store the count along the way (sumEven += number).

Good luck,

David

idk why my program is not working, i wanted to create a program that counts even and odd number when a user inputs number except 0. Zero is the terminator of the loop.

#include <stdio.h>

int main(){

    int entNum, ans;
    int even;
    int odd;

    do{
        even=0;
        odd=0;
        scanf("%d",&entNum);
        ans = entNum%2;

    if(ans==0){
        even++;
    }

    else if(ans!=0) {
        odd++;

    }   
    }while(entNum!=0);

    printf("Even: %d",even);
    printf("Odd: %d",odd);

    getchar();
}
Write a Python program to take input of a positive number, say N, with an appropriate prompt, from the user. The user should be prompted again to enter the number until the user enters a positive number. Find the sum of first N odd numbers and first N even numbers. Display both the sums with appropriate titles.  
n = int(input("enter n  no.... : "))
sumOdd =0
sumEven = 0
for i in range (n) : 
    no = int(input("enter a positive number : "))
    if no > 0 :
        if no % 2 == 0 :
            sumEven = sumEven + no
        else :
            sumOdd = sumOdd + no
    else :
        print("exit.....")
        break
print ("sum odd ==  ",sumOdd)
print ("sum even ==  ",sumEven)
commented: 17 years late and in the wrong language. OP asked for C++. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.