amazingme001
07-25-2005, 04:59 AM
I have been trying to figure it out for the past 3 hours, but it just seems not to work. It prints all the odd numbers as it is asked in the first part. Then it can not add all the even numbers and display the sum. I know something is wrong with my 2nd while loop, but can'f figure it out!!!
Please help!! :confused:
//*************************************************************
// The following program uses while loops to perform
// several steps. It prompts user to enter two numbers,
//it ouputs the odd numbers between those numbers, outputs
// the sum of all even numbers found between the two numbers
// and finally it outputs all uppercase letters.
//*************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
#include "PROG05.h"
using namespace std;
int main()
{
// initialize variables
int firstNum, secondNum;
int sum;
//sum = 0;
// get input from the user
cout << "Please enter two numbers, first no. must be smaller than the second: ";
// read (input)values into variables
cin >> firstNum >> secondNum;
// show what the input is
cout << "You have entered " << firstNum << " and " << secondNum << ".";
cout << endl << endl << endl;
if((firstNum % 2) == 0) firstNum++;
while(firstNum < secondNum)
{
cout << firstNum << " ";
firstNum += 2;
}
cout << endl << endl << endl;
// find the sum of all even numbers
//if (( firstNum % 2) != 0 ); //firstNum++;
//while (firstNum < secondNum)
sum = 0;
while((firstNum % 2 )!=0) firstNum++;
{
sum = sum + firstNum;
cout << sum << endl;
}
//sum = sum + firstNum;
cout << endl << endl << endl;
// << endl << endl << endl;
//system ("pause");
return 0;
}
i have commented out some parts that I was trying to figure out, that's why there are few things repeated
Please help!! :confused:
//*************************************************************
// The following program uses while loops to perform
// several steps. It prompts user to enter two numbers,
//it ouputs the odd numbers between those numbers, outputs
// the sum of all even numbers found between the two numbers
// and finally it outputs all uppercase letters.
//*************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
#include "PROG05.h"
using namespace std;
int main()
{
// initialize variables
int firstNum, secondNum;
int sum;
//sum = 0;
// get input from the user
cout << "Please enter two numbers, first no. must be smaller than the second: ";
// read (input)values into variables
cin >> firstNum >> secondNum;
// show what the input is
cout << "You have entered " << firstNum << " and " << secondNum << ".";
cout << endl << endl << endl;
if((firstNum % 2) == 0) firstNum++;
while(firstNum < secondNum)
{
cout << firstNum << " ";
firstNum += 2;
}
cout << endl << endl << endl;
// find the sum of all even numbers
//if (( firstNum % 2) != 0 ); //firstNum++;
//while (firstNum < secondNum)
sum = 0;
while((firstNum % 2 )!=0) firstNum++;
{
sum = sum + firstNum;
cout << sum << endl;
}
//sum = sum + firstNum;
cout << endl << endl << endl;
// << endl << endl << endl;
//system ("pause");
return 0;
}
i have commented out some parts that I was trying to figure out, that's why there are few things repeated