
/************* Program: Numbers.cpp Author: Doug Mainwaring Date: October 26, 2008 Description: Write a program that uses loops to perform the following steps. Note that each part must use the specified looping technique. Prompt the user for two integer numbers: name them firstNum and secondNum (firstNum must be less than secondNum—tell the user this!). HINT - use % operator to detemine odd and even. · Part a. Output all odd numbers between firstNum and secondNum. Use a while loop. · Part b. Output the sum of all even numbers between firstNum and secondNum. Use a do while loop. · Part c. Output the numbers and their square between one and 10. Use a for loop. · Part d is optional—bonus points for this. Output the sum of the square of odd numbers between firstNum and secondNum. Use a loop of your choice. · Part e is optional- bonus points for this. Output all uppercase letters from A to Z, using a loop of your choice. You must create these letters by using a loop with a character variable. Refer to the ASCII Character Set on page 1002 of your text; it shows the alphabet and decimal equivalents. See sample data and output below. Please note in your code (with comments) as well as your output which part you are completing. New Concepts: string_cast, multiple loops Challenges: **************/ #include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; // Declare constants // Function Prototypes int main() { // Declare variables below here int number1 = 1, number2 = 65, firstNum, secondNum, oddNum, evenNum, setNum, resultB = 0, resultC = 0, oddSquare = 0, square1; char letter; // Initialization Section for real number output cout <<setiosflags(ios::fixed | ios::showpoint); cout <<setprecision(2); // Begin your "main processing" below here cout << "Welcome to the Number Functions Program\n"; cout << endl; cout << endl; cout << "Please input two whole numbers and press Enter\n"; cout << "(Note: The first number must be less than the second number)\n"; cin >> firstNum >> secondNum; cout << endl; setNum = firstNum % 2; if (setNum == 0) { oddNum = firstNum + 1; evenNum = firstNum; } else { oddNum = firstNum; evenNum = firstNum + 1; } //Begin Part a cout << "Part a - Odd integers between " << firstNum << " and " << secondNum << " are: "; while (oddNum < secondNum) { cout << oddNum << " "; oddNum = oddNum + 2; } cout << endl; cout << endl; //Begin Part b cout << "Part b - Sum of even integers between " << firstNum << " and " << secondNum << " = "; do { resultB = resultB + evenNum; evenNum = evenNum +2; } while (evenNum <= secondNum); cout << resultB; cout << endl; cout << endl; //Begin Part c cout << "Part c:\n"; cout << endl; cout << "Number Square\n"; for (int number1 = 1; number1 <= 10; number1++) { square1 = number1 * number1; cout << " " << number1 << " " << square1 << endl; } cout << endl; //Begin Part d cout << "Part d - Sum of the squares of odd integers between " << firstNum << " and " << secondNum << " = "; while (oddNum <= secondNum) { oddSquare = oddNum * oddNum; resultC = resultC + oddSquare; oddNum = oddNum + 2; } cout << resultC; cout << endl; cout << endl; return 0; }
Thank you in advance for any assistance!