Part 1.
1. Displaying n Numbers
a. Starting from zero:
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive number: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
Use code with caution.
b. Starting from one:
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
Use code with caution.
c. Starting from a number entered from the keyboard:
C++
#include <iostream>
using namespace std;
int main() {
int n, start;
cout << "Enter a positive number (n): ";
cin >> n;
cout << "Enter the starting number: ";
cin >> start;
for (int i = 0; i < n; i++) {
cout << start + i << " ";
}
cout << endl;
return 0;
}
Use code with caution.
2. Displaying n Numbers in Reversed Order
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive number: ";
cin >> n;
for (int i = n; i >= 1; i--) {
cout << i << " ";
}
cout << endl;
return 0;
}
Use code with caution.
3. Entering Numbers Until a Negative Number is Entered
C++
#include <iostream>
using namespace std;
int main() {
int num;
while (true) {
cout << "Enter a number: ";
cin >> num;
if (num < 0) {
cout << "Now you have entered a negative number" <<
endl;
break;
}
}
return 0;
}
Use code with caution.
4. Displaying Upper and Lowercase Alphabets
C++
#include <iostream>
using namespace std;
int main() {
for (char ch = 'A'; ch <= 'Z'; ch++) {
cout << ch << (char)(ch + 32) << " ";
}
cout << endl;
return 0;
}
Use code with caution.
5. Computing Sum, Difference, and Product of a Range of Numbers
C++
#include <iostream>
using namespace std;
int main() {
int start, end, sum = 0, difference, product = 1;
cout << "Enter the starting number: ";
cin >> start;
cout << "Enter the ending number: ";
cin >> end;
for (int i = start; i <= end; i++) {
sum += i;
product *= i;
}
difference = end - start;
cout << "Sum: " << sum << endl;
cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;
return 0;
}
6. Sum of Reciprocals
C++
#include <iostream>
using namespace std;
int main() {
int n;
double sum = 0;
cout << "Enter a positive integer n: ";
cin >> n;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
cout << "Sum of reciprocals: " << sum << endl;
return 0;
}
Use code with caution.
7. Square Root of Numbers
C++
#include <iostream>#include <cmath>
using namespace std;
int main() {
char choice;
double num;
do {
cout << "Enter a number: ";
cin >> num;
cout << "Square root: " << sqrt(num) << endl;
cout << "Do you want to continue (y/n)? ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;
}
Use code with caution.
8. Factorial Calculation
C++
#include <iostream>
using namespace std;
int main() {
int n, factorial = 1;
cout << "Enter a non-negative integer: ";
cin >> n;
if (n < 0) {
cout << "Factorial is not defined for negative numbers." <<
endl;
} else {
for (int i = 1; i <= n; i++) {
factorial *= i;
}
cout << "Factorial of " << n << " is " << factorial << endl;
}
return 0;
}
Use code with caution.
9. Counting Students with Marks > 20
C++
#include <iostream>
using namespace std;
int main() {
int n, count = 0;
cout << "Enter the number of students: ";
cin >> n;
for (int i = 1; i <= n; i++) {
int marks;
cout << "Enter marks for student " << i << ": ";
cin >> marks;
if (marks > 20) {
count++;
}
}
cout << "Number of students with marks > 20: " << count <<
endl;
return 0;
}
Use code with caution.
10. Finding Maximum and Minimum
C++
#include <iostream>
using namespace std;
int main() {
int n, max, min;
cout << "Enter the size of the list: ";
cin >> n;
cout << "Enter " << n << " numbers: ";
cin >> max;
min = max;
for (int i = 1; i < n; i++) {
int num;
cin >> num;
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
cout << "Maximum: " << max << endl;
cout << "Minimum: " << min << endl;
return 0;
}
11. Reversing Digits
C++
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0, remainder;
cout << "Enter a positive integer: ";
cin >> num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
cout << "Reversed number: " << reversed << endl;
return 0;
}
Use code with caution.
12. Multiplication Table
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << i * j << "\t";
}
cout << endl;
}
return 0;
}
Use code with caution.
13. Numbers Divisible by 10
a) While loop
C++
#include <iostream>
using namespace std;
int main() {
int num = 10;
while (num < 2000) {
cout << num << " ";
num += 10;
}
cout << endl;
return 0;
}
Use code with caution.
b) For loop
C++
#include <iostream>
using namespace std;
int main() {
for (int num = 10; num < 2000; num += 10) {
cout << num << " ";
}
cout << endl;
return 0;
}
Use code with caution.
c) Do-while loop
C++
#include <iostream>
using namespace std;
int main() {
int num = 10;
do {
cout << num << " ";
num += 10;
} while (num < 2000);
cout << endl;
return 0;
}
Use code with caution.
14. Fibonacci Series
C++
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
15. Pattern Generation
a. Right-angled triangle of numbers
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
b. Numbers in a square pattern
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows/columns: ";
cin >> n;
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << num++ << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
c. Right-angled triangle patter
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = i; j <= n; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
d. Inverted right-angled triangle pattern
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
cout << " ";
}
for (int j = i; j <= n; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
e. Diamond pattern
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = i; j >= 1; j--) {
cout << j << " ";
}
for (int j = 2; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = i; j >= 1; j--) {
cout << j << " ";
}
for (int j = 2; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
f. Double-line pattern
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Use code with caution.
g. Diamond-shaped pattern
C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = i; j >= 1; j--) {
cout << j << " ";
}
for (int j = 2; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = i; j >= 1; j--) {
cout << j << " ";
}
for (int j = 2; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
h. Right-angled triangle of alphabets
C++
#include <iostream>
using namespace std;
int main() {
int n;
char ch = 'a';
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << ch++ << " ";
}
cout << endl;
ch = 'a';
}
return 0;
Use code with caution.
i. Right-angled triangle of alphabets
C++
#include <iostream>
using namespace std;
int main() {
int n;
char ch = 'a';
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << ch++ << " ";
}
cout << endl;
}
return 0;
}
Use code with caution.
k. Right-angled triangle of alphabets
C++
#include <iostream>
using namespace std;
int main() {
int n;
char ch = 'a';
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = i; j <= n; j++) {
cout << ch++ << " ";
}
ch = 'a';
cout << endl;
}
return 0;
}
Part 2
1. Array Declarations
A) float voltage[100]; B) int years[100]; C) char characters[30]; D)
float velocities[32];
2. Input and Output of Voltage Values
C++
#include <iostream>
using namespace std;
int main() {
float volts[9] = {10.95, 16.32, 8.22, 15.98, 26.22, 13.54,
8.22, 6.45, 173.86};
for (int i = 0; i < 9; i++) {
cout << "Volt " << i + 1 << ": " << volts[i] << endl;
}
return 0;
}
Use code with caution.
3. Input, Sum, and Average of Temperatures
C++
#include <iostream>
using namespace std;
int main() {
int temp[8], total = 0;
for (int i = 0; i < 8; i++) {
cout << "Enter temperature " << i + 1 << ": ";
cin >> temp[i];
total += temp[i];
}
cout << "Temperatures: ";
for (int i = 0; i < 8; i++) {
cout << temp[i] << " ";
}
cout << endl;
cout << "Average temperature: " << (double)total / 8 << endl;
return 0;
}
Use code with caution.
4. Output of the Program
The output of the given program will be:
8 16 9 52 3 15 27 6 14 25 2 10
5. Sorting and Storing Values
C++
#include <iostream>
using namespace std;
int main() {
int arr[4][5] = {{16, 22, 99, 4, 18},
{-258, 4, 101, 5, 98},
{105, 6, 15, 2, 45},
{33, 88, 72, 16, 0}};
int sort[20];
int index = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
sort[index++] = arr[i][j];
}
}
// Sort the 'sort' array using a sorting algorithm (e.g., bubble
sort, selection sort, etc.)
// Here, we'll use a simple selection sort for demonstration:
for (int i = 0; i < 20; i++) {
int min_idx = i;
for (int j = i + 1; j < 20; j++) {
if (sort[j] < sort[min_idx]) {
min_idx = j;
}
}
swap(sort[i], sort[min_idx]);
}
cout << "Sorted values: ";
for (int i = 0; i < 20; i++) {
cout << sort[i] << " ";
}
cout << endl;
return 0;
}
6. &test
In C++, &test is the address-of operator. It returns the memory address of the
variable test. This is often used when passing arguments to functions by reference,
or when working with pointers.
7. Pointer Variable
A pointer variable must store a memory address. This address points to a location in
memory where the actual data is stored.
8. Declaration Statements
A) int *a_addr; B) double *b_addr; C) float *date; D) char *pt_char;
9. Pointer References
A) *(prices + 5) B) *(mile + 0) C) *(temp + 20) D) *(test + a + b) or
*(test + 9)
10. Array Representations
A) message[6] B) stock[2] C) *amount can't be directly replaced with an array
representation without more context. It depends on how amount is declared and used.
If amount is a pointer to a single value, then *amount is equivalent to the value it
points to. If amount is an array, then *amount is the first element of the array.
Part 3.
1. Multiplication Function
C++
#include <iostream>
using namespace std;
void mult(float a, float b) {
float product = a * b;
cout << "The product of " << a << " and " << b << " is: " <<
product << endl;
}
int main() {
float num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
mult(num1, num2);
return 0;
}
Use code with caution.
2. Leap Year Function
C++
#include <iostream>
using namespace std;
bool isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if (isLeapYear(year)) {
cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}
Use code with caution.
3. Number Guessing Game
C++
#include <iostream>#include <cstdlib>#include <ctime>
using namespace std;
int main() {
int num_games, score = 0, total_games = 0;
cout << "Enter the number of games: ";
cin >> num_games;
srand(time(0));
for (int i = 1; i <= num_games; i++) {
int secret_num = rand() % 100 + 1;
int guess, attempts = 0;
cout << "\nGame " << i << ":" << endl;
while (attempts < 3) {
cout << "Guess a number between 1 and 100: ";
cin >> guess;
attempts++;
if (guess == secret_num) {
if (attempts == 1) {
score += 10;
} else if (attempts == 2) {
score += 8;
} else {
score += 5;
}
cout << "Correct guess! You earned " << (attempts
== 1 ? 10 : attempts == 2 ? 8 : 5) << " points." << endl;
break;
} else if (guess < secret_num) {
cout << "Too low. Try again." << endl;
} else {
cout << "Too high. Try again." << endl;
}
}
if (attempts == 3 && guess != secret_num) {
cout << "You've run out of attempts. The correct
number was " << secret_num << endl;
}
total_games++;
}
double percentage = (double)score / (total_games * 10) * 100;
cout << "\nYour final score is: " << score << "/" << total_games *
10 << " (" << percentage << "%)" << endl;
if (percentage >= 80) {
cout << "Excellent performance! You're a number guessing
wizard!" << endl;
} else if (percentage >= 60) {
cout << "Good job! You're getting better at this." << endl;
} else {
cout << "Keep practicing, and you'll improve!" << endl;
}
return 0;
}
Use code with caution.
4. Function's Behavior
The given function foo computes the sum of the numbers from 1 to x, including x
itself.
Here's a breakdown of how it works:
Base cases:
o If x is 0, the function returns 0.
o If x is negative, the function recursively calls itself with -x and adds k to the result.
Recursive case:
o If x is positive, the function recursively calls itself with x-1, adds k to the result,
and then adds 1 to account for the current x.
The static variable k is used to keep track of the current value of x during the
recursive calls. This allows the function to calculate the sum correctly.
Sources and related content
5. Variable Scope
C++
# include <iostream.h> char key; long int number; int main() { int
a,b,c; double x,y;
… return 0;
} double secnum; int func1(int num1, int num2) { int o,p; float q;
… return p;
} double func2( float fisrt, float last) { int a,b,c,o,p;
floar r; double s,t,x;
… return s*t; }
Use code with caution.
Scope of key and number: Global scope, visible throughout the entire program.
Scope of x and y: Local to the main function.
Scope of secnum: Global scope, but only accessible from the point of declaration onwards.
Scope of o, p, and q: Local to the func1 function.
Scope of a, b, c, o, p, r, s, t, and x: Local to the func2 function.
6. Local Auto vs. Local Static Variables
Local Auto Variable:
o Declared inside a function.
o Its value is lost when the function returns.
o A new instance is created each time the function is called.
Local Static Variable:
o Declared inside a function using the static keyword.
o Its value persists between function calls.
o It's initialized only once, and subsequent calls to the function use the same value.
7. Recursive Function Analysis
a) Terminating Condition: n == 0 b) Recursive Part: return (n + sum(n -
1)); c) Hand Trace for n = 4:
sum(4) = 4 + sum(3)
sum(3) = 3 + sum(2)
sum(2) = 2 + sum(1)
sum(1) = 1 + sum(0)
sum(0) = 0
Tracing back:
o sum(1) = 1 + 0 = 1
o sum(2) = 2 + 1 = 3
o sum(3) = 3 + 3 = 6
o sum(4) = 4 + 6 = 10 So, the output is 10.
8. Recursive Programs
a) Palindrome Check
C++
bool isPalindrome(string str, int start, int end) {
if (start >= end) {
return true;
}
return str[start] == str[end] && isPalindrome(str, start + 1, end
- 1);
}
Use code with caution.
b) Array Comparison
C++
bool areArraysIdentical(int arr1[], int arr2[], int size) {
if (size == 0) {
return true;
}
return arr1[0] == arr2[0] && areArraysIdentical(arr1 + 1, arr2
+ 1, size - 1);
}
Use code with caution.
9. Output of the Program
the value of firstnum is 20
the value of firstnum is now 20
10. Modular Program for Marks Analysis
C++
#include <iostream>
using namespace std;
// Function prototypesvoid readList(float myList[], unsigned&
maxNo);float findMax(const float myList[], unsigned maxNo);float
findMin(const float myList[], unsigned maxNo);float
findRange(const float myList[], unsigned maxNo);float
findMean(const float myList[], unsigned num);void display(const
float myList[], unsigned maxNo);
int main() {
float marks[100];
unsigned numMarks;
readList(marks, numMarks);
float maxMark = findMax(marks, numMarks);
float minMark = findMin(marks, numMarks);
float range = findRange(marks, numMarks);
float mean = findMean(marks, numMarks);
display(marks, numMarks);
cout << "Maximum mark: " << maxMark << endl;
cout << "Minimum mark: " << minMark << endl;
cout << "Range: " << range << endl;
cout << "Mean: " << mean << endl;
return 0;
}
// Function definitions (implementations)// ... (implementations for
each function as described in the prompt)
Use code with caution.
Note: The implementations for the functions readList, findMax, findMin,
findRange, findMean, and display would involve standard array operations and
calculations.
Part 4.
1. Input and Display Student Record
C++
#include <iostream>
using namespace std;
struct Student {
char name[20];
char sex;
int age;
};
void InputData(Student &s) {
cout << "Enter name: ";
[Link]([Link], 20);
cout << "Enter sex (M/F): ";
cin >> [Link];
cout << "Enter age: ";
cin >> [Link];
[Link](); // Consume the newline character
}
void DisplayData(Student s) {
cout << "Name: " << [Link] << endl;
cout << "Sex: " << [Link] << endl;
cout << "Age: " << [Link] << endl;
}
int main() {
Student s;
InputData(s);
DisplayData(s);
return 0;
}
Use code with caution.
2. List of Students
C++
#include <iostream>
using namespace std;
// ... (Same Student struct and InputData function)
void DisplayList(Student students[], int n) {
cout << "Name\tSex\tAge\n";
for (int i = 0; i < n; i++) {
cout << students[i].name << "\t" << students[i].sex << "\t" <<
students[i].age << endl;
}
}
void DisplayBySex(Student students[], int n, char sex) {
cout << "Name\tSex\tAge\n";
for (int i = 0; i < n; i++) {
if (students[i].sex == sex) {
cout << students[i].name << "\t" << students[i].sex << "\
t" << students[i].age << endl;
}
}
}
int main() {
int n;
cout << "Enter the number of students: ";
cin >> n;
Student students[n];
for (int i = 0; i < n; i++) {
InputData(students[i]);
}
DisplayList(students, n);
char choice;
cout << "Display male (M) or female (F) students? ";
cin >> choice;
DisplayBySex(students, n, choice);
return 0;
}
Use code with caution.
3. Student Records with Ranks
C++
// ... (Similar structure and functions)
void calculateAverageAndRank(Student students[], int n) {
for (int i = 0; i < n; i++) {
float sum = 0;
for (int j = 0; j < 5; j++) {
sum += students[i].marks[j];
}
students[i].average = sum / 5;
}
// Sort students by average marks in descending order
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].average < students[j + 1].average) {
swap(students[j], students[j + 1]);
}
}
}
// Assign ranks
students[0].rank = 1;
for (int i = 1; i < n; i++) {
students[i].rank = students[i - 1].rank;
if (students[i].average < students[i - 1].average) {
students[i].rank++;
}
}
}
void displayRankedList(Student students[], int n) {
cout << "ID\tAverage Rank\n";
for (int i = 0; i < n; i++) {
cout << students[i].id << "\t" << students[i].average << "\t"
<< students[i].rank << endl;
}
}
Use code with caution.
4. Town Structure
C++
#include <iostream>
using namespace std;
typedef bool Boolean;
struct Town {
char name[50];
int population;
Boolean hasAirport;
};
int main() {
// ... (code to input and process town data)
}
Use code with caution.
5. Employee Record and Payroll Report
C++
#include <iostream>
using namespace std;
struct Employee {
int number;
char name[20];
float rate;
int hours;
};
int main() {
Employee employees[6];
float totalPay = 0;
// Input employee data
for (int i = 0; i < 6; i++) {
cout << "Enter employee " << i + 1 << " details:\n";
cout << "Number: ";
cin >> employees[i].number;
[Link]();
cout << "Name: ";
[Link](employees[i].name, 20);
cout << "Rate: ";
cin >> employees[i].rate;
cout << "Hours: ";
cin >> employees[i].hours;
}
// Calculate payroll
cout << "\nPayroll Report\n";
cout << "Number\tName\tGross Pay\n";
for (int i = 0; i < 6; i++) {
float grossPay = employees[i].rate * employees[i].hours;
totalPay += grossPay;
cout << employees[i].number << "\t" << employees[i].name <<
"\t" << grossPay << endl;
}
cout << "\nTotal Gross Pay: " << totalPay << endl;
return 0;
}