Document 32
Document 32
#include <iostream>
int sum = 0;
number /= 10;
return sum;
int main() {
int num;
cout << "The sum of the digits is: " << sumOfDigits(num) << endl;
} else {
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>
int main() {
cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
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>
int main() {
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>
int main() {
int size;
cout << "Enter the size of the arrays: ";
return 0;
}
Q5 ;Take 2 different arrays and add corresponding element of each array display
output
#include <iostream>
int main() {
int size;
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>
int main() {
int n, m;
cin >> n;
cin >> m;
}
return 0;