Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Philip Fernandez
- // Lab01 Question 1
- // 8/21/2017
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- // Function declarations
- void getInput(int &, int &);
- void outputOdd(int, int);
- void outputSumOfEven(int, int);
- void outputSquare(int, int);
- void outputSumOfSquaresOfOdd(int, int);
- int main() {
- // Declare ints for two numbers to be input
- int firstNum, secondNum;
- // Function calls
- getInput(firstNum, secondNum);
- outputOdd(firstNum, secondNum);
- outputSumOfEven(firstNum, secondNum);
- outputSquare(firstNum, secondNum);
- outputSumOfSquaresOfOdd(firstNum, secondNum);
- return 0;
- }
- // getInput takes two arguments by reference.
- // This is so we can return values for firstNum and secondNum
- // this function also provides a do while loop to validate
- // that firstNum < secondNum
- void getInput(int &num1, int &num2) {
- cout << "Enter an integer: ";
- cin >> num1;
- do {
- cout << "\nEnter an integer that is greater than " << num1 << ": ";
- cin >> num2;
- } while(num1 >= num2);
- }
- // outputOdd takes two arguments.
- // this function displays all of the odd
- // integers between firstNum and secondNum inclusive
- void outputOdd(int num1, int num2) {
- cout << "\nAll of the odd integers between " <<
- num1 << " and " << num2 << ": ";
- for(int i = num1; i <= num2; i++) {
- if(i % 2 != 0) {
- cout << i << " ";
- }
- }
- cout << "\n";
- }
- // outputSumOfEven takes two arguments.
- // this function calculates and displays
- // the sum of all even numbers between
- // firstNum and secondNum inclusive
- void outputSumOfEven(int num1, int num2) {
- int sumOfEven = 0;
- for(int i = num1; i <= num2; i++) {
- if (i % 2 == 0) {
- sumOfEven += i;
- }
- }
- cout << "\nSum of all even integers between " <<
- num1 << " and " << num2 << ": " << sumOfEven << "\n";
- }
- // outputSquare takes two arguments
- // this function displays firstNum and every
- // consecutive integer after firstNum up to
- // 10 integers past firstNum. The integer
- // squared is also displayed.
- void outputSquare(int num1, int num2) {
- cout << "\n";
- for(int i = num1; (i <= num2) && (i < num1 + 10); i++) {
- cout << i << ": " << i*i << " ";
- }
- cout << "\n";
- }
- // outputSumOfSquaresOfOdd takes two arguments.
- // this functions calculates and displays
- // the sum of all of the odd numbers squared
- // between firstNum and secondNum inclusive
- void outputSumOfSquaresOfOdd(int num1, int num2) {
- int sumOfSquaresOfOdd = 0;
- for(int i = num1; i <= num2; i++) {
- if(i % 2 != 0) {
- sumOfSquaresOfOdd += (i*i);
- }
- }
- cout << "\nSum of the square of all odd integers " <<
- "between " << num1 << " and " << num2 << ": " << sumOfSquaresOfOdd << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement