1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <conio.h> //use of _getch();
#include <math.h> //Use of fmod
using namespace std;
void user_input(double &fNum, double &sNum); //Function prototype
void computation(double comp1, double comp2); //Function prototype
int main() {
double first, second;
user_input(first, second); //Function call
computation(first, second); //Function call
_getch();
return 0;
}
void user_input(double &fNum, double &sNum) { //Function grabs user input, then checks for valid input
cout << "Input two numbers, your first number must be larger than" << endl;
cout << "your second number. The program will then output all odd" << endl;
cout << "numbers between the two, and the sum of all even between the two" << endl;
cout << "What is your first number?" << endl;
cin >> fNum;
cin.ignore(256, '\n');
cout << "What is your second number?" << endl;
cin >> sNum;
cin.ignore(256, '\n');
while (fNum >= sNum) { //Check user input
cin.clear();
cout << "Invalid input, first number must be less than second" << endl;
cout << "What is your first number?" << endl;
cin >> fNum;
cin.ignore(256, '\n');
cout << "What is your second number?" << endl;
cin >> sNum;
cin.ignore(256, '\n');
}
}
void computation(double comp1, double comp2) { //Checks for odd numbers between user input, display sum of even numbers
double sum = 0;
double x = comp1;
while (comp1 < comp2) {
if (fmod(comp1, 2) != 0) { //Fmod used for checking remainders
cout << comp1 << " is an odd number" << endl;
}
if (fmod(comp1, 2) == 0 && x != comp1){
sum += comp1;
}
comp1++; //Incrememnt comp1 until check fails
}
cout << "The sum is of all even numbers between your numbers is: " << sum << endl;
}
| |