0% found this document useful (0 votes)
27 views12 pages

Piggy

Codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Piggy

Codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Below are the C++ programs based on the given requirements:

If-Else and Switch Statements

1. Check if a number is positive, negative, or zero using if-else:

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter an integer: ";
cin >> num;

if (num > 0)
cout << "The number is positive.";
else if (num < 0)
cout << "The number is negative.";
else
cout << "The number is zero.";

return 0;
}

2. Display weekday name using switch statement:

#include <iostream>
using namespace std;

int main() {
int day;
cout << "Enter day number (1-7): ";
cin >> day;

switch (day) {
case 1: cout << "Sunday"; break;
case 2: cout << "Monday"; break;
case 3: cout << "Tuesday"; break;
case 4: cout << "Wednesday"; break;
case 5: cout << "Thursday"; break;
case 6: cout << "Friday"; break;
case 7: cout << "Saturday"; break;
default: cout << "Invalid day number!";
}
return 0;
}

3. Find the largest of three numbers using if-else:

#include <iostream>
using namespace std;

int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;

if (a >= b && a >= c)


cout << "The largest number is: " << a;
else if (b >= a && b >= c)
cout << "The largest number is: " << b;
else
cout << "The largest number is: " << c;

return 0;
}

4. Calculator program using switch:

#include <iostream>
using namespace std;

int main() {
char op;
float num1, num2;

cout << "Enter operator (+, -, *, /): ";


cin >> op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;

switch (op) {
case '+': cout << "Result: " << num1 + num2; break;
case '-': cout << "Result: " << num1 - num2; break;
case '*': cout << "Result: " << num1 * num2; break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2;
else
cout << "Division by zero error!";
break;
default: cout << "Invalid operator!";
}

return 0;
}

5. Check if a year is a leap year using if-else:

#include <iostream>
using namespace std;

int main() {
int year;
cout << "Enter a year: ";
cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";

return 0;
}

Loops

1. Multiplication table using a for loop:

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;

for (int i = 1; i <= 10; ++i) {


cout << num << " * " << i << " = " << num * i << endl;
}
return 0;
}

2. Sum of numbers using a while loop:

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;

int i = 1;
while (i <= n) {
sum += i;
++i;
}

cout << "Sum = " << sum;


return 0;
}

3. Count positive numbers using a do-while loop:

#include <iostream>
using namespace std;

int main() {
int num, count = 0;

do {
cout << "Enter a number (negative to stop): ";
cin >> num;
if (num >= 0)
count++;
} while (num >= 0);

cout << "Count of positive numbers: " << count;


return 0;
}

4. Pyramid pattern using nested for loop:


#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;

for (int i = 1; i <= rows; ++i) {


for (int j = 1; j <= rows - i; ++j)
cout << " ";
for (int k = 1; k <= 2 * i - 1; ++k)
cout << "*";
cout << endl;
}

return 0;
}

5. Factorial using a for loop:

#include <iostream>
using namespace std;

int main() {
int n;
unsigned long long factorial = 1;
cout << "Enter a number: ";
cin >> n;

for (int i = 1; i <= n; ++i) {


factorial *= i;
}

cout << "Factorial of " << n << " = " << factorial;
return 0;
}

If you need any more programs or have any specific requests, let me know!

Below are the remaining C++ programs for arrays, structures, pointers, and classes &
objects.
Array, Structure, and Pointers

1. Input 10 integers in an array and display even and odd numbers separately:

#include <iostream>
using namespace std;

int main() {
int arr[10];

cout << "Enter 10 integers: ";


for (int i = 0; i < 10; i++) {
cin >> arr[i];
}

cout << "Even numbers: ";


for (int i = 0; i < 10; i++) {
if (arr[i] % 2 == 0)
cout << arr[i] << " ";
}

cout << "\nOdd numbers: ";


for (int i = 0; i < 10; i++) {
if (arr[i] % 2 != 0)
cout << arr[i] << " ";
}

return 0;
}

2. Structure for Book with title, author, and price:

#include <iostream>
using namespace std;

struct Book {
string title;
string author;
float price;
};

