Piggy
Piggy
#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;
}
#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;
}
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char op;
float 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;
}
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
return 0;
}
Loops
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
#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;
}
#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);
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
unsigned long long factorial = 1;
cout << "Enter a number: ";
cin >> n;
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];
return 0;
}
#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;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
swap(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arr[5], sum = 0;
int *ptr = arr;
#include <iostream>
using namespace std;
int main() {
int marks[5];
int *ptr = marks;
int sum = 0;
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;
}
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int 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;
}
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
};
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;
}
#include <iostream>
using namespace std;
class Vehicle {
public:
string brand;
};
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;
}
#include <iostream>
using namespace std;
class BankAccount {
public:
string accountHolder;
float balance;
string accountType;
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!