Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Programming Using C++ MANUAL

Uploaded by

Saritha D
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)
20 views

Programming Using C++ MANUAL

Uploaded by

Saritha D
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/ 18

PROGRAMMING USING C++ LAB MANUAL

Part A
1) Program to find the area of Circle and Triangle

#include <iostream>
using namespace std;

int main() {
double r, area;

// Prompt the user to enter the radius


cout << "Enter the radius of the circle: ";
cin >> r;

// Calculate the area


area = 3.14 * r * r;

// Display the area


cout << "The area of the circle with radius " << r << " is " << area << endl;
return 0;
}
Enter the radius of the circle: 5
The area of the circle with radius 5 is 78.5

#include <iostream>
using namespace std;

int main() {
double b, h, area;

// Prompt the user to enter the base and height of the triangle
cout << "Enter the base of the triangle: ";
cin >> b;

cout << "Enter the height of the triangle: ";


cin >> h;

// Calculate the area


area = 0.5 * b * h;

// Display the area


cout << " area of triangle is" << area << endl;
return 0;
}
Enter the base of the triangle: 10
Enter the height of the triangle: 5
The area of the triangle is 25

PARTA-1
PROGRAMMING USING C++ LAB MANUAL
2) Program to find the largest and second largest among 3 numbers

#include <iostream>
using namespace std;

int main() {
int n1, n2, n3;
int largest, secondLargest;

// Prompt the user to enter three numbers


cout << "Enter the first number: ";
cin >> n1;
cout << "Enter the second number: ";
cin >> n2;
cout << "Enter the third number: ";
cin >> n3;

// Find the largest and second largest numbers


if (n1 >= n2 && n1 >= n3) {
largest = n1;
secondLargest = (n2 >= n3) ? n2 : n3;
} else if (n2 >= n1 && n2 >= n3) {
largest = n2;
secondLargest = (n1 >= n3) ? n1 : n3;
} else {
largest = n3;
secondLargest = (n1 >= n2) ? n1 : n2;
}

// Display the results


cout << "The largest number is " << largest << endl;
cout << "The second largest number is " << secondLargest << endl;

return 0;
}

Enter the first number: 10


Enter the second number: 20
Enter the third number: 15
The largest number is 20
The second largest number is 15

PARTA-2
PROGRAMMING USING C++ LAB MANUAL
3) Program to classify triangles based on the length of sides

#include <iostream>
using namespace std;

int main() {
int a, b, c;

// Prompt the user to enter the lengths of the sides of the triangle
cout << "Enter the length of the first side (a): ";
cin >> a;
cout << "Enter the length of the second side (b): ";
cin >> b;
cout << "Enter the length of the third side (c): ";
cin >> c;

// Classify the triangle


if (a == b && b == c) {
cout << "The triangle is equilateral." << endl;
} else if (a == b || a == c || b == c) {
cout << "The triangle is isosceles." << endl;
} else {
cout << "The triangle is scalene." << endl;
}

return 0;
}
Enter the length of the first side (a): 5
Enter the length of the second side (b): 5
Enter the length of the third side (c): 5
The triangle is equilateral.

Enter the length of the first side (a): 5


Enter the length of the second side (b): 5
Enter the length of the third side (c): 8
The triangle is isosceles.

Enter the length of the first side (a): 3


Enter the length of the second side (b): 4
Enter the length of the third side (c): 5
The triangle is scalene.

PARTA-3
PROGRAMMING USING C++ LAB MANUAL
4) Program to generate all prime numbers between M and N using function

#include <iostream>
using namespace std;

int main() {
int M, N;

// Prompt the user to enter the range


cout << "Enter the starting number M: ";
cin >> M;
cout << "Enter the ending number N: ";
cin >> N;

// Iterate through the range from M to N


cout << "Prime numbers between " << M << " and " << N << " are: ";
for (int num = M; num <= N; ++num) {
bool isPrime = true;

// Check if num is a prime number


if (num <= 1) {
isPrime = false;
} else if (num <= 3) {
isPrime = true;
} else if (num % 2 == 0 || num % 3 == 0) {
isPrime = false;
} else {
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
isPrime = false;
break;
}
}
}

// Print the number if it's prime


if (isPrime) {
cout << num << " ";
}
}
cout << endl;

return 0;
}
Enter the starting number M: 10
Enter the ending number N: 20
Prime numbers between 10 and 30 are: 11 13 17 19

PARTA-4
PROGRAMMING USING C++ LAB MANUAL
5) Program to find the sum of digits of a numbers and check whether it is
palindrome or not

