0% found this document useful (0 votes)
159 views

Write A Program To Store and Calculate The Sum of 5 Numbers Entered by The User Using Arrays

The document contains 4 C++ programs: 1) A program to store 5 numbers in an array, calculate their sum, and output the sum. 2) A program that displays all elements of an initialized 2D array. 3) A program that takes array elements from user input and displays the largest number. 4) A program that adds two matrices of user-defined size by taking elements as input and outputting the sum matrix.

Uploaded by

tarek mahmoud
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)
159 views

Write A Program To Store and Calculate The Sum of 5 Numbers Entered by The User Using Arrays

The document contains 4 C++ programs: 1) A program to store 5 numbers in an array, calculate their sum, and output the sum. 2) A program that displays all elements of an initialized 2D array. 3) A program that takes array elements from user input and displays the largest number. 4) A program that adds two matrices of user-defined size by taking elements as input and outputting the sum matrix.

Uploaded by

tarek mahmoud
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/ 5

1.

Write a program to store and calculate the sum of 5 numbers entered by the user using
arrays.

#include <iostream.h>
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";

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


{
cin >> numbers[i];
sum += numbers[i];
}

cout << "Sum = " << sum << endl;

return 0;
}
2. Write C++ Program that display all elements of an initialized two dimensional array.
#include <iostream.h>

int main() {

int test[3][2] = {

{2, -5},

{4, 0},

{9, 1}

};

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

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

cout<< "test["<< i << "][" << j << "] = " <<


test[i][j]<<endl;

return 0;

}
3. Write a program that Display largest number of an array b(elements entered by
user )

#include <iostream.h>

int main()
{
int i, n;
float arr[100];

cout << "Enter total number of elements(1 to 100): ";


cin >> n;
cout << endl;

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


{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}

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


{
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << "Largest element = " << arr[0];

return 0;
}
4. Write a program that add Two Matrices using Multi-dimensional Arrays.

#include <iostream.h>

int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i,
j;

cout << "Enter number of rows (between 1 and 100): ";


cin >> r;

cout << "Enter number of columns (between 1 and 100):


";
cin >> c;

cout << endl << "Enter elements of 1st matrix: " <<
endl;

// Storing elements of first matrix entered by user.


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << "
: ";
cin >> a[i][j];
}

// Storing elements of second matrix entered by user.


cout << endl << "Enter elements of 2nd matrix: " <<
endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << "
: ";
cin >> b[i][j];
}

// Adding Two matrices


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];

// Displaying the resultant sum matrix.


cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}

return 0;
}

You might also like