forked from Shubhamlmp/Programming-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45_SequenceOfNumber_Sum.cpp
More file actions
34 lines (34 loc) · 840 Bytes
/
45_SequenceOfNumber_Sum.cpp
File metadata and controls
34 lines (34 loc) · 840 Bytes
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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
while (true)
{
int n, m;
cin >> n >> m;
// as given in problem statement if either one of n or m <= 0 then stop taking input and print nothing.
if (n <= 0 || m <= 0)
break;
else
{
int sum = 0;
// just swapping n,m if n > m
// in this way we can make sure that n < m
if (n > m)
{
int tmp = n;
n = m;
m = tmp;
}
// calculating sum for numbers between n and m
for (int i = n; i <= m; i++)
{
cout << i << " ";
sum += i;
}
cout << "sum =" << sum << endl;
}
}
return 0;
}