0% found this document useful (0 votes)
30 views12 pages

C++ Programs With Output

The document contains multiple C++ programs demonstrating various programming concepts, including calculating factorials, checking even/odd numbers, determining prime numbers, performing arithmetic operations using switch cases, and implementing classes for banking operations. It also covers function overloading for area calculations, constructor overloading, Fibonacci series, stack and queue implementations, binary search, multiplication tables, bubble sort, palindrome checking, and string reversal. Each program includes code snippets along with example outputs.

Uploaded by

Rocking Jahangir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views12 pages

C++ Programs With Output

The document contains multiple C++ programs demonstrating various programming concepts, including calculating factorials, checking even/odd numbers, determining prime numbers, performing arithmetic operations using switch cases, and implementing classes for banking operations. It also covers function overloading for area calculations, constructor overloading, Fibonacci series, stack and queue implementations, binary search, multiplication tables, bubble sort, palindrome checking, and string reversal. Each program includes code snippets along with example outputs.

Uploaded by

Rocking Jahangir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Write a C++ program to find Factorial of given number

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int n, fact = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << "Factorial of " << n << " is " << fact;
getch();
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is 120

2. Write a C++ program to find whether a number is Even or Odd number

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int n;
cout << "Enter a number: ";
cin >> n;
if (n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
getch();
return 0;
}
Output:
Enter a number: 7
7 is odd.

Enter a number: 4

4 is even.

3. White a C++ program to find whether a number is Prime or not

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int n, i, flag = 0;
cout << "Enter a number: ";
cin >> n;
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << n << " is a prime number.";
else
cout << n << " is not a prime number.";
getch();
return 0;
}
Output:
Enter a number: 11
11 is a prime number.

Enter a number: 9

9 is not a prime number.

4.Write a C++ program to find addition, subtraction , multiplication, division


using Switch case.

#include <iostream.h>
#include <conio.h>
int main() {
clrscr();
int a, b, ch;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1: cout << "Sum = " << a + b; break;
case 2: cout << "Difference = " << a - b; break;
case 3: cout << "Product = " << a * b; break;
case 4: cout << "Division = " << a / b; break;
default: cout << "Invalid choice!";
}
getch();
return 0;
}
Output:
Enter two numbers: 20 5
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 3
Product = 100

5. write a C++ program t o represent a Class Bank with deposit, withdraw,


display function.

#include <iostream.h>
#include <conio.h>

class Bank {
int balance;
public:
void deposit(int amt) { balance += amt; }
void withdraw(int amt) { balance -= amt; }
void display() { cout << "Balance: " << balance << endl; }
};

int main() {
clrscr();
Bank b;
b.deposit(1000);
b.withdraw(200);
b.display();
getch();
return 0;
}
Output:
Balance: 800

6. Write a C++ program to find area of square,rectan,triangle using the concept


of function overloading.

#include <iostream.h>
#include <conio.h>

float area(float s)

return s * s;

}
float area(float l, float b)

return l * b;

}
float area(float b, float h, int)

return 0.5 * b * h;

int main() {
clrscr();
cout << "Area of square: " << area(5.0) << endl;
cout << "Area of rectangle: " << area(5.0, 6.0) << endl;
cout << "Area of triangle: " << area(4.0, 5.0, 1);
getch();
return 0;
}
Output:
Area of square: 25
Area of rectangle: 30
Area of triangle: 10

7. Write a C++ program to show the working of Constructor overloading.

#include <iostream.h>
#include <conio.h>

class Test {
int a, b;
public:
Test()

a = 0; b = 0;

}
Test(int x)

a = x; b = 0;

}
Test(int x, int y)

a = x;

b = y;

}
void display()

cout << "a=" << a << ", b=" << b << endl;

}
};
int main() {
clrscr();
Test t1, t2(5), t3(5, 10);
t1.display();
t2.display();
t3.display();
getch();
return 0;
}
Output:
a=0, b=0
a=5, b=0
a=5, b=10

