0% found this document useful (0 votes)
2 views8 pages

Document 32

The document contains multiple C++ programming tasks, including functions to calculate the sum of digits of a 5-digit number, swap two numbers using pass-by-reference, find the maximum of three integers, and add two matrices (both 2x2 and n x m). Each task includes code snippets demonstrating the required functionality. The programs emphasize user input, array manipulation, and basic arithmetic operations.

Uploaded by

ahmadsemexpert
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)
2 views8 pages

Document 32

The document contains multiple C++ programming tasks, including functions to calculate the sum of digits of a 5-digit number, swap two numbers using pass-by-reference, find the maximum of three integers, and add two matrices (both 2x2 and n x m). Each task includes code snippets demonstrating the required functionality. The programs emphasize user input, array manipulation, and basic arithmetic operations.

Uploaded by

ahmadsemexpert
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/ 8

Q1:A 5-digit positive integer is entered through the keyboard, write a user defined

function to calculate sum of digits of the 5-digit number.

#include <iostream>

using namespace std;

int sumOfDigits(int number) {

int sum = 0;

while (number > 0) {

sum += number % 10;

number /= 10;

return sum;

int main() {

int num;

cout << "Enter a 5-digit positive integer: ";

cin >> num;

if (num >= 10000 && num <= 99999) {

cout << "The sum of the digits is: " << sumOfDigits(num) << endl;

} else {

cout << "Please enter a valid 5-digit number!" << endl;

return 0;

}
Q2:Write a program that demonstrates pass-by-reference to swap two numbers.
Explain how this method differs from pass-by-value, and describe the advantages of
using pass-by-reference for certain use cases

#include <iostream>

using namespace std;

int main() {

int num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

int &a = num1;

int &b = num2;

int temp = a;

a = b;

b = temp;

cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

return 0;

}
Q3:Write a C++ program with a user-defined function findMax that takes three integer
parameters and returns the largest of the three numbers. Include both the function
definition and its usage in the main function.

#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

int max = (num1 > num2 && num1 > num3) ? num1 : (num2 > num3 ? num2 : num3);

cout << "The largest number is: " << max << endl;

return 0;

Q4:Write a C++ program to add two 2x2 matrices. The program should: Take two 2x2
matrices as input from the user. Add the corresponding elements of the matrices.
Display the resulting matrix

#include <iostream>

using namespace std;

int main() {

int size;
cout << "Enter the size of the arrays: ";

cin >> size;

int array1[size], array2[size], result[size];

cout << "Enter elements of the first array:\n";

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

cin >> array1[i];

cout << "Enter elements of the second array:\n";

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

cin >> array2[i];

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

result[i] = array1[i] + array2[i];

cout << "The resulting array after addition is:\n";

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

cout << result[i] << " ";

cout << endl;

return 0;

}
Q5 ;Take 2 different arrays and add corresponding element of each array display
output

#include <iostream>

using namespace std;

int main() {

int size;

cout << "Enter the size of the arrays: ";

cin >> size;

int array1[size], array2[size], result[size];

cout << "Enter elements of the first array:\n";

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

cin >> array1[i];

cout << "Enter elements of the second array:\n";

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

cin >> array2[i];

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


result[i] = array1[i] + array2[i];

cout << "The resulting array after addition is:\n";

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

cout << result[i] << " ";

cout << endl;

return 0;

Q.6 Write a program that takes two 2D arrays (matrices) of size n x m from the user and
calculates their sum. Print the resulting matrix. Enter elements of first matrix: 1 2 3 4 5
6 Enter elements of second matrix: 7 8 9 10 11 12 The sum of the two matrices is: 8 10
12 14 16 18

#include <iostream>

using namespace std;

int main() {

int n, m;

cout << "Enter the number of rows (n): ";

cin >> n;

cout << "Enter the number of columns (m): ";

cin >> m;

int matrix1[n][m], matrix2[n][m], result[n][m];


cout << "Enter elements of the first matrix:\n";

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

for (int j = 0; j < m; j++) {

cin >> matrix1[i][j];

cout << "Enter elements of the second matrix:\n";

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

for (int j = 0; j < m; j++) {

cin >> matrix2[i][j];

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

for (int j = 0; j < m; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

cout << "The sum of the two matrices is:\n";

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

for (int j = 0; j < m; j++) {

cout << result[i][j] << " ";

cout << endl;

}
return 0;

You might also like