int main() {
Book books[3];
for (int i = 0; i < 3; i++) {
cout << "Enter details for book " << i + 1 << ":\n";
cout << "Title: ";
cin >> books[i].title;
cout << "Author: ";
cin >> books[i].author;
cout << "Price: ";
cin >> books[i].price;
}

cout << "\nBook Details:\n";


for (int i = 0; i < 3; i++) {
cout << "Book " << i + 1 << ": " << books[i].title << ", " << books[i].author << ", $" <<
books[i].price << endl;
}

return 0;
}

3. Swap two numbers using pointers:

#include <iostream>
using namespace std;

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;

swap(&x, &y);

cout << "After swapping: x = " << x << ", y = " << y;
return 0;
}

4. Sum of array elements using pointers:

#include <iostream>
using namespace std;

int main() {
int arr[5], sum = 0;
int *ptr = arr;

cout << "Enter 5 integers: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

for (int i = 0; i < 5; i++) {


sum += *(ptr + i);
}

cout << "Sum of elements: " << sum;


return 0;
}

5. Calculate average marks using a pointer:

#include <iostream>
using namespace std;

int main() {
int marks[5];
int *ptr = marks;
int sum = 0;

cout << "Enter marks of 5 students: ";


for (int i = 0; i < 5; i++) {
cin >> marks[i];
}

for (int i = 0; i < 5; i++) {


sum += *(ptr + i);
}

cout << "Average marks: " << sum / 5.0;


return 0;
}

Class & Object, Constructor Overloading, Function Overloading, Inheritance

1. Class Rectangle with constructor overloading:


#include <iostream>
using namespace std;

class Rectangle {
float length, breadth;

public:
Rectangle() : length(1), breadth(1) {}
Rectangle(float l, float b) : length(l), breadth(b) {}

float area() {
return length * breadth;
}

float perimeter() {
return 2 * (length + breadth);
}
};

int main() {
Rectangle r1;
Rectangle r2(5.5, 3.5);

cout << "Area of r1: " << r1.area() << ", Perimeter of r1: " << r1.perimeter() << endl;
cout << "Area of r2: " << r2.area() << ", Perimeter of r2: " << r2.perimeter() << endl;

return 0;
}

2. Calculator class with function overloading:

#include <iostream>
using namespace std;

class Calculator {
public:
int add(int a, int b) {
return a + b;
}

float add(float a, float b) {


return a + b;
}
string add(string a, string b) {
return a + b;
}
};

int main() {
Calculator calc;
cout << "Int add: " << calc.add(2, 3) << endl;
cout << "Float add: " << calc.add(2.5f, 3.5f) << endl;
cout << "String add: " << calc.add("Hello, ", "World!") << endl;

return 0;
}

3. Single inheritance with Person and Student classes:

#include <iostream>
using namespace std;

class Person {
public:
string name;
int age;
};

class Student : public Person {


public:
int rollNumber;
};

int main() {
Student s;
s.name = "John";
s.age = 20;
s.rollNumber = 101;

cout << "Student Name: " << s.name << ", Age: " << s.age << ", Roll Number: " <<
s.rollNumber;
return 0;
}

4. Multilevel inheritance with Vehicle, Car, and ElectricCar:

#include <iostream>
using namespace std;

class Vehicle {
public:
string brand;
};

class Car : public Vehicle {


public:
string model;
};

class ElectricCar : public Car {


public:
int batteryCapacity;
};

int main() {
ElectricCar eCar;
eCar.brand = "Tesla";
eCar.model = "Model S";
eCar.batteryCapacity = 100;

cout << "Brand: " << eCar.brand << ", Model: " << eCar.model << ", Battery Capacity: " <<
eCar.batteryCapacity << " kWh";
return 0;
}

5. BankAccount class with constructor overloading:

#include <iostream>
using namespace std;

class BankAccount {
public:
string accountHolder;
float balance;
string accountType;

BankAccount(string name, string type) : accountHolder(name), balance(0),


accountType(type) {}
BankAccount(string name, float bal, string type) : accountHolder(name), balance(bal),
accountType(type) {}

void display() {
cout << "Account Holder: " << accountHolder << ", Balance: $" << balance << ",
Account Type: " << accountType << endl;
}
};

int main() {
BankAccount acc1("Alice", "Savings");
BankAccount acc2("Bob", 1000.0f, "Checking");

acc1.display();
acc2.display();

return 0;
}

These programs cover the requested topics in C++. Let me know if you need any further
assistance!

You might also like