-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++ Loop for loop even.cpp
More file actions
43 lines (31 loc) · 1.17 KB
/
C++ Loop for loop even.cpp
File metadata and controls
43 lines (31 loc) · 1.17 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
/**
[AUTHOR]: Saddam Arbaa
[Email] : <saddamarbaas@gmail.com>
for loop examples in C++
This example will print sum and average of even numbers
between 0 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 number_been_loop = 0; //variable to keep trace of number of even numbers
for(int i = 0; i <= N; i = i + 2)
{
number_been_loop++; // keeping trake of how many number we between 0 to
sum = sum + i; // calculating the sum
}
cout <<endl<< "the number of the even 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 even numbers between 0 to "<<N <<" = "<<sum<< endl;
cout <<"the average of even numbers between 0 to "<<N <<" = "<<average<< endl;
return 0;// signal to operating system everything works fine
}/** End of main function */