#include <iostream>
using namespace std;

int main() {
int num, originalNum, sumOfDigits = 0, reversedNum = 0, remainder;

// Prompt the user to enter a number


cout << "Enter a number: ";
cin >> num;

originalNum = num;

// Calculate the sum of digits and reversed number


while (num != 0) {
remainder = num % 10;
sumOfDigits += remainder;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}

// Output the sum of digits


cout << "Sum of the digits: " << sumOfDigits << endl;

// Check and output if the number is a palindrome


if (reversedNum == originalNum) {
cout << originalNum << " is a palindrome." << endl;
} else {
cout << originalNum << " is not a palindrome." << endl;
}

return 0;
}

Enter a number: 121


Sum of the digits: 4
121 is a palindrome.

Enter a number: 123


Sum of the digits: 6
123 is not a palindrome.

PARTA-5
PROGRAMMING USING C++ LAB MANUAL
6) Program to generate the following pattern
a)
1
1 2
1 2 3

b)

#include <iostream>
using namespace std;

int main() {
int rows = 3; // Number of rows in the pattern

// Loop through each row


for (int i = 1; i <= rows; ++i) {
// Loop through each column in the current row
for (int j = 1; j <= i; ++j) {
cout << j << " ";
}
cout << endl; // Move to the next line after each row
}

return 0;
}
1
12
123

PARTA-6
PROGRAMMING USING C++ LAB MANUAL

#include <iostream>
using namespace std;

int main() {
int n = 6; // Number of rows for the pattern

// Generate the pattern


for (int i = 1; i <= n; ++i) {
// Print leading spaces
for (int j = n; j > i; --j) {
cout << " ";
}

// Print the first part of asterisks


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

// Print the second part of asterisks


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

cout << endl;


}

return 0;
}
*
**
***
****
** ***
******

PARTA-7
PROGRAMMING USING C++ LAB MANUAL
7) Program to solve a given quadratic equation using switch statement

#include <iostream>
#include <cmath>
using namespace std;

int main() {
double a, b, c;
double disc, x1, x2, rp, ip;

// Prompt the user to enter the coefficients a, b, and c


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

// Calculate the discriminant


disc = b * b - 4 * a * c;

// Use a switch statement to determine the nature of the roots


switch ((disc > 0) - (disc < 0)) {
case 1: // disc > 0
x1 = (-b + sqrt(disc)) / (2 * a);
x2 = (-b - sqrt(disc)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
break;
case 0: // disc == 0
x1 = x2 = -b / (2 * a);
cout << "Roots are real and the same." << endl;
cout << "Root 1 = Root 2 = " << x1 << endl;
break;
case -1: // disc < 0
rp = -b / (2 * a);
ip = sqrt(-disc) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << rp << " + " << ip << "i" << endl;
cout << "Root 2 = " << rp << " - " << ip << "i" << endl;
break;
default:
cout << "Unexpected case." << endl;
break;
}
return 0;
}

PARTA-8
PROGRAMMING USING C++ LAB MANUAL

Enter coefficients a, b and c: 1 -3 2


Roots are real and different.
Root 1 = 2
Root 2 = 1

Enter coefficients a, b and c: 1 -2 1


Roots are real and the same.
Root 1 = Root 2 = 1

Enter coefficients a, b and c: 1 2 5


Roots are complex and different.
Root 1 = -1 + 2i
Root 2 = -1 - 2i

PARTA-9
PROGRAMMING USING C++ LAB MANUAL
8) Program to sum of all elements in an array

#include <iostream>
using namespace std;

int main() {
int n;

// Prompt the user to enter the number of elements


cout << "Enter the number of elements in the array: ";
cin >> n;

int A[n]; // Declare the array

// Prompt the user to enter the elements of the array


cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> A[i];
}

int sum = 0;

// Calculate the sum of all elements


for (int i = 0; i < n; ++i) {
sum += A[i];
}

// Output the sum


cout << "The sum of all elements in the array is: " << sum << endl;

return 0;
}

Enter the number of elements in the array: 5


Enter the elements of the array: 1 2 3 4 5
The sum of all elements in the array is: 15

PARTA-10
PROGRAMMING USING C++ LAB MANUAL
9) Program to search an element in an array

#include <iostream>
using namespace std;

int main() {
int a[10];
int n, key, found = -1;

// Prompt the user to enter the number of elements


cout << "Enter number of elements: " << endl;
cin >> n;

// Prompt the user to enter the elements of the array


cout << "Enter the elements: " << endl;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}

