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

c++.23pdf

The document contains multiple C++ programs demonstrating various programming concepts such as input/output, control structures, functions, classes, and basic algorithms. Each program includes a prompt for user input, performs specific operations (like calculating factorials, finding the largest number, or performing arithmetic operations), and outputs results along with the author's name and roll number. The programs also illustrate different programming techniques including recursion, matrix operations, and object-oriented programming.

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)
8 views29 pages

c++.23pdf

The document contains multiple C++ programs demonstrating various programming concepts such as input/output, control structures, functions, classes, and basic algorithms. Each program includes a prompt for user input, performs specific operations (like calculating factorials, finding the largest number, or performing arithmetic operations), and outputs results along with the author's name and roll number. The programs also illustrate different programming techniques including recursion, matrix operations, and object-oriented programming.

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

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

return 0;

2|Page
#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
#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
#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
#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
#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
#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
#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|Page
#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
#include <iostream>

using namespace std;

int main() {

int i = 1, num, sum = 0;

float average;

while (i <= 4) {

cout << "Enter number " << i << ": ";

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
#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
#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
#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) {

14 | P a g e
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;

15 | P a g e
#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;

16 | P a g e
#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;

17 | P a g e
#include <iostream>

using namespace std;

class Book {

string title;

string author;

float price;

public:

void input() {

cout << "Enter book title: ";

getline(cin, title);

cout << "Enter author name: ";

getline(cin, author);

cout << "Enter book price: ";

cin >> price;

void display() {

cout << "\nBook Title: " << title << endl;

cout << "Author: " << author << endl;

cout << "Price:₨" << price << endl;

};

int main() {

Book b;

b.input();

b.display();

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

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

18 | P a g e
return 0;

19 | P a g e
#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;

#include <iostream>

20 | P a g e
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;

21 | P a g e
#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;

22 | P a g e
#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;

23 | P a g e
#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;

24 | P a g e
#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;

25 | P a g e
#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;

26 | P a g e
#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;

27 | P a g e
#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;

28 | P a g e
#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;

29 | P a g e

You might also like