Exercises C++
Exercises C++
Exercises C++
2024
1.Write a program to find the Factorial number
4!=1*2*3*4=24
#include <iostream>
using namespace std;
int main()
{ int i, f=1;
for( i =1; i<= 4 ; i++)
f=f*i;
cout<<f ;
return 0;}
#include <iostream>
using namespace std;
int n , i , s= 0 , f ,j ;
cout<<"enter n";
cin>>n;
{ f=1;
s=s+f;}
cout<<s;
1
3.Write a C++ program to solve sequences
𝟏 𝟏 𝟏 𝟏 𝟏
S= + + + … … +
𝟐 𝟑 𝟒 𝟓 𝐍
#include <iostream>
#include <math.h>
using namespace std;
int main()
{int i;
float n,s=0 ;
cout<<"enter n";
cin>>n;
for(i=1;i<=n;i++)
s=s+1.0/i;
cout<<s;
return 0;}
2
4.Write a program in C++ that asks the user to enter the number of rows of a
triangle, then prints a triangle of numbers with the number increasing in each
row.
1
22
333
4444
#include <iostream>
int main() {
int n;
cout << "Enter the number of rows for the triangle: ";
cin >> n;
return 0;
}
3
5.Write a C++ program to print an inverted triangle of stars as the number of
stars in each row decreases
****
***
**
*
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows for the inverted triangle: ";
cin >> rows;
return 0;
}
4
6.Write a C++ program to print a square of numbers where the value of the
numbers is incremented in each row and in each column.
123456
123456
123456
123456
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the square: ";
cin >> size;
return 0;
}
5
7.Write a program in C++ language to extract the largest number in a single array
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int array[size];
cout << "Enter the elements of the array:\n";
for (int i = 0; i < size; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> array[i];
}
return 0;
}
6
8.Write a program in C++ to calculate the sum of the elements of a single matrix
#include <iostream>
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int array[size];
cout << "Enter the elements of the array:\n";
for (int i = 0; i < size; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> array[i];
}
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += array[i];
}
cout << "\nSum of all elements in the array: " << sum;
return 0;
}
7
9.Write a program in C to calculate the sum of the diagonal-principal elements of
single matrices
#include <iostream>
int main() {
int size;
cout << "Enter the size of the square matrix: ";
cin >> size;
int matrix[size][size];
cout << "Enter the elements of the square matrix:\n";
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << "Element at position (" << i + 1 << ", " << j + 1 << "): ";
cin >> matrix[i][j];
}
}
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += matrix[i][i];
}
cout << "Sum of main diagonal elements in the matrix: " << sum;
return 0;
}
8
10.A C++ program to calculate the sum of the elements of a binary matrix
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows in the matrix: ";
cin >> rows;
cout << "Enter the number of columns in the matrix: ";
cin >> cols;
int matrix[rows][cols];
cout << "Enter the elements of the matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Enter element at position (" << i + 1 << ", " << j + 1 << "): ";
cin >> matrix[i][j];
}
}
int sum = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
sum += matrix[i][j];
}
}
cout << "Sum of all elements in the matrix: " << sum;
return 0;
}
9
11.Write a program in C++ to calculate the largest number in a binary array
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows in the matrix: ";
cin >> rows;
cout << "Enter the number of columns in the matrix: ";
cin >> cols;
int matrix[rows][cols];
cout << "Enter the elements of the matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Enter element at position (" << i + 1 << ", " << j + 1 << "): ";
cin >> matrix[i][j];
}
}
cout << "Original matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << " " << matrix[i][j];
}
cout << endl;
}
int maxElement = matrix[0][0];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] > maxElement) {
maxElement = matrix[i][j];
}
}
}
cout << "Largest element in the matrix: " << maxElement;
return 0;
}
10
12.Write a program c++ to calculate the sum of the main diagonal elements in
binary matrices
#include <iostream>
int main() {
int rows, cols;
cout << "Enter the number of rows in the matrix: ";
cin >> rows;
cout << "Enter the number of columns in the matrix: ";
cin >> cols;
int matrix[rows][cols];
cout << "Enter the elements of the matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Element at position (" << i + 1 << ", " << j + 1 << "): ";
cin >> matrix[i][j];
}
}
int sum = 0;
for (int i = 0; i < rows; ++i) {
sum += matrix[i][i];
}
cout << "Sum of main diagonal elements in the matrix: " << sum;
return 0;
}
11
13.Write a program in C++ to swap two numbers using a function
#include <iostream>
int main() {
int a, b;
swap(a, b);
return 0;
}
12
14.Writing a program in C++ that uses a function to find the sum of two numbers
using a function
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
return 0;
}
13
15.Write a program in C++ to find the largest three-digit number using functions
#include <iostream>
int main() {
int num1, num2, num3;
cout << "The maximum number is: " << maxNumber << endl;
return 0;
}
14