-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule4Week1-FunctionsforOddandEven.cpp
More file actions
174 lines (136 loc) · 4.99 KB
/
Copy pathModule4Week1-FunctionsforOddandEven.cpp
File metadata and controls
174 lines (136 loc) · 4.99 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//************************************************************************
// Author: Rolando Carreon
// Date: mar 21 2025
// Language: C++
// Assignment: Module4LabWeek1- Functions for Odd and Even
// Description: ill write multiple functions that take 2 parameters an
// integers (less than 200 but greater than 100) and returns
// the number of odd, even, and the sum of the odd and even
// numbers in the range. And a program to test my function.
//************************************************************************
#include<iostream>
using namespace std;
//
// function prototypes
//
// prototypes for sum and count of all even numbers within selected range
void oddSum( int&, int&, int& );
void oddCount( int&, int&, int& );
// prototypes for sum and count of all odd numbers within selected range
void evenSum( int&, int&, int& );
void evenCount( int&, int&, int& );
int main()
{
// declare local variables
int lowRange;
int highRange;
int sum;
int count;
// display instructions and prompt user to enter low range number
cout << "Please enter 2 numbers between 100 and 200\n";
// if user enters number lower than 100 OR higher than 199, prompt again
while (lowRange < 100 || lowRange > 199)
{
cout << "Enter the first number (lowest) in the (100 - 199) range: ";
cin >> lowRange;
}
// prompt user to enter high range number
// if highRange number lower than lowRange OR higher than 200, prompt again
while (highRange < lowRange || highRange > 200)
{
cout << "Enter the first number (highest) in the (100 - 200) range: ";
cin >> highRange;
}
// call the oddSum function, passing sum by reference
oddSum(lowRange, highRange, sum);
// call the oddSum function, passing sum by reference
oddCount(lowRange, highRange, count);
// call the oddSum function, passing sum by reference
evenSum(lowRange, highRange, sum);
// call the oddSum function, passing sum by reference
evenCount(lowRange, highRange, count);
return 0;
}
//
// function definitions
//
// function to add all odd numbers within the selected range together
void oddSum( int& lowRange, int& highRange, int& sum )
{
// define local function variables
// copies lowRange to temp value and sets sum to 0
int next = lowRange;
sum = 0;
// checks if the temp value is odd, adds value to sum, increments to next number,
// loops untill temp value is larger than highRange value
while ( next <= highRange )
{
if ( next % 2 != 0) // if is odd
{
sum += next;
}
next++;
} // end of while
cout << "\nSum of all odd numbers in the range " << lowRange << " to "
<< highRange << " is " << sum << endl;
} // end of function
// function to count all times a odd number is within the selected range
void oddCount( int& lowRange, int& highRange, int& count )
{
// define local function variables
// copies lowRange to temp value and sets count to 0
int next = lowRange;
count = 0;
// checks if the temp value is odd, increments the count by 1,
// increments to next numbers, loops untill temp is larger than highRange
while ( next <= highRange )
{
if ( next % 2 != 0) // if is odd
{
count++;
}
next++;
} // end of while
cout << "Count of all odd numbers in the range of " << lowRange
<< " to " << highRange << " is " << count << endl;
} // end of function
// function to add all even numbers within the selectedrange
void evenSum( int& lowRange, int& highRange, int& sum )
{
// define local function variables
// copies lowRange to temp value and sets sum to 0
int next = lowRange;
sum = 0;
// checks if the temp value is even, adds value to sum, increments to next number,
// loops untill temp value is larger than highRange value
while ( next <= highRange )
{
if ( next % 2 == 0) // if is even
{
sum += next;
}
next++;
} // end of while
cout << "\nSum of all even numbers in the range " << lowRange << " to "
<< highRange << " is " << sum << endl;
} // end of function
// function to count all times a even number is within the selected range
void evenCount( int& lowRange, int& highRange, int& count )
{
// define local function variables
// copies lowRange to temp value and sets count to 0
int next = lowRange;
count = 0;
// checks if the temp value is even, increments the count by 1,
// increments to next numbers, loops untill temp is larger than highRange
while ( next <= highRange )
{
if ( next % 2 == 0) // if is even
{
count++;
}
next++;
} // end of while
cout << "Count of all even numbers in the range of " << lowRange
<< " to " << highRange << " is " << count << endl;
} // end of function