Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Problems Solving in C++

The document outlines a series of C++ programming problems and solutions for practical examinations, including programs for mathematical calculations, geometry, comparisons, and sorting. It provides detailed code examples for various tasks such as calculating interest, solving quadratic equations, and finding GCD/LCM. The document serves as a comprehensive guide for students to practice and understand fundamental programming concepts.

Uploaded by

inamalivlogs
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 views24 pages

Problems Solving in C++

The document outlines a series of C++ programming problems and solutions for practical examinations, including programs for mathematical calculations, geometry, comparisons, and sorting. It provides detailed code examples for various tasks such as calculating interest, solving quadratic equations, and finding GCD/LCM. The document serves as a comprehensive guide for students to practice and understand fundamental programming concepts.

Uploaded by

inamalivlogs
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/ 24

Important programs for practical examination

1. C++ program to convert Fahrenheit to Celsius


2. Find area of rectangle and area of triangle
3. Solved quadratic equation
4. Write a program to print grades of students
5. Write a program to print electricity bill
6. Write a program user input number and print table of the number
7. Write a program to input three number and find which number is greater
8. Write a program to input number and find number is even or odd
9. Write a program to input number a find number is prime
10. Write a program to print Fibonacci series
11. Write a program to find the factorial of a number
12. Write a program to print series 1,2,4,6,8 and add final total
13. Write a program to input a numbers and reverse the numbers

Solution List of practical given in modal papers

Problem 1

#include <iostream>

using namespace std;