8. Write a C++ program to implement Fibonacci series

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int n, a = 0, b = 1, c;
cout << "Enter number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; i++) {
cout << a << " ";
c = a + b;
a = b;
b = c;
}
getch();
return 0;
}
Output:
Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8

9. Write a C++ program to implement Stack .

#include <iostream.h>
#include <conio.h>
#define SIZE 5

class Stack {
int arr[SIZE], top;
public:
Stack()

top = -1;

}
void push(int x) {
if (top == SIZE - 1)
cout << "Stack Overflow";
else
arr[++top] = x;
}
void pop() {
if (top == -1)
cout << "Stack Underflow";
else
cout << "Popped: " << arr[top--] << endl;
}
void display() {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++)
cout << arr[i] << " ";
cout << endl;
}
};

int main() {
clrscr();
Stack s;
s.push(10);
s.push(20);
s.display();
s.pop();
s.display();
getch();
return 0;
}
Output:
Stack elements: 10 20
Popped: 20
Stack elements: 10

10. Write a C++ program to implement Queue.

#include <iostream.h>
#include <conio.h>
#define SIZE 5

class Queue {
int arr[SIZE], front, rear;
public:
Queue()

front = rear = -1;

}
void enqueue(int x) {
if (rear == SIZE - 1)
cout << "Queue Full";
else {
if (front == -1) front = 0;
arr[++rear] = x;
}
}
void dequeue()

{
if (front == -1 || front > rear)
cout << "Queue Empty";
else
cout << "Dequeued: " << arr[front++] << endl;
}
void display() {
cout << "Queue elements: ";
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
clrscr();
Queue q;
q.enqueue(10);
q.enqueue(20);
q.display();
q.dequeue();
q.display();
getch();
return 0;
}
Output:
Queue elements: 10 20
Dequeued: 10
Queue elements: 20

11. Write a C++ program to implement Binary search

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int arr[5] = {2, 4, 6, 8, 10};
int low = 0, high = 4, mid, key, flag = 0;
cout << "Enter element to search: ";
cin >> key;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
flag = 1;
break;
}
else if (key < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
if (flag)
cout << "Element found.";
else
cout << "Element not found.";
getch();
return 0;
}
Output:
Enter element to search: 8
Element found.

12.Write a C++ program to implement Multiplication table

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int n;
cout << "Enter number: ";
cin >> n;
for (int i = 1; i <= 10; i++) {
cout << n << " x " << i << " = " << n * i << endl;
}
getch();
return 0;
}
Output:
Enter number: 6
6x1=6
6 x 2 = 12
6 x 3 = 18

6 x 4 = 24

6 x 5= 30

6 x 6 = 36

6 x 7 = 42

6 x 8 = 48

6 x 9 = 54
6 x 10 = 60

13.Write a c++ program to implement Bubble sort.

#include <iostream.h>
#include <conio.h>

int main() {
clrscr();
int a[5] = {5, 1, 4, 2, 8};
int i, j, temp;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5 - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << "Sorted array: ";
for (i = 0; i < 5; i++)
cout << a[i] << " ";
getch();
return 0;
}
Output:
Sorted array: 1 2 4 5 8

14. Write a C++ program to find whether a String is palindrome or not.

#include <iostream.h>
#include <string.h>
#include <conio.h>

int main() {
clrscr();
char str[20], rev[20];
cout << "Enter string: ";
cin >> str;
strcpy(rev, str);
strrev(rev);
if (strcmp(str, rev) == 0)
cout << "Palindrome string.";
else
cout << "Not palindrome.";
getch();
return 0;
}
Output:
Enter string: level
Palindrome string.

Enter string: teacher

Not palindrome.

15. Write a C++ program to find reverse of a String.

#include <iostream.h>
#include <string.h>
#include <conio.h>

int main() {
clrscr();
char str[20];
cout << "Enter string: ";
cin >> str;
strrev(str);
cout << "Reversed string: " << str;
getch();
return 0;
}
Output:
Enter string: hello
Reversed string: olleh

You might also like