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