Question 1:
Write a program in C++ to find max of two numbers using if-else control structure.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
if (a > b)
cout << "Maximum: " << a;
else
cout << "Maximum: " << b;
getch();
}
Question 2:
Write a program in C++ to find maximum and minimum of two numbers using conditional
operator.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
int max = (a > b) ? a : b;
int min = (a < b) ? a : b;
cout << "Maximum: " << max << "\nMinimum: " << min;
getch();
}
Question 3:
Write a program in C++ to find if the given year is leap year or not using if-else control
structure.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << year << " is a Leap Year.";
else
cout << year << " is not a Leap Year.";
getch();
}
Question 4:
Write a program in C++ to find factorial of a given number using for loop.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int n, fact = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++)
fact *= i;
cout << "Factorial: " << fact;
getch();
}
Question 5:
Write a program in C++ to print the sum of first 100 natural numbers using for control
structure.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int sum = 0;
for (int i = 1; i <= 100; i++)
sum += i;
cout << "Sum of first 100 natural numbers: " << sum;
getch();
}
Question 6:
Write a program in C++ to print first 15 terms of Fibonacci series.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int n1 = 0, n2 = 1, n3;
cout << "Fibonacci Series: " << n1 << " " << n2 << " ";
for (int i = 2; i < 15; i++) {
n3 = n1 + n2;
cout << n3 << " ";
n1 = n2;
n2 = n3;
}
getch();
}
Question 7:
Write a program in C++ to read an array of 10 elements and print its sum.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int arr[10], sum = 0;
cout << "Enter 10 elements: ";
for (int i = 0; i < 10; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "Sum of elements: " << sum;
getch();
}
Question 8:
Write a program in C++ to find factorial of a given number using function void fact(int).
Code:
#include <iostream.h>
#include <conio.h>
void fact(int n) {
int factorial = 1;
for (int i = 1; i <= n; i++)
factorial *= i;
cout << "Factorial: " << factorial;
}
void main() {
clrscr();
int num;
cout << "Enter a number: ";
cin >> num;
fact(num);
getch();
}
Question 9:
Write an Object Oriented Program in C++ to implement inventory class to calculate total price
of number of items purchased.
Code:
#include <iostream.h>
#include <conio.h>
class Inventory {
int quantity;
float price;
public:
void getData() {
cout << "Enter quantity and price per item: ";
cin >> quantity >> price;
}
void showTotal() {
cout << "Total price: " << (quantity * price);
}
};
void main() {
clrscr();
Inventory item;
item.getData();
item.showTotal();
getch();
}
Question 10:
Write an Object Oriented Program in C++ to find the GCD of two given numbers.
Code:
#include <iostream.h>
#include <conio.h>
class GCD {
public:
int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
};
void main() {
clrscr();
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
GCD obj;
cout << "GCD: " << obj.findGCD(num1, num2);
getch();
}
Question 11:
Implement a class fact to find the factorial of a given number.
Code:
#include <iostream.h>
#include <conio.h>
class Fact {
public:
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
return fact;
}
};
void main() {
clrscr();
int num;
cout << "Enter a number: ";
cin >> num;
Fact obj;
cout << "Factorial: " << obj.factorial(num);
getch();
}
Question 12:
Write an Object Oriented Program in C++ to implement circle class to find area and
circumference of a circle using functions void area(), void circum().
Code:
#include <iostream.h>
#include <conio.h>
class Circle {
float radius;
public:
void getData() {
cout << "Enter radius: ";
cin >> radius;
}
void area() {
cout << "Area: " << (3.14 * radius * radius);
}
void circum() {
cout << "Circumference: " << (2 * 3.14 * radius);
}
};
void main() {
clrscr();
Circle c;
c.getData();
c.area();
c.circum();
getch();
}
Question 13:
Write a program in C++ to find the number of occurrences of character 'a' in the given string.
Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main() {
clrscr();
char str[100];
char ch = 'a';
int count = 0;
cout << "Enter a string: ";
cin >> str;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ch)
count++;
}
cout << "Occurrences of 'a': " << count;
getch();
}
Question 14:
Write a program in C++ to reverse a given string.
Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main() {
clrscr();
char str[100], rev[100];
cout << "Enter a string: ";
cin >> str;
strcpy(rev, strrev(str));
cout << "Reversed string: " << rev;
getch();
}
Question 15:
Implement class temperature to convert degree Fahrenheit (F) to degree Celsius (C). Using
formula C = (F - 32) / 9 * 5.
Code:
#include <iostream.h>
#include <conio.h>
class Temperature {
public:
float toCelsius(float f) {
return (f - 32) / 9 * 5;
}
};
void main() {
clrscr();
float fahrenheit;
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheit;
Temperature temp;
cout << "Temperature in Celsius: " << temp.toCelsius(fahrenheit);
getch();
}
Question 16:
Write a program in C++ to find the largest number in an array of 10 integers.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int arr[10], max;
cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++)
cin >> arr[i];
max = arr[0];
for (int i = 1; i < 10; i++) {
if (arr[i] > max)
max = arr[i];
}
cout << "Largest number: " << max;
getch();
}
Question 17:
Write a program in C++ to check if the given number is a prime number.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int num, i, flag = 0;
cout << "Enter a number: ";
cin >> num;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << "Prime Number";
else
cout << "Not a Prime Number";
getch();
}
Question 18:
Write a program in C++ to calculate x^y using the function void power(int, int).
Code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void power(int x, int y) {
cout << "Result: " << pow(x, y);
}
void main() {
clrscr();
int base, exponent;
cout << "Enter base and exponent: ";
cin >> base >> exponent;
power(base, exponent);
getch();
}
Question 19:
Write a program in C++ to count the number of words in a line of text.
Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main() {
clrscr();
char str[100];
int count = 1;
cout << "Enter a line of text: ";
cin.getline(str, 100);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ')
count++;
}
cout << "Number of words: " << count;
getch();
}
Question 20:
Write a program in C++ to swap two integers using function void swap(int &, int &).
Code:
#include <iostream.h>
#include <conio.h>
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void main() {
clrscr();
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
swap(x, y);
cout << "After swapping: " << x << " " << y;
getch();
}
Question 21:
Write an Object Oriented Program in C++ to implement circle class to find area and
circumference of a circle using functions void area(), void circum().
Code:
#include <iostream.h>
#include <conio.h>
class Circle {
float radius;
public:
void getData() {
cout << "Enter radius: ";
cin >> radius;
}
void area() {
cout << "Area: " << (3.14 * radius * radius);
}
void circum() {
cout << "Circumference: " << (2 * 3.14 * radius);
}
};
void main() {
clrscr();
Circle c;
c.getData();
c.area();
c.circum();
getch();
}
Question 22:
Write a program in C++ to find the number of occurrences of character 'a' in the given string.
Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main() {
clrscr();
char str[100];
char ch = 'a';
int count = 0;
cout << "Enter a string: ";
cin >> str;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ch)
count++;
}
cout << "Occurrences of 'a': " << count;
getch();
}
Question 23:
Write a program in C++ to reverse a given string.
Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main() {
clrscr();
char str[100], rev[100];
cout << "Enter a string: ";
cin >> str;
strcpy(rev, strrev(str));
cout << "Reversed string: " << rev;
getch();
}
Question 24:
Implement class temperature to convert degree Fahrenheit (F) to degree Celsius (C). Using
formula C = (F - 32) / 9 * 5.
Code:
#include <iostream.h>
#include <conio.h>
class Temperature {
public:
float toCelsius(float f) {
return (f - 32) / 9 * 5;
}
};
void main() {
clrscr();
float fahrenheit;
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheit;
Temperature temp;
cout << "Temperature in Celsius: " << temp.toCelsius(fahrenheit);
getch();
}
Question 25:
Write a program in C++ to find the largest number in an array of 10 integers.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int arr[10], max;
cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++)
cin >> arr[i];
max = arr[0];
for (int i = 1; i < 10; i++) {
if (arr[i] > max)
max = arr[i];
}
cout << "Largest number: " << max;
getch();
}
Question 26:
Write a program in C++ to check if the given number is a prime number.
Code:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int num, i, flag = 0;
cout << "Enter a number: ";
cin >> num;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << "Prime Number";
else
cout << "Not a Prime Number";
getch();
}
Question 27:
Write a program in C++ to calculate x^y using the function void power(int, int).
Code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void power(int x, int y) {
cout << "Result: " << pow(x, y);
}
void main() {
clrscr();
int base, exponent;
cout << "Enter base and exponent: ";
cin >> base >> exponent;
power(base, exponent);
getch();
}
Question 28:
Write a program in C++ to count the number of words in a line of text.
Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main() {
clrscr();
char str[100];
int count = 1;
cout << "Enter a line of text: ";
cin.getline(str, 100);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ')
count++;
}
cout << "Number of words: " << count;
getch();
}
Question 29:
Write a program in C++ to swap two integers using function void swap(int &, int &).
Code:
#include <iostream.h>
#include <conio.h>
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void main() {
clrscr();
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
swap(x, y);
cout << "After swapping: " << x << " " << y;
getch();
}