-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++ Loop while loop average.cpp
More file actions
44 lines (33 loc) · 1.2 KB
/
C++ Loop while loop average.cpp
File metadata and controls
44 lines (33 loc) · 1.2 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
/**
[AUTHOR]: Saddam Arbaa
[Email] : <saddamarbaas@gmail.com>
while loop examples in C++
This example will print sum and average of odd numbers
between 1 and N and N will be entered by user
*/
#include <iostream>
using namespace std;
// the Driver Code
int main()
{
int N; // N variable
int sum = 0; // sum variable and give value 0
float average; // average variable
// asking user to enter variable n
cout << "Enter N: ";
cin >> N;
int i = 1; //counter
int number_been_loop = 0; //variable to keep trace of number of even numbers
while(i <= N)
{
number_been_loop++; // keeping trake of how many number we between 0 to
sum = sum + i; // calculating the sum
i = i + 2; // increment counter
}
cout <<endl<< "the number of the dd number is :"<<number_been_loop <<endl;
average = (float)sum / number_been_loop; // calculating the average with type casting sum to float
// print sum and average
cout <<"the sum of odd numbers between 0 to "<<N <<" = "<<sum<< endl;
cout <<"the average of odd numbers between 0 to "<<N <<" = "<<average<< endl;
return 0;// signal to operating system everything works fine
}/** End of main function */