-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumNaturalNumb.cpp
More file actions
46 lines (40 loc) · 870 Bytes
/
SumNaturalNumb.cpp
File metadata and controls
46 lines (40 loc) · 870 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
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;
// To Print the Sum of First n NAtural Numbers
int main(){
/*Using For Loop*/
// int n ;
// int sum = 0 ;
// cout<<"Enter the No : ";
// cin>>n;
// for(int i = 0 ; i <= n ; i++){
// sum += i;
// }
// cout<<"The Sum of n Natural No is "<<sum<<endl;
// Using While Loop
// int n ;
// int sum = 0 , i = 0 ;
// cout<<"Enter the No : ";
// cin>>n;
// while(i <= n){
// sum += i ;
// i++;
// }
// cout<<"The Sum of n Natural No is "<<sum<<endl;
// Using Do While Loop
int n ;
int sum = 0 , i = 0 ;
cout<<"Enter the No : ";
cin>>n;
do{
sum += i;
i++;
}while(i <= n);
cout<<"The Sum of n Natural No is "<<sum<<endl;
return 0;
}
/*
Output :
Enter the No : 8
The Sum of n Natural No is 36
*/