-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat1.cpp
41 lines (33 loc) · 911 Bytes
/
chat1.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
#include <iostream>
int sumAlgorithmA(int n) {
int sum = (n * (n + 1)) / 2;
return sum;
}
int sumAlgorithmB(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
int sumAlgorithmC(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
sum += j;
}
}
return sum;
}
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
int resultA = sumAlgorithmA(n);
int resultB = sumAlgorithmB(n);
int resultC = sumAlgorithmC(n);
std::cout << "Sum of numbers from 1 to " << n << " using Algorithm A: " << resultA << std::endl;
std::cout << "Sum of numbers from 1 to " << n << " using Algorithm B: " << resultB << std::endl;
std::cout << "Sum of numbers from 1 to " << n << " using Algorithm C: " << resultC << std::endl;
return 0;
}