0% found this document useful (0 votes)
51 views15 pages

Exercises C++

The document contains 15 programming exercises in C++. The exercises cover topics like calculating factorials, sequential sums, solving sequences, printing patterns like triangles and stars, extracting largest numbers from arrays, calculating sums of array and matrix elements, swapping numbers using functions, finding sums and maximums using functions.

Uploaded by

bit20181210218
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)
51 views15 pages

Exercises C++

The document contains 15 programming exercises in C++. The exercises cover topics like calculating factorials, sequential sums, solving sequences, printing patterns like triangles and stars, extracting largest numbers from arrays, calculating sums of array and matrix elements, swapping numbers using functions, finding sums and maximums using functions.

Uploaded by

bit20181210218
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/ 15

University of Basra

College of Computer Science

and Information Technology


Department of Information Systems

Exercises C++

‫من قبل الطالبة‬


‫خاتون قيصر زيد‬
‫املرحله االوىل قسم نظم معلومات‬
A‫شعبة‬

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

2. A C++ program that calculates the sequential sum of factors


S=1!+2!+3!+4!.......N

#include <iostream>
using namespace std;

int n , i , s= 0 , f ,j ;
cout<<"enter n";
cin>>n;

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

{ f=1;

For ( j=1 ; j < = i; j ++)


f=f * j ;

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>

using namespace std;

int main() {
int n;
cout << "Enter the number of rows for the triangle: ";
cin >> n;

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


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

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;

for (int i = rows; i >= 1; --i) {


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

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;

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


for (int j = 1; j <= size; ++j) {
std::cout << j << " ";
}
cout << endl;
}

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

cout << "Original array:";


for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}

int maxElement = array[0];


for (int i = 1; i < size; ++i) {
if (array[i] > maxElement) {
maxElement = array[i];
}
}

cout << "\nLargest element in the array: " << maxElement;

return 0;
}

6
8.Write a program in C++ to calculate the sum of the elements of a single matrix

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

cout << "Original array:";


for (int i = 0; i < size; ++i) {
cout << " " << 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>

using namespace std;

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

cout << "Original matrix:\n";


for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << " " << matrix[i][j];
}
cout << endl;
}

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

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

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 << "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 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>

using namespace std;

void swap(int &x, int &y) {


int temp = x;
x = y;
y = temp;
}

int main() {
int a, b;

cout << "Enter the first number: ";


cin >> a;

cout << "Enter the second number: ";


cin >> b;

cout << "Before swapping:" << endl;


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

swap(a, b);

cout << "After swapping:" << endl;


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

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 sum(int x, int y) {


return x + y;
}

int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;

int result = sum(num1, num2);


std::cout << "The sum of " << num1 << " and " << num2 << " is: " << result << endl;

return 0;
}

13
15.Write a program in C++ to find the largest three-digit number using functions

#include <iostream>

using namespace std;

int findMax(int num1, int num2, int num3) {


int maxNumber = num1;
if (num2 > maxNumber) {
maxNumber = num2;
}
if (num3 > maxNumber) {
maxNumber = num3;
}
return maxNumber;
}

int main() {
int num1, num2, num3;

cout << "Enter the first number: ";


cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

cout << "Enter the third number: ";


cin >> num3;

int maxNumber = findMax(num1, num2, num3);

cout << "The maximum number is: " << maxNumber << endl;

return 0;
}

14

You might also like