Simple_CPP_Programs_Explained
Simple_CPP_Programs_Explained
Solution:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[10] = {5, 2, 9, 1, 5, 6, 3, 7, 8, 4};
sort(arr, arr + 10); // Ascending
cout << "Ascending: ";
for (int i : arr) cout << i << " ";
sort(arr, arr + 10, greater<int>()); // Descending
cout << "\nDescending: ";
for (int i : arr) cout << i << " ";
return 0;
}
Output:
Ascending: 1 2 3 4 5 5 6 7 8 9
Descending: 9 8 7 6 5 5 4 3 2 1
Explanation:
This program sorts an array in ascending order and then in descending order using
nested loops.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10] = {2, 4, 5, 6, 4, 7, 8, 5, 9, 2};
cout << "Duplicates: ";
for (int i = 0; i < 10; i++)
for (int j = i + 1; j < 10; j++)
if (arr[i] == arr[j]) cout << arr[i] << " ";
return 0;
}
Output:
Duplicates: 2 4 5
Explanation:
This program finds duplicate numbers by comparing each element with all the
elements after it.
Solution:
#include <iostream>
using namespace std;
int main() {
int h, m, s;
cout << "Enter time (HH MM SS): ";
cin >> h >> m >> s;
cout << "Middle value: " << (h + m + s) / 3;
return 0;
}
Output:
Middle value: (HH+MM+SS)/3
Explanation:
This program takes input for time in hours, minutes, and seconds.
It calculates the middle value by finding the average using the formula:
Middle Value = (Hours + Minutes + Seconds) / 3
Finally, it prints the middle value.
Example:
Input: 10 30 50
Calculation: (10 + 30 + 50) / 3 = 30
Output: Middle value: 30
Solution:
#include <iostream>
using namespace std;
int main() {
int roll[10], classNum[10];
string name[10], section[10];
for (int i = 0; i < 10; i++)
cin >> roll[i] >> name[i] >> classNum[i] >> section[i];
for (int i = 0; i < 10; i++)
cout << roll[i] << " " << name[i] << " " << classNum[i] << " " << section[i] << "\n";
return 0;
}
Output:
Roll No Name Class Section
1 John 10 A
2 Alice 12 B
...
Explanation:
This program takes student details and then displays them.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0}, key;
cout << "Enter number: ";
cin >> key;
for (int i = 0; i < 10; i++)
if (arr[i] == key) { cout << "Found at " << i + 1; return 0; }
cout << "Not found";
return 0;
}
Output:
Found at position 5 or Not found
Explanation:
This program searches for a number in the array and prints its position.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10] = {1, 45, 23, 89, 56, 90, 12, 77, 34, 99}, max = arr[0];
for (int i = 1; i < 10; i++)
if (arr[i] > max) max = arr[i];
cout << "Largest: " << max;
return 0;
}
Output:
Largest: 99
Explanation:
This program finds the largest number in an array using a simple loop.
Solution:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i <= 255; i++) cout << i << " = " << char(i) << "\n";
return 0;
}
Output:
0 = NULL
1 = SOH
...
255 = ÿ
Explanation:
This program loops from 0 to 255 and prints the ASCII characters.
Each number represents an ASCII character.