You are on page 1of 6

Write a C++ program that creates a 2D array having 4 rows and 4 columns.

Then, ask the user to input


values for the 2D array or matrix. After that, the program should calculate transpose of a matrix.
Transpose is basically calculated by changing rows of matrix into columns and columns into rows. After
calculating, display the final matrix.

#include <iostream>

using namespace std;

int main() {

int matrix[4][4];

cout << "Enter values for a 4x4 matrix:" << endl;

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

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

cout << "Enter value for row " << i + 1 << " column " << j + 1 << ": ";

cin >> matrix[i][j];

cout << "\nOriginal Matrix:" << endl;

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

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

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


}

cout << endl;

int transpose[4][4];

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

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

transpose[j][i] = matrix[i][j];

cout << "\nTranspose Matrix:" << endl;

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

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

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

cout << endl;

return 0;

Write a program that creates and then reads a matrix of 5 rows and 5 columns of type int. while reading;
the program should not accept values greater than 100. For any entered value greater than 100, the
program should ask for input repeatedly. After reading all numbers, the system should find the
smallestnumber in the matrix and its location or index values. The program should print the smallest
number and its location (row and column) and also print their count.
#include <iostream>

using namespace std;

int main() {

int rows = 5;

int cols = 5;

int matrix[rows][cols];

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

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

do {

cout << "Enter value for row " << i + 1 << " column " << j + 1 << ": ";

cin >> matrix[i][j];

if (matrix[i][j] > 100) {

cout << "Please enter a value less than or equal to 100." << endl;

} while (matrix[i][j] > 100);

int smallest = matrix[0][0];

int smallestRow = 0, smallestCol = 0;

int countSmallest = 0;

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

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

if (matrix[i][j] < smallest) {

smallest = matrix[i][j];

smallestRow = i;
smallestCol = j;

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

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

if (matrix[i][j] == smallest) {

countSmallest++;

cout << "\nSmallest number in the matrix: " << smallest << endl;

cout << "Location (Row, Column): (" << smallestRow + 1 << ", " << smallestCol + 1 << ")" << endl;

cout << "Number of occurrences: " << countSmallest << endl;

return 0;

Write a C++ program that displays the row numbers of a matrix containing at least two even numbers.
First, the program should create a 2D array (5 rows, 5 columns). Then, ask the user to input values in the
matrix or 2D array. After that, program should display row number (or index) which have less than 2
prime numbers. If no row (of the matrix) contains at least two prime numbers then the program should
display the message “No row found containing two prime numbers”. Sample Input: Please enter data for
matrix of 5 x 5: 2 6 18 49 8 33 15 11 17 13 8 21 47 37 28 12 12 12 12 12 2 3 4 5 6 Sample Output: Row
number 1, 4 contains prime numbers less than 2.

#include <iostream>
using namespace std;

int main() {

int rows = 5;

int cols = 5;

int matrix[rows][cols];

cout << "Please enter data for matrix of 5 x 5:" << endl;

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

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

cin >> matrix[i][j];

bool found = false;

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

int evenCount = 0, primeCount = 0;

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

if (matrix[i][j] % 2 == 0) evenCount++;

bool isPrime = true;

for (int k = 2; k * k <= matrix[i][j]; ++k) {

if (matrix[i][j] % k == 0) {

isPrime = false;

break;

if (isPrime && matrix[i][j] > 1) primeCount++;

if (evenCount >= 2 && primeCount < 2) {

if (found) cout << ", ";

cout << "Row number " << i + 1;


found = true;

if (!found) cout << "No row found containing two prime numbers";

else cout << " contains prime numbers less than 2." << endl;

return 0;

You might also like