Q1: inverted pyramid:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
for(int i = n; i >= 1; i--) { // Rows
// Print leading spaces
for(int space = 0; space < n - i; space++) {
cout << " "; // 2 spaces for alignment
}
// Print numbers
for(int j = i; j >= 1; j--) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Q2: even number count
#include <iostream>
using namespace std;
int main() {
int arr[10];
int count = 0;
cout << "Enter 10 integers: ";
// Taking input
for(int i = 0; i < 10; i++) {
cin >> arr[i];
}
cout << "\nEven numbers are: ";
// Checking even numbers
for(int i = 0; i < 10; i++) {
if(arr[i] % 2 == 0) {
cout << arr[i] << " ";
count++;
}
}
cout << "\nTotal Even Numbers: " << count << endl;
return 0;
}
Q3: input positive number
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a positive number: ";
cin >> num;
while(num <= 0) {
cout << "Invalid input! Please enter a positive number: ";
cin >> num;
}
cout << "You entered: " << num << endl;
return 0;
}
Q4: find target in array
#include <iostream>
using namespace std;
int main() {
int arr[10];
int target;
bool found = false;
cout << "Enter 10 elements:\n";
for(int i = 0; i < 10; i++) {
cin >> arr[i];
}
cout << "Enter target number to search: ";
cin >> target;
for(int i = 0; i < 10; i++) {
if(arr[i] == target) {
cout << "Target found at index: " << i << endl;
found = true;
break;
}
}
if(!found) {
cout << "Target not found in array." << endl;
}
return 0;
}
Q5: min and max in array
#include <iostream>
using namespace std;
int main() {
int arr[10];
int max, min;
int sum = 0;
double average;
// 1⃣ Input values into array
cout << "Enter 10 integers:\n";
for(int i = 0; i < 10; i++) {
cin >> arr[i];
}
// Assume first element is max and min initially
max = arr[0];
min = arr[0];
// 2⃣ Find maximum, minimum and calculate sum
for(int i = 0; i < 10; i++) {
if(arr[i] > max) {
max = arr[i];
}
if(arr[i] < min) {
min = arr[i];
}
sum += arr[i];
}
// 3⃣ Calculate average
average = sum / 10.0;
// Output results
cout << "Maximum value: " << max << endl;
cout << "Minimum value: " << min << endl;
cout << "Average value: " << average << endl;
return 0;
}
Q6: gate keeper
#include <iostream>
using namespace std;
int main() {
int password, index;
int arr[5] = {10, 20, 30, 40, 50};
do {
cout << "Password dalo: ";
cin >> password;
cout << "Index dalo (0-4): ";
cin >> index;
if (password == 1234 && index >= 0 && index <= 4) {
cout << "Access Mil Gaya!\n";
cout << "Value: " << arr[index] << endl;
break; // loop band
}
cout << "Galat input! Dobara try karo.\n\n";
} while (true);
return 0;
}
Q7: find peak element
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n]; // array of size n
cout << "Enter " << n << " elements:\n";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Peak elements are: ";
for(int i = 0; i < n; i++) {
// First element
if(i == 0 && arr[i] >= arr[i+1]) {
cout << arr[i] << " ";
}
// Last element
else if(i == n-1 && arr[i] >= arr[i-1]) {
cout << arr[i] << " ";
}
// Middle elements
else if(i > 0 && i < n-1 && arr[i] >= arr[i-1] && arr[i] >= arr[i+1]) {
cout << arr[i] << " ";
}
}
cout << endl;
return 0;
}
Q8: PIN code
#include <iostream>
using namespace std;
int main() {
int pin;
const int SECRET_PIN = 1234; // define the correct PIN
do {
cout << "Enter the Secret PIN: ";
cin >> pin;
if(pin != SECRET_PIN) {
cout << "Incorrect PIN! Try again.\n\n";
}
} while(pin != SECRET_PIN); // loop continues until correct PIN
cout << "PIN accepted! Access granted." << endl;
return 0;
}
Q9: table
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
cout << "Multiplication table for " << num << ":\n";
for(int i = 1; i <= 12; i++) {
cout << num << " x " << i << " = " << num * i << endl;
}
return 0;
}
Q10: factorial calculator
#include <iostream>
using namespace std;
int main() {
int n;
unsigned long long factorial = 1; // Use long long for big numbers
cout << "Enter a positive integer: ";
cin >> n;
if(n < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
return 0;
}
for(int i = 1; i <= n; i++) {
factorial *= i; // multiply total by current i
}
cout << n << "! = " << factorial << endl;
return 0;
}
Q11: pattern builder
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; i++) { // loop for each row
for(int j = 1; j <= i; j++) { // loop for printing stars
cout << "*";
}
cout << endl; // move to next row
}
return 0;
}
Q12: simple calculator
#include <iostream>
using namespace std;
int main() {
double num1, num2, result;
char op;
// Take input
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
switch(op) {
case '+':
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case '-':
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case '*':
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case '/':
if(num2 == 0) {
cout << "Error: Division by zero is not allowed!" << endl;
} else {
result = num1 / num2;
cout << "Result: " << result << endl;
}
break;
default:
cout << "Error: Invalid operator!" << endl;
}
return 0;
}
Q13: grade calculator
#include <iostream>
using namespace std;
int main() {
char grade;
cout << "Enter your grade (A, B, C, D, F): ";
cin >> grade;
switch(grade) {
case 'A':
case 'a':
cout << "Excellent work!" << endl;
break;
case 'B':
case 'b':
cout << "Well done." << endl;
break;
case 'C':
case 'c':
cout << "You passed." << endl;
break;
case 'D':
case 'd':
cout << "Need improvement." << endl;
break;
case 'F':
case 'f':
cout << "Better luck next time." << endl;
break;
default:
cout << "Invalid grade entered!" << endl;
}
return 0;
}
Q14: inverted star pattern pyramid
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; i--) { // outer loop for rows
// print leading spaces
for(int j = 0; j < rows - i; j++) {
cout << " ";
}
// print stars
for(int k = 0; k < 2*i - 1; k++) {
cout << "*";
}
cout << endl; // move to next row
}
return 0;
}
Q15: erected pyramid numbers
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
for(int i = 1; i <= n; i++) { // Rows
// Print leading spaces
for(int space = 0; space < n - i; space++) {
cout << " "; // 2 spaces for alignment
}
// Print numbers from 1 to i
for(int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Q16: erected pyramid stars
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; i++) { // Rows
// Print leading spaces
for(int j = 1; j <= rows - i; j++) {
cout << " "; // single space
}
// Print stars
for(int k = 1; k <= 2*i - 1; k++) {
cout << "*";
}
cout << endl; // Move to next row
}
return 0;
}