Codes For C++ Language
Codes For C++ Language
Table of Contents
1
Cs-308 assignment
1. Sorted Array
2. Swapped Values
3. Maximum Value
4. Account Details (Dynamic Array)
5. Average Marks (Dynamic Array)
6. Prime Checker (Function Pointer)
1. Palindrome Check
2. Count Words & Characters
3. Reverse String
4. Triangle Pattern
5. Toggle Case
Output:
#include <iostream>
#include <cmath>
int main() {
2
Cs-308 assignment
cout << "Log base 2 of " << number << " is: " << result << endl;
return 0;
Output:
#include <iostream>
int main() {
cout << "Input letter: " << letter << ", Next letters: "
return 0;
3
Cs-308 assignment
Output:
#include <iostream>
int main() {
sum += digit;
temp /= 10;
cout << "Sum of digits of " << number << " is: " << sum << endl;
return 0;
Output:
#include <iostream>
4
Cs-308 assignment
int main() {
cout << "Basic Salary: " << basic << ", Gross Salary: " << gross << endl;
return 0;
Output:
#include <iostream>
int main() {
totalSec %= 60;
totalMin %= 60;
5
Cs-308 assignment
cout << "Total time is " << totalHour << ":" << totalMin << ":" << totalSec << endl;
return 0;
Compound Interest
Output:
#include <iostream>
#include <cmath>
int main() {
double P = 1000, R = 5, T = 2;
return 0;
6
Cs-308 assignment
ASCII Code
Output:
#include <iostream>
int main() {
char ch = 'A';
cout << "ASCII code of '" << ch << "' is: " << int(ch) << endl;
return 0;
Output:
7
Cs-308 assignment
#include <iostream>
int main() {
total += marks[i];
cout << "Total Marks: " << total << ", Percentage: " << percentage << "%" << endl;
return 0;
Palindrome Check
#include <iostream>
#include <string>
#include <algorithm>
reverse(reversed.begin(), reversed.end());
8
Cs-308 assignment
int main() {
string input;
getline(cin, input);
if (isPalindrome(input)) {
} else {
return 0;
Output:
#include <iostream>
#include <string>
9
Cs-308 assignment
class BankAccount {
private:
string depositorName;
string accountNumber;
string accountType;
double balance;
public:
depositorName = name;
accountNumber = accNum;
accountType = type;
balance = initialBalance;
balance += amount;
} else {
10
Cs-308 assignment
balance -= amount;
void display() {
};
int main() {
account.display();
account.deposit(500);
account.withdraw(200);
account.display();
return 0;
Output:
Balance: 1000
Deposited: 500
Withdrawn: 200
11
Cs-308 assignment
Balance: 1300
Temperature Message
#include <iostream>
int main() {
float temp;
} else {
return 0;
Output:
Enter temperature: 28
Pleasant day
12
Cs-308 assignment
Enter temperature: 12
Cool day
Distance Converter
#include <iostream>
int main() {
float value;
int choice;
return 1;
13
Cs-308 assignment
switch(choice) {
case 1:
cout << value << " inches = " << value * 2.54 << " cm" << endl;
break;
case 2:
cout << value << " gallons = " << value * 3.785 << " liters" << endl;
break;
case 3:
cout << value << " miles = " << value * 1.609 << " km" << endl;
break;
case 4:
cout << value << " pounds = " << value * 0.4536 << " kg" << endl;
break;
return 0;
Output:
Conversion Options:
1. Inches to Centimeters
2. Gallons to Liters
3. Miles to Kilometers
14
Cs-308 assignment
4. Pounds to Kilograms
10 miles = 16.09 km
#include <iostream>
#include <string>
class Run {
private:
string runnerName;
float distance;
public:
void getData() {
15
Cs-308 assignment
maxDistance = distance;
bestRunner = runnerName;
void showData() {
cout << "Distance: " << distance << " km" << endl;
cout << "Best Runner: " << bestRunner << " (" << maxDistance << " km)" << endl;
};
float Run::maxDistance = 0;
int main() {
Run runners[3];
cout << "\nEnter details for runner " << i+1 << endl;
runners[i].getData();
16
Cs-308 assignment
runners[i].showData();
Run::showBestRunner();
return 0;
Output:
All Runners:
17
Cs-308 assignment
Runner: Alice
Distance: 12.5 km
Runner: Bob
Distance: 15.2 km
Runner: Carol
Distance: 14.8 km
#include <iostream>
return true;
int main() {
int number;
18
Cs-308 assignment
if (isPrime(&number)) {
} else {
cout << number << " is not a prime number." << endl;
return 0;
Output:
Enter an integer: 17
17 is a prime number.
Enter an integer: 22
#include <iostream>
#include <string>
int main() {
string input;
19
Cs-308 assignment
getline(cin, input);
return 0;
Output:
Wo
Wor
Worl
World
#include <iostream>
#include <cctype>
20
Cs-308 assignment
if (tolower(c) == tolower(ch)) {
if (islower(c)) c = toupper(c);
else c = tolower(c);
int main() {
string input;
char character;
getline(cin, input);
convertChar(input, character);
return 0;
Output:
Enter a character: l
21
Cs-308 assignment
Series Sum
#include <iostream>
int main() {
return 0;
Output:
#include <iostream>
22
Cs-308 assignment
int main() {
int year;
if (isLeapYear(year)) {
} else {
cout << year << " is not a leap year." << endl;
return 0;
Output:
23
Cs-308 assignment
Array of Structures
#include <iostream>
struct Person {
float income;
float taxRate;
float tax;
};
int main() {
Person people[5];
cout << "Enter income for person " << i+1 << ": ";
cout << "Person " << i+1 << ": Income=" << people[i].income
24
Cs-308 assignment
return 0;
Output:
Tax Details:
#include <iostream>
25
Cs-308 assignment
return true;
int main() {
if (isPrime(numbers[i])) count++;
cout << "Number of prime numbers: " << count << endl;
return 0;
Output:
Enter 10 integers:
26
Cs-308 assignment
7 12 23 34 41 56 67 78 89 97
#include <iostream>
#include <cmath>
if (discriminant < 0) {
cout << "Sorry, the roots are not real." << endl;
} else {
cout << "Roots of the equation are " << root1 << " and " << root2 << endl;
int main() {
double a, b, c;
27
Cs-308 assignment
if (a == 0) {
} else {
findRoots(a, b, c);
return 0;
/* Output:
Enter coefficients a, b, c: 1 1 -6
Enter coefficients a, b, c: 1 0 9
*/
Student Grades
#include <iostream>
int main() {
28
Cs-308 assignment
else gradeF++;
return 0;
/* Output:
Grade Distribution:
A: 3 students
B: 3 students
C: 2 students
F: 2 students
*/
29
Cs-308 assignment
```
#include <iostream>
#include <string>
int main() {
string names[NUM_EMPLOYEES];
float salaries[NUM_EMPLOYEES];
cout << "Enter name of employee " << i+1 << ": ";
30
Cs-308 assignment
} else {
return 0;
/* Output:
Employee Details:
*/
#include <iostream>
31
Cs-308 assignment
*(arr + j) = temp;
int main() {
sort(arr, 5);
return 0;
Sample Output:
32
Cs-308 assignment
Sorted array: 1 2 3 5 9
#include <iostream>
*a = *b;
*b = temp;
int main() {
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(&x, &y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
Sample Output:
33
Cs-308 assignment
#include <iostream>
int main() {
max = *ptr[i];
cout << "Maximum value is: " << max << endl;
return 0;
Sample Output:
34
Cs-308 assignment
#include <iostream>
struct Account {
int id;
float amount;
};
cout << "Enter ID and Amount for Account " << i + 1 << ": ";
cout << "Account ID: " << acc[i].id << ", Amount: " << acc[i].amount << endl;
int main() {
int n;
35
Cs-308 assignment
cin >> n;
input(acc, n);
display(acc, n);
delete[] acc;
return 0;
Sample Output:
#include <iostream>
int main() {
int n;
36
Cs-308 assignment
cin >> n;
float sum = 0;
cout << "Enter marks for student " << i + 1 << ": ";
sum += marks[i];
delete[] marks;
return 0;
Sample Output:
Average marks: 90
37
Cs-308 assignment
#include <iostream>
*x = *y;
*y = temp;
int main() {
cout << "Before: a = " << a << ", b = " << b << endl;
swap(&a, &b);
cout << "After: a = " << a << ", b = " << b << endl;
return 0;
#include <iostream>
bool isPrime(int n) {
38
Cs-308 assignment
if (n % i == 0)
return false;
return true;
int main() {
int num;
if (ptr(num))
else
return 0;
#include <iostream>
39
Cs-308 assignment
struct Player {
string name;
int minutes;
int seconds;
};
int main() {
cout << "Enter name, minutes and seconds for Player 1: ";
cout << "Enter name, minutes and seconds for Player 2: ";
else
40
Cs-308 assignment
return 0;
Sample Output:
#include <iostream>
struct Employee {
int code;
float salary;
string grade;
};
int main() {
cout << "Enter code, salary, and grade for Employee 1: ";
41
Cs-308 assignment
cout << "Enter code, salary, and grade for Employee 2: ";
cout << "Code: " << e1.code << ", Salary: " << e1.salary << ", Grade: " << e1.grade << endl;
else
cout << "Code: " << e2.code << ", Salary: " << e2.salary << ", Grade: " << e2.grade << endl;
return 0;
Sample Output:
#include <iostream>
struct Person {
42
Cs-308 assignment
float income;
float taxRate;
};
int main() {
Person persons[5];
cout << "Enter income and tax rate for person " << i + 1 << ": ";
cout << "Tax for person " << i + 1 << ": " << tax << endl;
return 0;
Sample Output:
43
Cs-308 assignment
#include <iostream>
struct Book {
int bookID;
string name;
float price;
};
struct Order {
int orderID;
Book books[5];
};
int main() {
Order order;
cout << "Enter BookID, Name, Price for Book " << i + 1 << ": ";
44
Cs-308 assignment
cout << "Book " << i + 1 << ": " << order.books[i].bookID << ", "
return 0;
Sample Output:
Order Details:
...
// ===============================
#include <iostream>
#include <cstring>
45
Cs-308 assignment
#include <cctype>
struct Player {
int minutes;
int seconds;
float distance;
};
int totalSeconds(Player p) {
void structure_ex1() {
cout << "Enter distance, minutes and seconds for Player 1: ";
cout << "Enter distance, minutes and seconds for Player 2: ";
46
Cs-308 assignment
struct Employee {
int code;
float salary;
string grade;
};
void structure_ex2() {
cout << "Enter code, salary, and grade for Employee 1: ";
cout << "Enter code, salary, and grade for Employee 2: ";
cout << ((e1.salary > e2.salary) ? "Employee 1 has higher salary\n" : "Employee 2 has higher salary\n");
struct Person {
float income;
float taxRate;
};
47
Cs-308 assignment
void structure_ex3() {
Person p[5];
cout << "Enter income and tax rate for person " << i + 1 << ": ";
cout << "Tax for person " << i + 1 << ": " << (p[i].income * p[i].taxRate / 100) << endl;
struct Book {
int bookID;
string name;
float price;
};
struct Order {
int orderID;
Book books[5];
};
void structure_ex4() {
Order o;
48
Cs-308 assignment
cout << "Enter book ID, name, price for Book " << i + 1 << ": ";
cout << "Book " << i + 1 << ": " << o.books[i].bookID << ", " << o.books[i].name << ", $" <<
o.books[i].price << endl;
void pointer_ex1() {
int* p = arr;
*(p + j) = temp;
49
Cs-308 assignment
for (int i = 0; i < 5; i++) cout << *(p + i) << " ";
void pointer_ex2() {
*x = *y;
*y = temp;
cout << "\n[Pointer 2] Swapped: a = " << a << ", b = " << b << endl;
void pointer_ex3() {
cout << "\n[Pointer 3] Maximum Value: " << max << endl;
struct Account {
50
Cs-308 assignment
int id;
float amount;
};
void pointer_ex4() {
int n;
cin >> n;
cout << "Enter ID and Amount for Account " << i + 1 << ": ";
cout << "Account ID: " << acc[i].id << ", Amount: " << acc[i].amount << endl;
delete[] acc;
void pointer_ex5() {
int n;
cin >> n;
float sum = 0;
51
Cs-308 assignment
cout << "Enter marks for student " << i + 1 << ": ";
sum += marks[i];
delete[] marks;
bool isPrime(int n) {
if (n % i == 0) return false;
return true;
void pointer_ex6() {
int num;
52
Cs-308 assignment
void string_ex1() {
char str[100];
isPal = false;
break;
void string_ex2() {
char str[200];
cin.ignore();
cin.getline(str, 200);
chars++;
53
Cs-308 assignment
if (!inWord) {
words++;
inWord = true;
cout << "Words: " << words << ", Characters (no spaces): " << chars << endl;
void string_ex3() {
char str[100];
void string_ex4() {
char str[100];
54
Cs-308 assignment
void string_ex5() {
if (str[i] == ch) {
int main() {
structure_ex1();
structure_ex2();
structure_ex3();
55
Cs-308 assignment
structure_ex4();
pointer_ex1();
pointer_ex2();
pointer_ex3();
pointer_ex4();
pointer_ex5();
pointer_ex6();
string_ex1();
string_ex2();
string_ex3();
string_ex4();
string_ex5();
return 0;
}// ===============================
// ===============================
#include <iostream>
#include <cstring>
#include <cctype>
56
Cs-308 assignment
struct Player {
int minutes;
int seconds;
float distance;
};
int totalSeconds(Player p) {
void structure_ex1() {
cout << "Enter distance, minutes and seconds for Player 1: ";
cout << "Enter distance, minutes and seconds for Player 2: ";
57
Cs-308 assignment
struct Employee {
int code;
float salary;
string grade;
};
void structure_ex2() {
cout << "Enter code, salary, and grade for Employee 1: ";
cout << "Enter code, salary, and grade for Employee 2: ";
cout << ((e1.salary > e2.salary) ? "Employee 1 has higher salary\n" : "Employee 2 has higher salary\n");
struct Person {
float income;
float taxRate;
};
void structure_ex3() {
Person p[5];
58
Cs-308 assignment
cout << "Enter income and tax rate for person " << i + 1 << ": ";
cout << "Tax for person " << i + 1 << ": " << (p[i].income * p[i].taxRate / 100) << endl;
struct Book {
int bookID;
string name;
float price;
};
struct Order {
int orderID;
Book books[5];
};
void structure_ex4() {
Order o;
cout << "Enter book ID, name, price for Book " << i + 1 << ": ";
59
Cs-308 assignment
cout << "Book " << i + 1 << ": " << o.books[i].bookID << ", " << o.books[i].name << ", $" <<
o.books[i].price << endl;
void pointer_ex1() {
int* p = arr;
*(p + j) = temp;
for (int i = 0; i < 5; i++) cout << *(p + i) << " ";
60
Cs-308 assignment
void pointer_ex2() {
*x = *y;
*y = temp;
cout << "\n[Pointer 2] Swapped: a = " << a << ", b = " << b << endl;
void pointer_ex3() {
cout << "\n[Pointer 3] Maximum Value: " << max << endl;
struct Account {
int id;
float amount;
};
void pointer_ex4() {
61
Cs-308 assignment
int n;
cin >> n;
cout << "Enter ID and Amount for Account " << i + 1 << ": ";
cout << "Account ID: " << acc[i].id << ", Amount: " << acc[i].amount << endl;
delete[] acc;
void pointer_ex5() {
int n;
cin >> n;
float sum = 0;
cout << "Enter marks for student " << i + 1 << ": ";
sum += marks[i];
62
Cs-308 assignment
delete[] marks;
bool isPrime(int n) {
if (n % i == 0) return false;
return true;
void pointer_ex6() {
int num;
void string_ex1() {
char str[100];
63
Cs-308 assignment
isPal = false;
break;
void string_ex2() {
char str[200];
cin.ignore();
cin.getline(str, 200);
chars++;
if (!inWord) {
words++;
inWord = true;
64
Cs-308 assignment
cout << "Words: " << words << ", Characters (no spaces): " << chars << endl;
void string_ex3() {
char str[100];
void string_ex4() {
char str[100];
65
Cs-308 assignment
void string_ex5() {
if (str[i] == ch) {
int main() {
structure_ex1();
structure_ex2();
structure_ex3();
structure_ex4();
pointer_ex1();
pointer_ex2();
pointer_ex3();
66
Cs-308 assignment
pointer_ex4();
pointer_ex5();
pointer_ex6();
string_ex1();
string_ex2();
string_ex3();
string_ex4();
string_ex5();
return 0;
67