// Prompt the user to enter the element to search for


cout << "Enter the element to be searched: " << endl;
cin >> key;

// Perform linear search


for (int i = 0; i < n; ++i) {
if (a[i] == key) {
found = i;
break;
}
}

// Output the result


if (found == -1) {
cout << "Element is not present in the array" << endl;
} else {
cout << "Element is present in the array at index " << found << endl;
}

return 0;
}

Enter number of elements:


5
Enter the elements:
10 20 30 40 50
Enter the element to be searched:
30

PARTA-11
PROGRAMMING USING C++ LAB MANUAL
Element is present in the array at index 2

Enter number of elements:


5
Enter the elements:
10 20 30 40 50
Enter the element to be searched:
25
Element is not present in the array

PARTA-12
PROGRAMMING USING C++ LAB MANUAL
10) Program to find the sum of 2 matrices

#include <iostream>
using namespace std;

int main() {
int r, c;

// Prompt the user to enter the number of rows and columns


cout << "Enter the number of rows: ";
cin >> r;
cout << "Enter the number of columns: ";
cin >> c;

int m1[r][c], m2[r][c], sum[r][c];

// Input elements of the first matrix


cout << "Enter elements of the first matrix:" << endl;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cin >> m1[i][j];
}
}
// Input elements of the second matrix
cout << "Enter elements of the second matrix:" << endl;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cin >> m2[i][j];
}
}
// Calculate the sum of the matrices
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
sum[i][j] = m1[i][j] + m2[i][j];
}
}
// Display the sum of the matrices
cout << "Sum of the two matrices is:" << endl;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cout << sum[i][j] << " ";
}
cout << endl;
}
return 0;
}

PARTA-13
PROGRAMMING USING C++ LAB MANUAL
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
12
34
Enter elements of the second matrix:
56
78
Sum of the two matrices is:
68
10 12

PARTA-14
PROGRAMMING USING C++ LAB MANUAL
11) Program to find the simple interest with default argument

#include <iostream>
using namespace std;

// Function to calculate simple interest with default arguments


float calculatesi(float p, float r = 5.0, int t = 1) {
return (p * r * t) / 100;
}

int main() {
float p, r;
int t;
double interest;

// Prompt the user to enter the principal amount


cout << "Enter the principal amount: ";
cin >> p;

// Prompt the user to enter the rate of interest (optional)


cout << "Enter the rate of interest (default is 5%): ";
cin >> r;

// Prompt the user to enter the time period in years (optional)


cout << "Enter the time period in years (default is 1 year): ";
cin >> t;

// Calculate simple interest


interest = calculatesi(p, r, t);

// Display the result


cout << "The simple interest is: " << interest << endl;

return 0;
}

Enter the principal amount: 1000


Enter the rate of interest (default is 5%):
Enter the time period in years (default is 1 year):
The simple interest is: 50

PARTA-15
PROGRAMMING USING C++ LAB MANUAL
12) Program to check whether a given number is prime or not using friend
function

#include <iostream>
using namespace std;

class Number {
private:
int num;

public:
// Constructor to initialize the number
Number(int n) : num(n) {}

// Friend function declaration


friend bool isPrime(Number n);
};

// Friend function to check if a number is prime


bool isPrime(Number n) {
int number = n.num;
if (number <= 1) return false;
if (number == 2) return true; // 2 is the only even prime number
if (number % 2 == 0) return false; // Other even numbers are not prime

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


if (number % i == 0) return false;
}
return true;
}

int main() {
int num;
cout << "Enter a number to check if it's prime: ";
cin >> num;

Number number(num);

if (isPrime(number)) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}

return 0;
}

PARTA-16
PROGRAMMING USING C++ LAB MANUAL
Enter a number to check if it's prime: 29
29 is a prime number.

Enter a number to check if it's prime: 30


30 is not a prime number.

PARTA-17
PROGRAMMING USING C++ LAB MANUAL
13) Program to check whether a given number is even or odd using inline
function

#include <iostream>
using namespace std;

// Inline function to check if a number is even


inline bool isEven(int num) {
return (num % 2 == 0);
}

int main() {
int num;

// Prompt the user to enter a number


cout << "Enter a number: ";
cin >> num;

// Check if the number is even or odd


if (isEven(num)) {
cout << num << " is an even number." << endl;
} else {
cout << num << " is an odd number." << endl;
}

return 0;
}
Enter a number: 8
8 is an even number.

Enter a number: 15
15 is an odd number.

PARTA-18

You might also like