Opp PRCDD
Opp PRCDD
1.Program to take two numbers and print their sum and average
Code: -
#include <iostream>
int main() {
avg = sum / 2;
return 0;
Output: -
int main() {
int a, b
cout << "Before swapping: a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
Output: -
int main() {
int num;
if (num % 2 == 0)
else
return 0;
Output: -
int main() {
largest = num2;
largest = num3;
cout << "The largest number is: " << largest << endl;
return 0;
Output: -
int main() {
else
return 0;
Output: -
int main() {
int num;
cout << "Table of " << num << ":" << endl;
cout << num << " x " << i << " = " << num * i << endl;
return 0;
Output: -
7. Program to print the table of a number using do-while loop.
Code: -
#include <iostream>
int main() {
int num, i = 1;
cout << "Table of " << num << ":" << endl;
do {
cout << num << " x " << i << " = " << num * i << endl;
i++;
return 0;
Output: -
8.Write a program to print Fibonacci Series (0, 1, 1, 2, 3, 5, 8,
13, 21...).
Code: -
#include <iostream>
int main() {
int n;
cout << "Enter the number of terms for Fibonacci series: ";
cin >> n;
int a = 0, b = 1, c;
c = a + b;
a = b;
b = c;
}
return 0;
Output: -
int main() {
while (num != 0) {
num /= 10;
return 0;
Output: -
10. Write a program to check whether a number is a prime
number or not.
Code: -
#include <iostream>
int main() {
int num;
if (num <= 1) {
cout << num << " is not a prime number." << endl;
return 0;
if (num % i == 0) {
isPrime = false;
break;
if (isPrime)
else
cout << num << " is not a prime number." << endl;
return 0;
Output: -
11. Write a program to convert binary number to decimal
number.
Code: -
#include <iostream>
#include <cmath>
int main() {
while (binary != 0) {
binary /= 10;
i++;
return 0;
Output: -
int main() {
int size;
cout << "Enter " << size << " elements: ";
return 0;
Output: -
int main() {
string str;
cout << "Length of the string: " << str.length() << endl;
return 0;
}
Output: -
#include <string>
int main() {
string str;
int start = 0;
if (str[start] != str[end]) {
isPalindrome = false;
break;
start++;
end--;
if (isPalindrome)
else
return 0;
}
Output: -
int main() {
}
return 0;
Output: -
int main() {
cout << "Enter number of rows and columns for second matrix: ";
if (col1 != row2) {
return 0;
product[i][j] = 0;
return 0;
}
Output: - -
int main() {
return 0;
Output: -
18. Write a program to demonstrate the use of class and object.
Code: -
#include <iostream>
class Rectangle {
public:
length = l;
width = w;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
Rectangle rect;
rect.setDimensions(5, 10);
rect.display();
return 0;
Output: -
class Rectangle {
public:
Rectangle(int l, int w) {
length = l;
width = w;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
rect.display();
return 0;
Output: -
class Rectangle {
public:
Rectangle(int l, int w) {
length = l;
width = w;
~Rectangle() {
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
rect.display();
return 0;
Output: -
class Counter {
public:
Counter() {
count++;
}
static void displayCount() {
};
int Counter::count = 0;
int main() {
Counter::displayCount();
Counter c3;
Counter::displayCount();
return 0;
Output: -
class Swap {
public:
int temp = a;
a = b;
b = temp;
};
int main() {
int a = 5, b = 10;
Swap swapObj;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
swapObj.swapNumbers(a, b);
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
Output: -
class NumberPrinter {
public:
void printNumbers(int n) {
};
int main() {
NumberPrinter printer;
int n;
cin >> n;
printer.printNumbers(n);
return 0;
Output: -
class Calculator {
public:
return a + b;
return a + b;
return a + b + c;
};
int main() {
Calculator calc;
cout << "Sum of doubles: " << calc.sum(3.5, 4.5) << endl;
cout << "Sum of three integers: " << calc.sum(1, 2, 3) << endl;
return 0;
}
Output: -
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
int s,l,b;
float r,bs,ht;
cin>>s;
cin>>l>>b;
cin>>r;
cin>>bs>>ht;
}
int area(int s)
return(s*s);
return(l*b);
float area(float r)
return(3.14*r*r);
return((bs*ht)/2);
Output: -
26. Write a program to print factorial of a given number using
class.
Code: -
#include <iostream>
class Factorial {
public:
int calculate(int n) {
if (n == 0 || n == 1) return 1;
};
int main() {
Factorial fact;
int num;
cout << "Factorial of " << num << " is: " << fact.calculate(num) << endl;
return 0;
Output: -
int main() {
int year;
else
cout << year << " is not a leap year." << endl;
return 0;
Output: -
#include <fstream>
int main() {
ofstream outfile("data.txt");
if (outfile.is_open()) {
outfile.close();
} else {
return 0;
}
Output: -
#include <fstream>
int main() {
ofstream outfile("data.txt");
if (outfile.is_open()) {
outfile << "Hello, this is the first line in the file." << endl;
outfile << "This is the second line of the file." << endl;
outfile << "C++ file handling is easy and powerful!" << endl;
outfile.close();
} else {
cout << "Unable to open the file for writing." << endl;
return 0;
Output: -
30. Write a program to retrieve/read data from a text file.
Code: -
#include <iostream>
#include <fstream>
int main() {
string line;
ifstream infile("data.txt");
if (infile.is_open()) {
infile.close();
} else {
cout << "Unable to open the file for reading." << endl;
return 0;
Output: -
*-*End*-*