void calculateInterest() {

float principal, rate, time, interest;

cout << "Enter Principal amount: ";

cin >> principal;

cout << "Enter Rate (%): ";

cin >> rate;

cout << "Enter Time (years): ";

cin >> time;

interest = (principal * rate * time) / 100;

cout << "Simple Interest = " << interest << endl;

}
void calculatePercentage() {

float obtained, total, percent;

cout << "Enter obtained marks: ";

cin >> obtained;

cout << "Enter total marks: ";

cin >> total;

percent = (obtained / total) * 100;

cout << "Percentage = " << percent << "%" << endl;

void calculateAverage() {

int n;

cout << "Enter number of values: ";

cin >> n;

float value, sum = 0;

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

cout << "Enter value " << i << ": ";

cin >> value;

sum += value;

cout << "Average = " << sum / n << endl;

void calculateRatio() {

float a, b;

cout << "Enter first number: ";

cin >> a;

cout << "Enter second number: ";

cin >> b;

float ratio = a / b;

cout << "Ratio (a:b) = " << a << ":" << b << " or " << ratio << ":1" << endl;
}

void calculateGrade() {

float marks;

cout << "Enter percentage: ";

cin >> marks;

if (marks >= 90)

cout << "Grade: A+" << endl;

else if (marks >= 80)

cout << "Grade: A" << endl;

else if (marks >= 70)

cout << "Grade: B" << endl;

else if (marks >= 60)

cout << "Grade: C" << endl;

else if (marks >= 50)

cout << "Grade: D" << endl;

else

cout << "Grade: F (Fail)" << endl;

int main() {

int choice;

do {

cout << "\n=== Arithmetic Problem Solver ===\n";

cout << "1. Calculate Interest\n";

cout << "2. Calculate Percentage\n";

cout << "3. Calculate Average\n";

cout << "4. Calculate Ratio\n";

cout << "5. Determine Grade\n";

cout << "0. Exit\n";

cout << "Enter your choice: ";


cin >> choice;

switch (choice) {

case 1: calculateInterest(); break;

case 2: calculatePercentage(); break;

case 3: calculateAverage(); break;

case 4: calculateRatio(); break;

case 5: calculateGrade(); break;

case 0: cout << "Exiting program." << endl; break;

default: cout << "Invalid choice!" << endl;

} while (choice != 0);

return 0;

Problem 2

#include <iostream>

#include <cmath>

using namespace std;

const float PI = 3.14159;

void areaCircle() {

float radius;

cout << "Enter radius: ";

cin >> radius;

cout << "Area of Circle = " << PI * radius * radius << endl;

void perimeterCircle() {
float radius;

cout << "Enter radius: ";

cin >> radius;

cout << "Perimeter of Circle = " << 2 * PI * radius << endl;

void areaRectangle() {

float length, width;

cout << "Enter length and width: ";

cin >> length >> width;

cout << "Area of Rectangle = " << length * width << endl;

void perimeterRectangle() {

float length, width;

cout << "Enter length and width: ";

cin >> length >> width;

cout << "Perimeter of Rectangle = " << 2 * (length + width) << endl;

void areaTriangle() {

float base, height;

cout << "Enter base and height: ";

cin >> base >> height;

cout << "Area of Triangle = " << 0.5 * base * height << endl;

void volumeCube() {

float side;

cout << "Enter side length: ";

cin >> side;


cout << "Volume of Cube = " << side * side * side << endl;

void volumeCylinder() {

float radius, height;

cout << "Enter radius and height: ";

cin >> radius >> height;

cout << "Volume of Cylinder = " << PI * radius * radius * height << endl;

void areaSquare() {

float side;

cout << "Enter side: ";

cin >> side;

cout << "Area of Square = " << side * side << endl;

void perimeterSquare() {

float side;

cout << "Enter side: ";

cin >> side;

cout << "Perimeter of Square = " << 4 * side << endl;

int main() {

int choice;

do {

cout << "\n=== Geometry Calculator ===\n";

cout << "1. Area of Circle\n";

cout << "2. Perimeter of Circle\n";

cout << "3. Area of Rectangle\n";


cout << "4. Perimeter of Rectangle\n";

cout << "5. Area of Triangle\n";

cout << "6. Volume of Cube\n";

cout << "7. Volume of Cylinder\n";

cout << "8. Area of Square\n";

cout << "9. Perimeter of Square\n";

cout << "0. Exit\n";

cout << "Choose an option: ";

cin >> choice;

switch (choice) {

case 1: areaCircle(); break;

case 2: perimeterCircle(); break;

case 3: areaRectangle(); break;

case 4: perimeterRectangle(); break;

case 5: areaTriangle(); break;

case 6: volumeCube(); break;

case 7: volumeCylinder(); break;

case 8: areaSquare(); break;

case 9: perimeterSquare(); break;

case 0: cout << "Exiting program.\n"; break;

default: cout << "Invalid choice!\n";

} while (choice != 0);

return 0;

}
Problem 3

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

void compareNumbers() {

float a, b;

cout << "Enter two numbers: ";

cin >> a >> b;

if (a > b)

cout << a << " is greater than " << b << endl;

else if (a < b)

cout << b << " is greater than " << a << endl;

else

cout << "Both numbers are equal." << endl;


}

void compareStrings() {

string str1, str2;

cout << "Enter first string: ";

cin >> str1;

cout << "Enter second string: ";

cin >> str2;

if (str1 == str2)

cout << "Strings are equal." << endl;

else {

cout << "Strings are not equal." << endl;

if (str1 < str2)

cout << str1 << " comes before " << str2 << " in dictionary order." << endl;

else

cout << str2 << " comes before " << str1 << " in dictionary order." << endl;

void solveQuadratic() {

double a, b, c;

cout << "Enter coefficients a, b, and c: ";

cin >> a >> b >> c;

if (a == 0) {

cout << "This is not a quadratic equation." << endl;

return;

double discriminant = b * b - 4 * a * c;
cout << "Discriminant = " << discriminant << endl;

if (discriminant > 0) {

double root1 = (-b + sqrt(discriminant)) / (2 * a);

double root2 = (-b - sqrt(discriminant)) / (2 * a);

cout << "Roots are real and distinct: " << root1 << " and " << root2 << endl;

} else if (discriminant == 0) {

double root = -b / (2 * a);

cout << "Roots are real and equal: " << root << endl;

} else {

double realPart = -b / (2 * a);

double imagPart = sqrt(-discriminant) / (2 * a);

cout << "Roots are complex: "

<< realPart << " + " << imagPart << "i and "

<< realPart << " - " << imagPart << "i" << endl;

int main() {

int choice;

do {

cout << "\n=== Comparison & Quadratic Equation ===\n";

cout << "1. Compare Numbers\n";

cout << "2. Compare Strings\n";

cout << "3. Solve Quadratic Equation\n";

cout << "0. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: compareNumbers(); break;


case 2: compareStrings(); break;

case 3: solveQuadratic(); break;

case 0: cout << "Exiting...\n"; break;

default: cout << "Invalid option!\n";

} while (choice != 0);

return 0;

Problem 4:

#include <iostream>

using namespace std;

// Function to calculate GCD using Euclidean algorithm

int findGCD(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

// Function to calculate LCM using GCD

int findLCM(int a, int b) {

return (a * b) / findGCD(a, b);

int main() {
int choice;

do {

cout << "\n=== GCD and LCM Calculator ===\n";

cout << "1. Find GCD\n";

cout << "2. Find LCM\n";

cout << "0. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

int num1, num2;

switch (choice) {

case 1:

cout << "Enter two integers: ";

cin >> num1 >> num2;

cout << "GCD of " << num1 << " and " << num2 << " = " << findGCD(num1, num2) << endl;

break;

case 2:

cout << "Enter two integers: ";

cin >> num1 >> num2;

cout << "LCM of " << num1 << " and " << num2 << " = " << findLCM(num1, num2) << endl;

break;

case 0:

cout << "Exiting...\n";

break;

default:

cout << "Invalid choice!\n";

} while (choice != 0);

return 0;
}

Problem 5

#include <iostream>

using namespace std;

bool isPrime(int n) {

if (n <= 1)

return false; // 0 and 1 are neither prime nor composite

if (n == 2)

return true;

if (n % 2 == 0)

return false;

for (int i = 3; i * i <= n; i += 2) {

if (n % i == 0)

return false;

return true;

int main() {

int number;

cout << "Enter a positive integer: ";

cin >> number;


if (number <= 1)

cout << number << " is neither prime nor composite." << endl;

else if (isPrime(number))

cout << number << " is a prime number." << endl;

else

cout << number << " is a composite number." << endl;

return 0;

Problem 6

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

using namespace std;

void sortNumbers() {

int n;

cout << "Enter how many numbers you want to sort: ";

cin >> n;

vector<int> numbers(n);

cout << "Enter " << n << " numbers:\n";

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

cin >> numbers[i];

sort(numbers.begin(), numbers.end());
cout << "Sorted Numbers: ";

for (int num : numbers) {

cout << num << " ";

cout << endl;

void sortStrings() {

int n;

cout << "Enter how many strings you want to sort: ";

cin >> n;

vector<string> strings(n);

cout << "Enter " << n << " strings:\n";

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

cin >> strings[i];

sort(strings.begin(), strings.end());

cout << "Sorted Strings: ";

for (string str : strings) {

cout << str << " ";

cout << endl;

int main() {

int choice;

do {

cout << "\n=== Sorting Menu ===\n";


cout << "1. Sort Numbers\n";

cout << "2. Sort Strings\n";

cout << "0. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: sortNumbers(); break;

case 2: sortStrings(); break;

case 0: cout << "Exiting...\n"; break;

default: cout << "Invalid choice!\n";

} while (choice != 0);

return 0;

Problem 7

#include <iostream>

#include <vector>

#include <string>

using namespace std;

void searchNumber() {

int n, target;

cout << "Enter how many numbers you want to input: ";

cin >> n;

vector<int> numbers(n);

cout << "Enter " << n << " numbers:\n";


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

cin >> numbers[i];

cout << "Enter number to search: ";

cin >> target;

bool found = false;

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

if (numbers[i] == target) {

cout << "Number " << target << " found at position " << i + 1 << "." << endl;

found = true;

break;

if (!found)

cout << "Number not found in the list.\n";

void searchString() {

int n;

string target;

cout << "Enter how many strings you want to input: ";

cin >> n;

vector<string> strings(n);

cout << "Enter " << n << " strings:\n";

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

cin >> strings[i];

cout << "Enter string to search: ";


cin >> target;

bool found = false;

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

if (strings[i] == target) {

cout << "String \"" << target << "\" found at position " << i + 1 << "." << endl;

found = true;

break;

if (!found)

cout << "String not found in the list.\n";

int main() {

int choice;

do {

cout << "\n=== Search Menu ===\n";

cout << "1. Search Number\n";

cout << "2. Search String\n";

cout << "0. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: searchNumber(); break;

case 2: searchString(); break;

case 0: cout << "Exiting...\n"; break;

default: cout << "Invalid choice!\n";

}
} while (choice != 0);

return 0;

Problem 8

#include <iostream>

#include <cstdlib> // For rand() and srand()

#include <ctime> // For time()

using namespace std;

// Function to roll a dice (returns number between 1 and 6)

int rollDice() {

return rand() % 6 + 1;

int main() {

// Seed the random number generator with current time

srand(time(0));

char choice;

do {

cout << "Rolling the dice...\n";

int result = rollDice();

cout << "You rolled: " << result << endl;

cout << "Roll again? (y/n): ";

cin >> choice;

} while (choice == 'y' || choice == 'Y');

cout << "Thanks for playing!\n";


return 0;

Problem 9

#include <iostream>

using namespace std;

const int MAX = 3;

void inputMatrix(int matrix[MAX][MAX], int rows, int cols) {

cout << "Enter elements of matrix (" << rows << "x" << cols << "):\n";

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

for (int j = 0; j < cols; j++)

cin >> matrix[i][j];

void printMatrix(int matrix[MAX][MAX], int rows, int cols) {

cout << "Matrix:\n";

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

for (int j = 0; j < cols; j++)

cout << matrix[i][j] << "\t";

cout << "\n";

void addMatrices(int A[MAX][MAX], int B[MAX][MAX], int result[MAX][MAX], int rows, int cols) {

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

for (int j = 0; j < cols; j++)

result[i][j] = A[i][j] + B[i][j];

}
void multiplyMatrices(int A[MAX][MAX], int B[MAX][MAX], int result[MAX][MAX], int r1, int c1, int
r2, int c2) {

// Initialize result matrix to zero

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

int main() {

int A[MAX][MAX], B[MAX][MAX], result[MAX][MAX];

int r1, c1, r2, c2;

cout << "Matrix Addition and Multiplication (Max 3x3)\n";

// Input dimensions for first matrix

do {

cout << "Enter rows and columns for Matrix A (max 3): ";

cin >> r1 >> c1;

if (r1 > 3 || c1 > 3 || r1 <= 0 || c1 <= 0)

cout << "Invalid size. Please enter values between 1 and 3.\n";

} while (r1 > 3 || c1 > 3 || r1 <= 0 || c1 <= 0);

// Input dimensions for second matrix

do {

cout << "Enter rows and columns for Matrix B (max 3): ";

cin >> r2 >> c2;


if (r2 > 3 || c2 > 3 || r2 <= 0 || c2 <= 0)

cout << "Invalid size. Please enter values between 1 and 3.\n";

} while (r2 > 3 || c2 > 3 || r2 <= 0 || c2 <= 0);

// Input matrices

inputMatrix(A, r1, c1);

inputMatrix(B, r2, c2);

// Addition only possible if dimensions match

if (r1 == r2 && c1 == c2) {

addMatrices(A, B, result, r1, c1);

cout << "\nSum of matrices:\n";

printMatrix(result, r1, c1);

} else {

cout << "\nAddition not possible (matrices dimensions do not match).\n";

// Multiplication possible if columns of A == rows of B

if (c1 == r2) {

multiplyMatrices(A, B, result, r1, c1, r2, c2);

cout << "\nProduct of matrices:\n";

printMatrix(result, r1, c2);

} else {

cout << "\nMultiplication not possible (columns of A != rows of B).\n";

return 0;

Problem 10

Reversing a given number / string

#include <iostream>
#include <string>

using namespace std;

// Function to reverse a number

int reverseNumber(int num) {

int rev = 0;

while (num != 0) {

rev = rev * 10 + (num % 10);

num /= 10;

return rev;

// Function to reverse a string

string reverseString(const string& str) {

string rev = str;

int n = rev.length();

for (int i = 0; i < n / 2; i++) {

swap(rev[i], rev[n - 1 - i]);

return rev;

int main() {

int choice;

cout << "Choose what to reverse:\n1. Number\n2. String\nEnter choice: ";

cin >> choice;

cin.ignore(); // to consume leftover newline

if (choice == 1) {

int num;
cout << "Enter a number: ";

cin >> num;

cout << "Reversed number: " << reverseNumber(num) << "\n";

else if (choice == 2) {

string str;

cout << "Enter a string: ";

getline(cin, str);

cout << "Reversed string: " << reverseString(str) << "\n";

else {

cout << "Invalid choice.\n";

return 0;

You might also like