mirror of
https://github.com/federico-busato/Modern-CPP-Programming.git
synced 2025-04-20 00:19:03 +03:00
loops1
This commit is contained in:
@ -14,5 +14,6 @@ int main(){
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
32
C++All_Programs/loopsQuestions/PrintNumbersfrom1toN.cpp
Normal file
32
C++All_Programs/loopsQuestions/PrintNumbersfrom1toN.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cout << "Enter the value of n : ";
|
||||
|
||||
cin >> n;
|
||||
// using for loop
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
cout << "\n " << i;
|
||||
}
|
||||
// using while loop
|
||||
int i = 1;
|
||||
cout<<endl;
|
||||
while (i <= n)
|
||||
{
|
||||
cout << i << endl;
|
||||
i++;
|
||||
}
|
||||
|
||||
// using do while loop
|
||||
i = 1;
|
||||
do
|
||||
{
|
||||
cout << i << endl;
|
||||
i++;
|
||||
} while (i <= n);
|
||||
|
||||
return 0;
|
||||
}
|
16
C++All_Programs/loopsQuestions/SumOfNnaturalnumbers.cpp
Normal file
16
C++All_Programs/loopsQuestions/SumOfNnaturalnumbers.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cout << "Enter the value of n : ";
|
||||
|
||||
cin >> n;
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
cout << "\n The sum of first " << n << " natural numbers is ---> " << sum << endl;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user