C++ File
C++ File
#include <iostream>
int main()
int num;
if (num % 2 == 0)
else
return 0;
}
1|Page
//A program to find larger of three numbers using if condition//
#include <iostream>
int main() {
int a, b, c;
int largest;
largest = a;
else
largest = b;
else
largest = c;
cout << "The largest number is: " << largest << endl;
return 0;
2|Page
//A program to find factorial of a number//
#include<iostream>
int main() {
int num;
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
factorial *= i;
cout << "Factorial of " << num << " is " << factorial << endl;
return 0;
3|Page
//A program to find a palindrome number//
#include <iostream>
int main() {
original = num;
while (num != 0) {
num /= 10;
if (original == reversed)
else
4|Page
//A program to find Fibonacci series for a number using recursion//
#include <iostream>
int fibonacci(int n) {
if (n <= 1)
return n;
int main() {
int terms;
cout << "Enter number of terms: ";
return 0;
5|Page
//A program of arithmetic operators//
#include <iostream>
int main() {
int a, b;
return 0;
6|Page
//A program for scope resolution operators//
#include <iostream>
using namespace
int main()
int num;
cout << "Global variable using scope resolution operator: " << ::num
endl; return 0;
7|Page
//A program for ternary operators//
#include <iostream>
int main()
int a b;
cout << "The greater number is: " << max << endl;
return 0;
8|Page
//A program to find highest of two numbers using if-else condition//
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout << "Enter two numbers: ";
cin >> a >> b;
if (a > b)
cout << a << " is greater." << endl;
else if (b > a)
cout << b << " is greater." << endl;
else
cout << "Both numbers are equal." << endl;
9| P a g e
//A program to find sum and average of ten different numbers using for loop//
#include <iostream>
using namespace std;
int main()
{
int num, sum = 0;
float average;
for (int i = 1; i <= 10; i++)
{
cout << "Enter number " << i << ": ";
cin >> num;
sum += num;
}
average = sum / 10.0;
cout << "Sum = " << sum << endl;
cout << "Average = " << average << endl;
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;
10| P a g e
//A program to find sum and average of ten different numbers using while loop//
#include <iostream>
using namespace std;
int main()
{
int i = 1, num, sum = 0;
float average;
while (i <= 4) {
cout << "Enter number : " << i << endl;
cin >> num;
sum += num;
i++;
}
average = sum / 4.0;
cout << "Sum = " << sum << endl;
cout << "Average = " << average << endl;
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;
11 | P a g e
//A program to find sum and average of ten different numbers using do-while loop//
#include <iostream>
using namespace std;
int main()
12 | P a g e
//A program to implement string function//
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
cout << "Enter first string: ";
getline(cin, str1);
cout << "Enter second string: ";
getline(cin,str2);
cout << "Length of first string: " << str1.length() << endl;
if (str1.compare(str2) == 0)
cout << "Strings are equal." << endl;
else
cout << "Strings are not equal." << endl;
13 | P a g e
//A program for matrix multiplication//
#include <iostream>
using namespace std;
int main() {
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2;
cout << "Enter rows and columns of first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns of second matrix: ";
cin >> r2 >> c2;
if (c1 != r2) {
cout << "Matrix multiplication not possible!" << endl;
return 0;}
cout << "Enter elements of first matrix:\n";
for (int i = 0; i < r1; ++i)
for (int j = 0; j < c1; ++j)
cin >> a[i][j];
}
}
cout << "Resultant matrix:\n";
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
cout << result[i][j] << " ";}
cout << endl;}
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;}
14 | P a g e
//A program of two numbers without using 3rd variable//
#include <iostream>
using namespace std;
int main(){
15 | P a g e
//A program of two numbers using 3rd variable//
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping:\n";
cout << "a = " << a << "\nb = " << b << endl;
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;
16 | P a g e
//A program to illustrate the use of friend function//
#include <iostream>
using namespace std;
class Box {
int length;
public:
Box() : length(0) {}
void setLength(int l) {
length = l;
}
friend void printLength(Box);
};
void printLength(Box b) {
cout << "Length of box: " << b.length << endl;
}
int main() {
Box b;
b.setLength(15);
printLength(b);
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
18 | P a g e
//A program to illustrate static data member and static member function//
#include <iostream>
using namespace std;
class Box {
int length;
public:
Box() : length(0) {}
void setLength(int l) {
length = l;
}
friend void printLength(Box);
};
void printLength(Box b) {
cout << "Length of box: " << b.length << endl;
}
int main() {
Box b;
b.setLength(15);
printLength(b);
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
18 | P a g e
//A program for inline function//
#include<iostream>
}
};
int main() {
Math m;
int num;
cout << "Enter a number to find its square: ";
cout << "Square of " << num << " is: " << m.square(num) <<endl;
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
19 | P a g e
//A program for parameterized constructor and destructor //
#include <iostream>
using namespace std;
class Student {
string name;
int age;
public:
Student(string n, int a) {
name = n;
age = a;
cout << "Constructor called!" << endl;
}
~Student() {
cout << "Destructor called!" << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s("Abhinav", 19);
s.display();
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
20 | P a g e
//A program to illustrate static data member and static data function//
#include <iostream>
using namespace std;
class Counter {
static int count;
public:
Counter() {
count++;
}
static void showCount() {
cout << "Total objects created: " << count << endl;
}
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
Counter::showCount();
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
21 | P a g e
//A program to print the stars structure as given in index//
#include <iostream>
using namespace std;
int main() {
int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int space = 1; space <= rows - i; space++) {
cout << " ";
}
for (int star = 1; star <= i; star++) {
cout << "* ";
}
cout << endl;
}
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
22 | P a g e
//A program for print using nested loops//
#include <iostream>
using namespace std;
int main() {
int rows = 4;
} else {
if (i == rows && j == i) {
cout << " +";
} else {
cout << "+";
}
}
}
cout << endl;
}
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
23 | P a g e
//A program to print the number pattern//
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int space = 1; space <= rows - i; space++) {
cout << " "; }
if (j == 5) {
cout << " ";
}
cout << j; }
}
cout << j;
}
cout << endl;
}
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
24| P a g e
//A program operator overloading//
#include <iostream>
using namespace std;
class Number {
int value;
public:
Number(int v = 0) {
value = v;
}
Number operator+(Number obj) {
Number temp;
temp.value = value + obj.value;
return temp;
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Number a(5), b(10);
Number c = a + b;
c.display();
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
25 | P a g e
//A program for function overloading//
#include <iostream>
using namespace std;
class Print {
public:
void show(int i) {
cout << "Integer: " << i << endl;
}
void show(double d) {
cout << "Double: " << d << endl;
}
void show(const string& s) {
cout << "String: " << s << endl;
}
};
int main() {
Print obj;
obj.show(5);
obj.show(3.14);
obj.show("Function Overloading");
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
26 | P a g e
//A program for function overriding//
#include <iostream>
using namespace std;
class Animal {
public:
void sound()
{
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound()
{
cout << "Dog barks" << endl;
}
};
int main() {
Dog d;
d.sound();
cout << "Name:Abhinav" << endl;
cout << "Roll No:" << 1240064057 << endl;
return 0;
27 | P a g e
Index
Sr. Program name Page no. Remarks
No.
*
**
***
****
*
*+
*++
*++ +
24. A program to print of the form: 24
1
121
12 32 1
12 343 21
12 34 543 21