0% found this document useful (0 votes)
10 views14 pages

Assignment_Intensive_Programming_Lab_

The document contains five C++ programming tasks, each with a description and code implementation. The tasks include calculating the product of digits in a number, finding the maximum of three numbers with user interaction, tripling a variable by value and by reference, validating and storing unique numbers in an array, and multiplying two matrices. Each task is accompanied by example code that demonstrates the required functionality.

Uploaded by

rsab50930
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)
10 views14 pages

Assignment_Intensive_Programming_Lab_

The document contains five C++ programming tasks, each with a description and code implementation. The tasks include calculating the product of digits in a number, finding the maximum of three numbers with user interaction, tripling a variable by value and by reference, validating and storing unique numbers in an array, and multiplying two matrices. Each task is accompanied by example code that demonstrates the required functionality.

Uploaded by

rsab50930
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/ 14

1

Q.No# 1:
Write a C++ program that will declare a function named product. It should enter a
5-digit number in main function and calculate the product of its digits using
product function.
e.g
if user has entered 12345 then it must display the product of its digits 1x2x3x4x5=
120 ?

Code:

#include <iostream>
using namespace std;
int product(int num) {
int result = 1;
while (num > 0) {
result *= num % 10;
num /= 10;
}
return result;}
int main() {
int num;
cout << "Enter a 5-digit number: ";
cin >> num;
cout << "Product of digits = " << product(num) << endl;
return 0;
}
Output:

Q.No # 2
Write a C++ program that will define a function max3 which user 3 parameters
and returns the value of the largest. Test the function in a program that
determines the largest. After it displays the largest number, it will then give user
option that whether he wants to continue calculating
maximum of three new numbers or want to exit, if user chooses continue it will
again enter three numbers from user and calculate which one is maximum, if
user chooses exit it will then end the program?
Code:
#include <iostream>
using namespace std;
int num3(int a, int b, int c) {
if (a>b) return a;
if(a>c) return a;
if (b > c) return b;
return c;}
int main() {
int x, y, z;
cout<<"Enter the first number: ";
cin>>x;
cout<<"Enter the second number: ";
cin>>y;
cout<<"Enter the third number: ";
cin>>z;
cout << "Largest number is: " << num3(x, y, z) <<
endl;
return 0;
}

Output :

Q. No# 3:
Write a complete C++ program with the two alternate func+tions specified
below, each of which simply triples the variable count defined in main. Then
compare and contrast the two approaches. These two functions are

a) Function triple By Value that passes a copy of count by value, triples the
copy and returns the new value and
b) Function triple By Reference that passes count by reference via a
reference parameter and triples the original value of count through its
alias (i.e., the reference parameter)?
Code:
#include <iostream>
using namespace std;
int triple By Value(int num) {
return num * 3;
}
void triple By Reference(int & num) {
num = num * 3;
}
int main() {
int count = 5;
cout << "Original count: " << count << endl;
int value Result = triple By Value(count);
cout << "After triple By Value, count: " << count << endl;
cout << "Result from triple By Value: " << value Result <<
endl;
triple By Reference(count);
cout << "After triple By Reference, count: " << count <<
endl;
return 0;
}
Output
Q. No # 4:
Use a one-dimensional array to solve the following problem:
10 numbers, each of which is between 10 and 100, inclusive. As each number is
read, validate it and store it in the array only if it isn’t a duplicate of a number
already read. After reading all the values, display only the unique values that the
user entered. Provide for the “worst case” in which all 10 numbers are different.
Use the smallest possible array to solve this problem?

Code:
#include <iostream>
using namespace std;
int main() {
int numbers[10];
int count = 0;
while (count < 10) {
int num;
cout << "Enter a number between 10 and 100: ";
cin >> num;
if (num < 10 )
if(num>100){

cout << "Invalid input. Please try again." << endl;


continue;
}
bool is Duplicate = false;
for (int i = 0; i < count; i++) {
if (numbers[i] = num) {
isDuplicate = true;
break;
}
}
if (!is Duplicate) {
numbers[count++] = num;
} else {
cout << "Duplicate number. Try again." << endl; }
}
cout << "Unique numbers entered: ";
for (int i = 0; i < 10; i++) {
cout << numbers[i] << " "; }
cout << endl;
return 0;
}
Output:

Q.NO# 5:
Write a C++ program that input 2 matrix from user and display its
multiplication.
Code :
#include <iostream>
using namespace std;
int main() {
int A[10][10], B[10][10], C[10][10] = {0};
int r1, c1, r2, c2;
cout << "Enter rows and columns of first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns of second matrix: ";
cin >> r2 >> c2;
if (c1 != r2) {
cout << "Cannot multiply matrices! (Columns of
first != Rows of second)";
return 0;
}
cout << "Enter elements of first matrix:\n";
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
cin >> A[i][j];
cout << "Enter elements of second matrix:\n";
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
cin >> B[i][j];
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++)
for (int k = 0; k < c1; k++)
C[i][j] += A[i][k] * B[k][j];
cout << "Product of the matrices:\n";
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << endl;
}
return 0;
}
Output:

You might also like