-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule3LabOption1LoopingThroughNumbers.cpp
More file actions
85 lines (71 loc) · 2.9 KB
/
Copy pathModule3LabOption1LoopingThroughNumbers.cpp
File metadata and controls
85 lines (71 loc) · 2.9 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//************************************************************************
// Author: Rolando Carreon
// Date: feb 28 2025
// Language: C++
// Assignment: Module 3 Lab - Option 1 - Looping Through Numbers
// Description: In this assignment, I will write a C++ program that
// prompts the user to input two integers with the constraint
// that firstNum must be less than secondNum.
//************************************************************************
#include <iostream>
using namespace std;
int main()
{
// declare local variables
int numOne;
int numTwo = 0;
int oddSum = 0; // the sum of all odd numbers
int evenSum = 0; // the sum of all even number
// prompts user for the first number
cout << "Enter the first number: ";
cin >> numOne;
int savedNumOne = numOne; // copies original value of numOne
// prompts user for a second number that must be larger else reprompts
// displays data when if statment requirements met
while ( numOne > numTwo )
{
cout << "Enter the second number: ";
cin >> numTwo;
// if numOne is less than numTwo calculate and display data
if ( numOne < numTwo )
{
cout << endl;
// displays only odd numbers between input numOne & numTwo
cout << "Odd numbers between " << numOne << " and "
<< numTwo << ": ";
for ( numOne = numOne; numOne <= numTwo; numOne++ )
{
if ( numOne % 2 != 0 )
{
cout << numOne << " "; // formatting
oddSum += numOne; // saves each odd num and adds it
}
}
numOne = savedNumOne; // reset numOne to orginal input value
// displays only even numbers between input numOne & numTwo
cout << "\nEven numbers between " << numOne << " and "
<< numTwo << ": ";
for ( numOne = numOne; numOne <= numTwo; numOne++ )
{
if ( numOne % 2 == 0 )
{
cout << numOne << " ";
evenSum += numOne; // saves each even num and adds it
}
}
numOne = savedNumOne; // reset to orginal input value again
break; // breaks out of the loop
} // end of if statment
else
{
cout << "Error: The first number must be less than the "
<< "second number. Please try again.\n";
}
} // end of while statment
// displays the sum of odd & even numbers seperatly
cout << "\n\nSum of the even numbers between " << numOne << " and "
<< numTwo << ": " << evenSum;
cout << "\nSum of the odd numbers between " << numOne << " and "
<< numTwo << ": " << oddSum << endl;
return 0;
}