0% found this document useful (0 votes)
6 views29 pages

C++ File

The document contains multiple C++ programs demonstrating various programming concepts such as checking odd/even numbers, finding the largest of three numbers, calculating factorials, checking for palindromes, generating Fibonacci series, performing arithmetic operations, and implementing matrix multiplication. Each program includes user input and outputs results along with the author's name and roll number. The document serves as a comprehensive guide to basic programming techniques in C++.

Uploaded by

a78646112
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)
6 views29 pages

C++ File

The document contains multiple C++ programs demonstrating various programming concepts such as checking odd/even numbers, finding the largest of three numbers, calculating factorials, checking for palindromes, generating Fibonacci series, performing arithmetic operations, and implementing matrix multiplication. Each program includes user input and outputs results along with the author's name and roll number. The document serves as a comprehensive guide to basic programming techniques in C++.

Uploaded by

a78646112
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/ 29

//A program to check whether it is odd or even//

#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;

cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 << endl;

return 0;
}

1|Page
//A program to find larger of three numbers using if condition//

#include <iostream>

using namespace std;

int main() {

int a, b, c;

cout << "Enter three numbers: ";

cin >> a >> b >> c;

int largest;

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

largest = a;

else

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

largest = b;

else

largest = c;

cout << "The largest number is: " << largest << endl;

cout <<" Name:Abhinav" << endl;

cout<<"RollNo:"<<1240064057 << endl;

return 0;

2|Page
//A program to find factorial of a number//

#include<iostream>

using namespace std;

int main() {

int num;

long long factorial = 1;

cout << "Enter a number: ";

cin >> num;

if (num < 0) {

cout << "Factorial is not defined for negative numbers." << endl;

} else {

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

factorial *= i;

cout << "Factorial of " << num << " is " << factorial << endl;

cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 << endl;

return 0;

3|Page
//A program to find a palindrome number//

#include <iostream>

using namespace std;

int main() {

int num, original, reversed = 0, digit;

cout << "Enter a number: ";

cin >> num;

original = num;

while (num != 0) {

digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;

if (original == reversed)

cout << original << " is a Palindrome." << endl;

else

cout << original << " is not a Palindrome." << endl;

cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 << endl;


return 0;

4|Page
//A program to find Fibonacci series for a number using recursion//

#include <iostream>

using namespace std;

int fibonacci(int n) {

if (n <= 1)

return n;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int terms;
cout << "Enter number of terms: ";

cin >> terms;

cout << "Fibonacci Series: ";

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

cout << fibonacci(i) << " ";

cout << endl;

cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 << endl;

return 0;

5|Page
//A program of arithmetic operators//

#include <iostream>

using namespace std;

int main() {

int a, b;

cout << "Enter two numbers: ";

cin >> a >> b;

cout << "Addition: " << a + b << endl;

cout << "Subtraction: " << a - b << endl;

cout << "Multiplication: " << a * b << endl;

cout << "Division: " << a / b << endl;

cout << "Modulus: " << a % b << endl;

cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 << endl;

return 0;

6|Page
//A program for scope resolution operators//

#include <iostream>

using namespace

std; int num = 100;

int main()

int num;

int num = 50;

cout << "Local variable: " << num <<


endl;

cout << "Global variable using scope resolution operator: " << ::num

<< endl; cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 <<

endl; return 0;

7|Page
//A program for ternary operators//

#include <iostream>

using namespace std;

int main()

int a b;

cout << "Enter two numbers: "; cin >> a >> b;

int max = (a > b) ? a : b;

cout << "The greater number is: " << max << endl;

cout <<" Name:Abhinav" << endl;

cout << " Roll No:" << 1240064057 << 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;

cout <<" Name:Abhinav" << endl;


cout << " Roll No:" << 1240064057 << endl;
return 0;
}

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()

int i = 1, num, sum = 0;


float average;
do {
cout << "Enter number " << i << ": ";
cin >> num;
sum += num;
i++;

} while (i <= 4);


average = sum / 4.0;
cout << "Sum = " << sum << endl;
cout << "Average = " << average << endl;
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;

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;

string combined = str1.append(" " + str2);


cout << "Combined string: " << combined << endl;
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;

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 << "Enter elements of second matrix:\n";


for (int i = 0; i < r2; ++i)
for (int j = 0; j < c2; ++j)
cin >> b[i][j];

for (int i = 0; i < r1; ++i)


for (int j = 0; j < c2; ++j)
result[i][j] = 0;

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


for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {

result[i][j] += a[i][k] * b[k][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(){

int a,b, temp;


cout << "Enter two numbers: ";
cin >> a >> b;
temp = a;
a = b;
b = temp;
cout << "After swapping:\n";
cout << "a = " << a << "\nb = " << b << endl;
cout <<" Name:Abhinav" << endl;
cout << " Roll No:" << 1240064057 << endl;
return 0;

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>

using namespace std;


class Math {
public:
inline int square(int x) {
return x * x;

}
};
int main() {
Math m;
int num;
cout << "Enter a number to find its square: ";

cin >> num;

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;

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


for (int j = 1; j <= i; j++) {
if (j == 1) {
cout << "*";

} 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 << " "; }

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


if (j == 3) {
cout << " "; }

if (j == 5) {
cout << " ";

}
cout << j; }

for (int j = i - 1; j >= 1; j--) {


if (j == 2) {
cout << " ";

}
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.

22. A program to print the following: 22

*
**
***
****

23. A program for print using nested loops: 23

*
*+
*++
*++ +
24. A program to print of the form: 24

1
121
12 32 1
12 343 21
12 34 543 21

25. A program for operator overloading. 25

26. A program for function overloading. 26

27. A program for function overriding. 27

28. A note on storage classes (not program).

29. A note on inheritance (not program).

30. A note on polymorphism (not program).


Index
Sr. Program name Page no. Remarks
no.
1. A program to check whether it is odd or 1
even.
2. A program to find larger of three numbers 2
using if condition.
3. A program to find factorial of a number. 3
4. A program to find a palindrome number. 4
5. A program to find Fibonacci series for a 5
Number using recursion.
6. A program of arithmetic operators. 6
7. A program for scope resolution operators. 7
8. A program for ternary operators. 8
9. A program to find highest of two numbers 9
using if-else condition.
10. A program to find sum and average of ten 10
different numbers using for loop.
11. A program to find sum and average of ten 11
different numbers using while loop.

12. A program to find sum and average of ten 12


different numbers using do-while loop.
13. A program to implement string function. 13

14. A program for matrix multiplication. 14

15. A program of two numbers without using 15


3rd variable.
16. A program of two numbers using 3rd 16
variable.
17. A program to enter and display the record 17
of a book using all the member functions
inside the class.
18. A program to illustrate the use of friend 18
function.
19. A program for inline function. 19

20. A program for parameterized constructor 20


and destructor.
21. A program to illustrate static data member 21
and static member function.

You might also like