Computer programming
Group assignment
Chemical engineering section B
Group members
1, Lidiya Tewelde ………………………………………………….. ETS
0867/16
2, Lieliena Zena ………………………………………………………. ETS
0870/16
3, Melat Gezahegn ……………………………………………….. ETS
0933/16
4, Tsige Tesfaye ……………………………………………………. ETS
1373/16
5, Meron Feleke …………………………………………………… ETS
0948/16
Submission date:-
January 12,2025
Submitted to:- MR.
Bilew Alemu
C++ Assignment (One point each)
1. Write a program to calculate the area of a circle using a
constant value for pi (3.14).
2. Write a program to perform addition, subtraction,
multiplication, and division of two numbers.
3. Write a program to read a user's name and age, and then
display them.
4. Write a program to calculate the total cost of items
purchased, considering the price per item and quantity
entered by the user.
5. Write a program to check if a number is positive, negative,
or zero using an if-else control structure.
6. Write a program to find the largest of three numbers using
a conditional (ternary) operator.
7. Write a program to display the multiplication table of a
number using a for loop.
8. Write a program that skips even numbers and only prints
odd numbers from 1 to 10 using the continue statement.
9. Write a program to display the day of the week based on a
number (1 for Monday, 2 for Tuesday, ..., 7 for Sunday) using a
switch statement.
10. Write a function to calculate the factorial of a number and
call it from the main() function.
11. Write a program to swap two numbers using pass-by-
value. Explain why it doesn’t work.
12. Modify the above program to swap two numbers using
pass-by-reference.
13. Write a program to find the largest element in an array of
integers.
14. Write a program to count the number of vowels in a
character array (C-style string).
15. Write a program to demonstrate the use of pointers to
swap two numbers.
16. Write a program to dynamically allocate memory for an
array and calculate the sum of its elements.
17. Write a program to store and display information of
students using a struct.
18. Write a program to implement a simple class to store and
display information about a book.
19. Write a program to create a class Rectangle with
attributes length and breadth, and a method to calculate the
area.
20. Write a program to demonstrate the use of constructors
and destructors in a class Circle. 1
21. Write a program to create a class Box that has multiple
constructors to initialize dimensions in different ways.
22. Write a program to create a class Student that initializes
name and rollNumber with default values using a default
constructor.
23. Write a program to create a class Car and demonstrate
how to use a pointer to access its methods and attributes.
24. Write a program to define a class Employee using struct
and demonstrate its usage.
25. Write a program to demonstrate a class-like union with
different data types.
Answer for Questions of C++ Assignments
1, #include <iostream>
using namespace std;
int main ()
{
float pi=3.14;
float radius, area ;
cout<< "enter the radius of the circle"<<endl;
cin>>radius;
area=pi* radius* radius;
cout<<"area of the circle is "<< area<<endl;
return 0;
}
2, #include <iostream>
using namespace std;
int main() {
float a,b ;
cout<<"enter two number"<<endl;
cin>> a>>b;
cout<<"the multiplication of two numbers is "<< a*b <<endl;
cout<< "the addition of two numbers is "<< a+b << endl;
cout<< "the substractiom of two number is "<< a-b<< endl;
cout<< "the division of two numbers is "<< a/b<< endl;
return 0;
}
3, #include <iostream>
using namespace std;
int main() {
string name;
int age;
cout<< "enter your user name "<<endl;
cin>>name;
cout<< "enter your age "<<endl;
cin>>age;
cout<< " hi " << name << " you are " << age <<" years old";
return 0;
}
4, #include <iostream>
using namespace std;
int main() {
double pricePerItem;
int quantity;
double totalCost;
cout << "Enter the price per item: ";
cin >> pricePerItem;
cout << "Enter the quantity of items: ";
cin >> quantity;
totalCost = pricePerItem * quantity;
cout << "The total cost is: " << totalCost << endl;
return 0;
}
5, #include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
6, #include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
int largest = (a > b && a > c) ? a : (b > c) ? b : c;
cout << "The largest number is: " << largest << endl;
return 0;
}
7, #include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number to display its multiplication table: ";
cin >> num;
for (int i = 1; i <= 10; i++) {
cout << num << " * " << i << " = " << num * i << endl;
}
return 0;
}
8, #include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
cout << i << " ";
}
cout << endl;
return 0;
}
9, #include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter a number (1-7) to get the day of the week: ";
cin >> day;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid input! Please enter a number between 1 and 7." << endl;
break;
}
return 0;
}
10, #include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1) {
return 1;
return n * factorial(n - 1);
int main() {
int num;
cout << "Enter a number to calculate its factorial: ";
cin >> num;
cout << "The factorial of " << num << " is: " << factorial(num) <<
endl;
return 0;
}
11, #include <iostream>
using namespace std;
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
int main() {
int x = 5, y = 10;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
12, #include <iostream>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
int main() {
int x = 5, y = 10;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
13, #include <iostream>
using namespace std;
int findLargest(int arr[], int size) {
int largest = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i];
return largest;
int main() {
int arr[] = {1, 4, 5, 2, 8, 6};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "The largest element is: " << findLargest(arr, size) <<
endl;
return 0;
}
14, #include <iostream>
using namespace std;
int countVowels(const char* str) {
int count = 0;
while (*str) {
char ch = tolower(*str);
if (ch == 'a' ch == 'e' ch == 'i' ch == 'o' ch == 'u') {
count++;
str++;
return count;
int main() {
const char* str = "Hello World";
cout << "The number of vowels is: " << countVowels(str) << endl;
return 0;
15, #include <iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
}
16, #include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int* arr = new int[n];
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
cout << "Sum of array elements: " << sum << endl;
delete[ ] arr;
return 0;
17, #include <iostream>
using namespace std;
struct Student {
string name;
int age;
float marks;
};
int main() {
int n;
cout << "Enter the number of students: ";
cin >> n;
Student* students = new Student[n];
for (int i = 0; i < n; i++) {
cout << "Enter details of student " << i + 1 << ":" << endl;
cout << "Name: ";
cin >> students[i].name;
cout << "Age: ";
cin >> students[i].age;
cout << "Marks: ";
cin >> students[i].marks;
cout << "\nStudent Details:\n";
for (int i = 0; i < n; i++) {
cout << "Name: " << students[i].name << ", Age: " <<
students[i].age << ", Marks: " << students[i].marks << endl;
delete[] students;
return 0;
18, #include <iostream>
using namespace std;
class Book {
private:
string title;
string author;
float price;
public:
void setDetails(string t, string a, float p) {
title = t;
author = a;
price = p;
void displayDetails() {
cout << "Book Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: Birr" << price << endl;
};
int main() {
Book book;
string title, author;
float price;
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter book author: ";
getline(cin, author);
cout << "Enter book price: ";
cin >> price;
book.setDetails(title, author, price);
cout << "\nBook Details:\n";
book.displayDetails();
return 0;
19, #include <iostream>
using namespace std;
class Rectangle {
private:
float length;
float breadth;
public:
void setDimensions(float l, float b) {
length = l;
breadth = b;
float calculateArea() {
return length * breadth;
}
void displayDetails() {
cout << "Length: " << length << ", Breadth: " << breadth <<
endl;
cout << "Area: " << calculateArea() << endl;
};
int main() {
Rectangle rect;
float length, breadth;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the breadth of the rectangle: ";
cin >> breadth;
rect.setDimensions(length, breadth);
cout << "\nRectangle Details:\n";
rect.displayDetails();
return 0;
20, #include <cmath>
using namespace std;
class Circle {
private:
double radius;
public:
Circle(double r) : radius(r) {
cout << "Constructor called: Circle created with radius = " <<
radius << endl;
Circle() {
cout << "Destructor called: Circle with radius " << radius << "
is destroyed" << endl;
}
double getArea() const {
return M_PI * radius * radius;
double getCircumference() const {
return 2 * M_PI * radius;
};
int main() {
Circle circle1(5.0);
cout << "Area of the circle: " << circle1.getArea() << endl;
cout << "Circumference of the circle: " <<
circle1.getCircumference() << endl;
return 0;
21, #include <iostream>
using namespace std;
class Box {
private:
double length, width, height;
public:
Box(double l = 0, double w = 0, double h = 0) : length(l),
width(w), height(h) {}
Box(const Box &b) = default;
void displayDimensions() const {
cout << "Length: " << length << ", Width: " << width << ",
Height: " << height << endl;
void setDimensions(double l, double w, double h) {
length = l;
width = w;
height = h;
};
int main() {
double l, w, h;
cout << "Enter length: " ;
cin >> l;
cout << "Enter width: ";
cin >> w;
cout << "Enter height: ";
cin >> h;
Box userBox(l, w, h);
return 0;
22, #include <iostream>
using namespace std;
class Student {
private:
string name;
int rollNumber;
public:
Student() : name("Default Name"), rollNumber(0) {}
void displayDetails() const {
cout << "Name: " << name << ", Roll Number: " << rollNumber
<< endl;
};
int main() {
Student student;
student.displayDetails();
return 0;
23, #include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
Car(string b, int y) : brand(b), year(y) {}
void displayDetails() const {
cout << "Brand: " << brand << ", Year: " << year << endl;
};
int main() {
Car* carPtr = new Car("Toyota", 2022);
carPtr->displayDetails();
delete carPtr;
return 0;
24, #include <iostream>
using namespace std;
struct Employee {
string name;
int id;
double salary;
void displayDetails() const {
cout << "Name: " << name << ", ID: " << id << ", Salary: $" <<
salary << endl;
};
int main() {
Employee emp = {"John Doe", 101, 75000.50};
emp.displayDetails();
return 0;
25, #include <iostream>
using namespace std;
union Data {
int intValue;
float floatValue;
char charValue;
void displayInt() const {
cout << "Integer: " << intValue << endl;
void displayFloat() const {
cout << "Float: " << floatValue << endl;
void displayChar() const {
cout << "Character: " << charValue << endl;
};
int main() {
Data data;
data.intValue = 42;
data.displayInt();
data.floatValue = 3.14f;
data.displayFloat();
data.charValue = 'A';
data.displayChar();
return 0;