Program will have user enter an integer between 1 and 100, anything else will cause the loop and program to quit. This integer is first tested to see if it is an odd or even number; here an appropriate message is displayed, displayed only in the main function. Then the sum of all the numbers up to and including the entered number is calculated and displayed. So if the user enters the number 4, the sum of numbers from 1 to 4 would yield 10. This program needs to call two separate functions, one to determine if number is odd or even, and the other to calculate the sum of the numbers. Both functions will have one value or input parameter and both should return an “int” back, which is the answer the main will deal with.
#include <iostream>
#include <iomanip>
usingnamespace std;
void EvenOdd (int Integer);
void SumDec (int Integer, int Sum);
int main()
{
int Integer, Sum = 0;
do
{
cout << "Enter an integer that is between 1 and 100 --> ";
cin >> Integer;
if(Integer >= 1 && Integer <= 100)
EvenOdd(Integer);
SumDec (Integer, Sum);
}
while (Integer >= 1 && Integer <= 100);
}
void EvenOdd (int Integer)
{
if((Integer % 2) != 0)
cout << Integer << " is odd.";
if ((Integer % 2) == 0)
cout << Integer << " is even.";
}
void SumDec (int Integer, int Sum)
{
Sum = 0;
while ( Integer > 0)
{
Sum = Integer + Sum;
--Integer;
}
cout << "The sum of all the numbers up to and including " << Integer
<< " is " << Sum << endl;
}
i am using Xcode and am not sure why my program is not running all the way through. i think i should add another int so that integer is unchanged and make the new int change with the dec.
#include <iostream>
void EvenOdd (int Integer) {
if ((Integer % 2) != 0) {
std::cout << Integer << " is odd.\n";
} else {
std::cout << Integer << " is even.\n";
}
}
void SumDec(int Integer) {
int Sum = 0, OldInteger = Integer;
while (Integer > 0) {
Sum += Integer;
Integer--;
}
std::cout << "The sum of all the numbers up to and including " << OldInteger << " is " << Sum << std::endl;
}
int main(void) {
int Integer;
while (true) {
std::cout << "Enter an integer that is between 1 and 100 --> ";
std::cin >> Integer;
std::cin.ignore();
if (Integer >= 1 && Integer <= 100) {
EvenOdd(Integer);
SumDec(Integer);
return 0;
}
std::cout << "Try again.\n";
}
}