Răspuns :

Răspuns:

#include <iostream>

#include <fstream>

using namespace std;

int digitsSum(int n) {

int sum = 0;

while (n != 0) {

sum += n % 10;

n /= 10;

}

return sum;

}

int isPrime(int n) {

int middle = n / 2;

for (int i = 2;i <= middle; i++) {

if (n % i == 0) {

return 0;

}

}

return 1;

}

void countOddNumbers(int matrix[][20],int n) {

int counter = 0;

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

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

if (j < i) {

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

counter++;

}

}

}

}

if (counter == 0) {

cout << "Mai jos de diagonala principala nu este nici un element impar\n";

} else if (counter == 1) {

cout << "Mai jos de diagonala principala este 1 element impar\n";

} else {

cout << "Mai jos de diagonala principala se afla " << counter << " elemente impare\n";

}

}

int main() {

ifstream fin("atestat.in");

ofstream fout("atestat.out");

int n;

int matrix[20][20];

fin >> n;

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

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

fin >> matrix[i][j];

}

}

cout << "Matricea citita: \n";

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

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

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

}

cout << endl;

}

cout << endl << "Elementele de pe diagonala secundara: \n";

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

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

if (i + j == n - 1) {

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

}

}

}

cout << endl;

countOddNumbers(matrix,n);

int countPrimeNumbers = 0;

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

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

if (isPrime(digitsSum(matrix[i][j]))) {

countPrimeNumbers++;

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

}

}

}

if (countPrimeNumbers == 0) {

fout << "NU EXISTA ASTFEL DE NUMERE";

}

return 0;

}

Explicație: