## Program 11: Hello World with User Input
This program prompts the user to enter the number of times to display
"Hello World".
```cpp
#include <iostream>
using namespace std;
int main() {
unsigned n;
cout << "How many times will I output the sentence? ";
cin >> n;
for(unsigned i = 0; i < n; ++i)
cout << "Hello, World!" << endl;
return 0;
}
```
**Input:**
`5`
**Output:**
``` Hello,
World! Hello,
World! Hello,
World!
1
Hello, World!
Hello, World!
```
## Program 12: Check if a Number is Even or Odd
This program checks whether the input number is even or odd.
```cpp
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if(a % 2 == 0)
cout << "even";
else
cout << "odd";
return 0;
}
```
**Input:** `8`
**Output:**
```
2
even
```
## Program 13: Swap Two Numbers
This program swaps the values of two numbers using a temporary
variable.
```cpp
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << a << " " << b << endl;
int temp = a;
a=b;
b = temp;
cout << a << " " << b << endl;
return 0;
}
```
**Output:**
```
1020
3
2010
```
## Program 14: Find the Largest Number Among Three Numbers This
program finds the largest number among three input numbers.
```cpp
#include <iostream>
using namespace std;
int main() {
float a, b, c;
cin >> a >> b >> c;
if(a >= b && a >= c)
cout << "Largest number: " << a;
else if(b >= a && b >= c)
cout << "Largest number: " << b;
else
cout << "Largest number: " << c;
return 0;
}
```
**Input:** `1 2 3`
**Output:**
4
```
Largest number: 3
```
## Program 15: Calculate the Sum of Natural Numbers from 1 to n
This program calculates the sum of all natural numbers from 1 to the input
number `n`.
```cpp
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cin >> n;
for(int i = 1; i <= n; ++i)
sum += i;
cout << "Sum: " << sum << endl;
return 0;
}
```
**Input:** `5`
**Output:**
```
5
Sum: 15
```
## Program 16: Calculate the Average of Array Elements
This program calculates the average of elements in an array.
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter array elements: ";
for(int i = 0; i < n; ++i)
cin >> arr[i];
float sum = 0;
for(int i = 0; i < n; ++i)
sum += arr[i];
cout << "Average: " << sum / n << endl;
return 0;
}
```
6
**Input:**
```
Enter the size of the array: 3
Enter array elements: 1 2 3
```
**Output:**
```
Average: 2
```
## Program 17: Find the GCD of Two Numbers
This program calculates the Greatest Common Divisor (GCD) of two input
numbers.
```cpp
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if(b == 0)
return a;
return gcd(b, a % b);
}
7
int main() {
int a, b;
cin >> a >> b;
cout << gcd(a, b) << endl;
return 0;
}
```
**Input:** `35 25`
**Output:**
```
5
```
## Program 18: Calculate the Power of a Number
This program calculates the power of a number using a loop.
```cpp
#include <iostream>
using namespace std;
int main() {
float base;
int power;
cin >> base >> power;
8
float result = 1;
while(power != 0) {
result *= base;
power--;
}
cout << result << endl;
return 0;
}
```
**Input:** `3 3`
**Output:**
```
27
```
## Program 19: Find the Length of a String
This program finds the length of an input string.
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
9
string str;
cin >> str;
int count = 0;
for(int i = 0; str[i]; ++i)
count++;
cout << count << endl;
return 0;
}
```
**Input:** `abcde`
**Output:**
```
5
```
## Program 20: Input and Display Multiple Variables
This program takes multiple inputs from the user and displays them.
```cpp
#include <iostream>
using namespace std;
int main() {
char ch;
10
int num; cout << "Enter a character and an
integer: "; cin >> ch >> num; cout <<
"Character: " << ch << endl; cout << "Number:
" << num << endl; return 0;
}
```
**Input:**
```
Enter a character and an integer: F 23
```
**Output:**
```
Character: F
Number: 23
```
11