GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2009, 21:38
ericak ericak is offline
New Member
 
Join Date: Mar 2009
Posts: 6
ericak is on a distinguished road

While and For Loop Program


I have to create the exact same program, one using a for loop and the other using a while loop. The goal of the program is to input 10 numbers and then sum up the odd numbers and the even numbers, respectively. I think I have a good idea of what I'm supposed to do, but I'm stumped on how to sum up the odd and even numbers. It's probably something very simple, but i'm very new to C++. The 2 programs only output "The sum of the even numbers is: 10" (which it's not because I entered the numbers 1-10) and it is supposed to output the sum of the even and odd numbers.
While
CPP / C++ / C Code:
#include <iostream>
using namespace std;
int main()
{
int num;
int sumo = 0; //sum of odd numbers
int sume = 0; //sum of even numbers
int count = 0;
while (count <=9)
{
	cout << "Please enter 10 integer numbers: " << endl;
	cin >> num;
	count++;
}

if (num%2 == 0)
{
	sume += num;
	cout <<"The sum of the even numbers is: " << sume << endl;
	num += 2;
}
if (num%2 == 1) 
{
	sumo += num;
	cout << "The sum of the odd numbers is : " << sumo << endl;
	num += 2;
}	

return 0;
}

For
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()

{
int sume = 0;
int sumo = 0;
int num;
int counter;

for (counter = 1; counter <= 10; counter++)
{
	cout << "Please enter an integer number: " << endl;
	cin >> num;
}
	
	if (num % 2 == 0)
	{
		sume += num;
		cout << "The sum of the even numbers is: " << sume << endl;
	}
	if (num % 2 == 1)
	{
		sumo += num;
		cout << "The sum of the odd numbers is: " << sumo << endl;
	}

	
return 0;
}
  #2  
Old 12-Mar-2009, 01:13
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: While and For Loop Program


You just put the 10 number in array and do the checking.

CPP / C++ / C Code:
if (number[loop] % == 0 )
{
even+=number[loop];}

else {}
  #3  
Old 12-Mar-2009, 05:45
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: While and For Loop Program


Perhaps something like this (both combined into one program):


CPP / C++ / C Code:
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int numbers[10]      = {0};
    int sum_odd_numbers  = 0;
    int sum_even_numbers = 0;

    size_t count         = 0;
    
    while(count < sizeof(numbers)/sizeof(int))
    {
	cout << "Enter an integer: " << endl;
	cin >> numbers[count];
	if(numbers[count] % 2 == 0)
	{
	    sum_even_numbers += numbers[count];
	}
	else
	{
	    sum_odd_numbers  += numbers[count];
	}
	++count;
    }
    cout << "The sum of odd  numbers is " << sum_odd_numbers  << endl;
    cout << "The sum of even numbers is " << sum_even_numbers << endl;

    memset(numbers, 0, sizeof(numbers));
    sum_odd_numbers  = 0;
    sum_even_numbers = 0;

    for(count = 0; count < sizeof(numbers)/sizeof(int); count++)
    {
	cout << "Enter an integer: " << endl;
	cin >> numbers[count];
	if(numbers[count] % 2 == 0)
	{
	    sum_even_numbers += numbers[count];
	}
	else
	{
	    sum_odd_numbers  += numbers[count];
	}
    }
    cout << "The sum of odd  numbers is " << sum_odd_numbers  << endl;
    cout << "The sum of even numbers is " << sum_even_numbers << endl;

    return 0;
}



MxB
  #4  
Old 12-Mar-2009, 11:24
ericak ericak is offline
New Member
 
Join Date: Mar 2009
Posts: 6
ericak is on a distinguished road

Re: While and For Loop Program


Thank you! I see that I almost had it correct, I just had the if/else statements in the wrong place.
Thanks again, you were a lot of help.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.