List of Programs
List of Programs
Task Breakdown:
1. Objective:
○ Create a class Person that models a person with attributes
such as name and age.
Python Implementation:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")
# Main program
person = Person("John Doe", 25)
person.display_info()
C++ program:
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
void display_info() {
cout << "Name: " << name << ", Age: " << age << endl;
}
int main() {
Person person("John Doe", 25);
person.display_info();
string new_name;
int new_age;
cout << "Enter a new name: ";
cin >> new_name;
cout << "Enter a new age: ";
cin >> new_age;
person.update_info(new_name, new_age);
person.display_info();
return 0;
}
def display_info(self):
print(f"Account holder: {self.account_holder}, Balance:
{self.balance}")
# Main program
account = BankAccount("Alice")
account.display_info()
account.display_info()
C++ Code: #include <iostream>
using namespace std;
class BankAccount {
private:
string account_holder;
double balance;
public:
BankAccount(string holder, double bal = 0) :
account_holder(holder), balance(bal) {}
void display_info() {
cout << "Account holder: " << account_holder << ", Balance: "
<< balance << endl;
}
};
int main() {
BankAccount account("Alice");
account.display_info();
double deposit_amount;
cout << "Enter amount to deposit: ";
cin >> deposit_amount;
account.deposit(deposit_amount);
double withdrawal_amount;
cout << "Enter amount to withdraw: ";
cin >> withdrawal_amount;
account.withdraw(withdrawal_amount);
account.display_info();
return 0;
}
Objective:
Instructions:
Create a base class Vehicle with attributes like make, model, and year.
Python Implementation:
class Vehicle:
self.make = make
self.model = model
self.year = year
def display_info(self):
class Car(Vehicle):
self.fuel_type = fuel_type
def display_info(self):
super().display_info()
my_car.display_info()
Objective:
If-Else Structure:
if condition:
else:
Else-If Ladder (Optional): If there are multiple conditions, you can use
an else if or elif (in Python) to test additional conditions.
Syntax:
if condition1:
elif condition2:
else:
Simple If Statement
Problem:
Steps:
if num > 0:
if num > 0:
print("Positive number.")
else:
print("Negative number.")
Program 3: Understanding and Practicing Boolean Operators (&&
and ||)
Objective:
This operator returns true if both operands are true. If any operand is
false, the entire expression evaluates to false.
Logical OR (||):
This operator returns true if at least one operand is true. It only returns
false if both operands are false.
}
Experiment 1: Use of the and Operator
Problem Statement:
# Test cases
check_number(12) # Should meet both conditions
check_number(25) # Out of range
check_number(15) # In range but odd
Output:
Problem Statement:
Write a Python program that checks whether a number is either negative
or greater than 50.
# Test cases
check_number(-5) # Negative
check_number(55) # Greater than 50
check_number(25) # Neither condition met
Practice Exercises:
1. Exercise 1:
Write a Python program that checks whether a number is divisible
by both 3 and 5.
2. Exercise 2:
Write a Python program that checks whether a string starts with
"A" or ends with "Z".
Program 4: Write a Program to Implement 4 Types of Pyramid Patterns
in Python
Left-aligned Pyramid:
This pyramid aligns its stars (*) to the left, increasing the number of
stars per row.
# Left-aligned Pyramid
def left_aligned_pyramid(rows):
for i in range(1, rows + 1):
print('* ' * i)
O/P
*
**
***
****
*****
Right-aligned Pyramid:
This pyramid aligns its stars to the right by adding leading spaces.
# Right-aligned Pyramid
def right_aligned_pyramid(rows):
for i in range(1, rows + 1):
rows = 5
right_aligned_pyramid(rows)
**
***
****
*****
Centered Pyramid:
# Centered Pyramid
def centered_pyramid(rows):
rows = 5
centered_pyramid(rows)
O/P:
**
***
****
*****
Inverted Pyramid:
This pyramid starts with the maximum number of stars and decreases
with each row.
# Inverted Pyramid
def inverted_pyramid(rows):
print('* ' * i)
rows = 5
inverted_pyramid(rows)
O/P:
*****
****
***
**
if number > 0:
print("The number is positive.")
else: