NAME:Vrutik Sankhesara 15934: Enroll/GR No
NAME:Vrutik Sankhesara 15934: Enroll/GR No
#include <iostream>
using namespace std;
int main() {
double a, b;
cout << "Enter two numbers separated by space: ";
cin >> a >> b;
double sum = a + b;
double avg = sum / 2;
cout << "Sum: " << sum << endl;
cout << "Average: " << avg << endl;
return 0;
}
Output:
Enter two numbers separated by space: 10 20
Sum: 30
Average: 15
int main() {
int a, b;
cout << "Enter two numbers separated by space: ";
cin >> a >> b;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
}
Output:
Enter two numbers separated by space: 5 10
After swapping: a = 10, b = 5
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is even." << endl;
else
cout << num << " is odd." << endl;
return 0;
}
Output:
Enter a number: 7
7 is odd.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers separated by space: ";
cin >> a >> b >> c;
cout << "The largest number is: " << largest << endl;
return 0;
}
Output:
Enter three numbers separated by space: 12 25 18
The largest number is: 25
Program 5: Write a single program that provides the
sum, difference, multiplication and division of two
numbers.
#include <iostream>
using namespace std;
int main() {
double a, b;
cout << "Enter two numbers separated by space: ";
cin >> a >> b;
if (b != 0)
cout << "Division: " << a / b << endl;
else
cout << "Division: Undefined (cannot divide by zero)" << endl;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number to print its table: ";
cin >> num;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
int num, i = 1;
cout << "Enter a number to print its table: ";
cin >> num;
do {
cout << num << " x " << i << " = " << num * i << endl;
i++;
} while (i <= 10);
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
int n, a = 0, b = 1, next;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: " << a << " " << b << " ";
for (int i = 2; i < n; i++) {
next = a + b;
cout << next << " ";
a = b;
b = next;
}
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0;
cout << "Enter a number to reverse: ";
cin >> num;
while (num != 0) {
int digit = num % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Build the reversed number
num /= 10; // Remove the last digit
}
Output:
Enter a number to reverse: 12345
Reversed Number: 54321
#include <iostream>
using namespace std;
int main() {
int num, i;
bool isPrime = true;
if (num <= 1) {
isPrime = false; // Numbers less than or equal to 1 are not prime
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}
Output:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long long binary;
int decimal = 0, base = 1, remainder;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i]; // Take input for each element
}
return 0;
}
Output:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "The length of the string is: " << str.length() << endl;
return 0;
}
Output:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str, reversedStr;
if (str == reversedStr) {
cout << "The string is a palindrome!" << endl;
} else {
cout << "The string is not a palindrome." << endl;
}
return 0;
}
Output:
Enter a string: madam
The string is a palindrome!
#include <iostream>
using namespace std;
int main() {
int rows, cols;
// Calculating Sum
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return 0;
}
Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of Matrix 1:
1 2
3 4
Enter elements of Matrix 2:
5 6
7 8
Sum of the matrices:
6 8
10 12
int main() {
int rows1, cols1, rows2, cols2;
// Multiplying matrices
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return 0;
}
Output:
Enter rows and columns for Matrix 1: 2 3
Enter rows and columns for Matrix 2: 3 2
Enter elements of Matrix 1:
1 2 3
4 5 6
Enter elements of Matrix 2:
7 8
9 10
11 12
Product of the matrices:
58 64
139 154
#include <iostream>
using namespace std;
int main() {
// Print ASCII values of digits
cout << "ASCII values of digits:" << endl;
for (char digit = '0'; digit <= '9'; digit++) {
cout << digit << ": " << int(digit) << endl;
}
return 0;
}
Output:
ASCII values of digits:
0: 48
1: 49
2: 50
3: 51
4: 52
5: 53
6: 54
7: 55
8: 56
9: 57
// Define a class
class Student {
public:
string name; // Data member to store name
int age; // Data member to store age
int main() {
Student s1; // Create an object of the Student class
return 0;
}
Output:
Enter student's name: John
Enter student's age: 20
Displaying Student Details:
Student's Name: John
Student's Age: 20
#include <iostream>
using namespace std;
// Define a class
class Student {
private:
string name;
int age;
public:
// Constructor to initialize data members
Student(string studentName, int studentAge) {
name = studentName;
age = studentAge;
}
int main() {
// Create an object of Student class and pass values to the
constructor
Student s1("John", 20);
// Display details using the object
cout << "Displaying Student Details:" << endl;
s1.displayDetails();
return 0;
}
Output:
#include <iostream>
using namespace std;
// Define a class
class Student {
private:
string name;
int age;
public:
// Constructor to initialize data members
Student(string studentName, int studentAge) {
name = studentName;
age = studentAge;
cout << "Constructor called: Object created and initialized." <<
endl;
}
int main() {
// Create an object of Student class
Student s1("Alice", 22);
Output:
Constructor called: Object created and initialized.
Displaying Student Details:
Student's Name: Alice
Student's Age: 22
Destructor called: Object destroyed.
#include <iostream>
using namespace std;
class Counter {
private:
static int count; // Static variable to keep track of object count
public:
// Constructor to increment count when an object is created
Counter() {
count++;
}
int main() {
Counter c1; // First object
c1.displayCount();
return 0;
}
Output:
Number of objects created: 1
Number of objects created: 2
Number of objects created: 3
class Swapper {
private:
int a, b;
public:
// Constructor to initialize numbers
Swapper(int num1, int num2) {
a = num1;
b = num2;
}
int main() {
int num1, num2;
return 0;
}
Output:
Enter two numbers:
5 10
Before swapping:
a: 5, b: 10
After swapping:
a: 10, b: 5
#include <iostream>
using namespace std;
class NumberPrinter {
private:
int n; // Member to store the upper limit
public:
// Constructor to initialize n
NumberPrinter(int limit) {
n = limit;
}
int main() {
int limit;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
// Calling the sum function for two integers
cout << "Sum of 10 and 20 (integers): " << sum(10, 20) << endl;
return 0;
}
Output:
Sum of 10 and 20 (integers): 30
Sum of 5.5 and 2.5 (floats): 8
Sum of 1, 2, and 3 (integers): 6
#include <iostream>
using namespace std;
// Function to calculate the area of a circle
float calculate(float radius) {
return 3.14159 * radius * radius;
}
int main() {
int choice;
cout << "Choose the shape to calculate area:" << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Triangle" << endl;
cin >> choice;
switch (choice) {
case 1: {
float radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "Area of the circle: " << calculate(radius) << endl;
break;
}
case 2: {
float length, breadth;
cout << "Enter the length and breadth of the rectangle: ";
cin >> length >> breadth;
cout << "Area of the rectangle: " << calculate(length,
breadth) << endl;
break;
}
case 3: {
float base, height;
cout << "Enter the base and height of the triangle: ";
cin >> base >> height;
cout << "Area of the triangle: " << calculate(base, height, 0)
<< endl;
break;
}
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
Output:
Choose the shape to calculate area:
1. Circle
2. Rectangle
3. Triangle
1
Enter the radius of the circle: 5
Area of the circle: 78.5398
class Factorial {
private:
int number;
public:
// Constructor to initialize the number
Factorial(int num) {
number = num;
}
int main() {
int num;
return 0;
}
Output:
Enter a number to calculate its factorial: 5
The factorial of 5 is: 120
Program 27 : Write a program to check whether a year is
leap year or not.
#include <iostream>
using namespace std;
int main() {
int year;
return 0;
}
Output:
Enter a year: 2000
2000 is a leap year.
#include <iostream>
#include <fstream> // Required for file handling
using namespace std;
int main() {
// Create an ofstream object to write to a file
ofstream outFile("data.txt");
return 0;
}
Output:
File 'data.txt' created successfully!
Data written to 'data.txt' successfully!
int main() {
string fileName = "output.txt"; // File name
string data; // Variable to store user input
return 0;
}
Output:
File 'output.txt' created successfully!
Enter data to write into the file: This is a sample text.
Data written to 'output.txt' successfully!
int main() {
string fileName = "output.txt"; // File to read from
string line; // Variable to store each line read
return 0;
}
Output:
This is a sample text.
File handling in C++ is simple.