-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.06.Summation_of_odd_numbers.cpp
72 lines (52 loc) · 1.65 KB
/
01.06.Summation_of_odd_numbers.cpp
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
#include <iostream>
using namespace std;
/* ============================
1.6. Summation of Odd Number
by : Putra
-------------------------------
Problem:
Write a program that displays the sum of the first ten odd numbers
(No Looping)
*/
int main() {
// 1st odd number
int n1 = 1;
int U1 = 2 * n1 + 1;
// 2nd odd number
int n2 = 2;
int U2 = 2 * n2 + 1;
// 3rd odd number
int n3 = 3;
int U3 = 2 * n3 + 1;
// 4th odd number
int n4 = 4;
int U4 = 2 * n4 + 1;
// 5th odd number
int n5 = 5;
int U5 = 2 * n5 + 1;
// 6th odd number
int n6 = 6;
int U6 = 2 * n6 + 1;
// 7th odd number
int n7 = 7;
int U7 = 2 * n7 + 1;
// 8th odd number
int n8 = 8;
int U8 = 2 * n8 + 1;
// 9th odd number
int n9 = 9;
int U9 = 2 * n9 + 1;
// 10th odd number
int n10 = 10;
int U10 = 2 * n10 + 1;
// The sum of the first ten odd numbers
int S10 = U1 + U2 + U3 + U4 + U5 + U6 + U7 + U8 + U9 + U10;
cout << "The sum of the first ten odd numbers is " << endl;
cout << U1 << " + " << U2 << " + " << U3 << " + " << U4 << " + " << U5 << " + " << U6 << " + " << U7 << " + " << U8 << " + " << U9 << " + " << U10 << " = " << S10 << "." << endl;
return 0;
}
/*
Source :
Liang. 2014. Introduction to Programming with C++ 3rd Edition. London : Pearson Education.
https://www.pearson.com/en-us/subject-catalog/p/Liang-Companion-Website-for-Introduction-to-Programming-with-C-Access-to-Videonotes-3rd-Edition/P200000003422/9780133380262
*/