Before developing any algorithm you need analyse the situation, with this we need to know first following:
What are prime numbers?
A prime number is a natural number greater than 1, whose only positive divisors are 1 and itself. All others are called composite numbers.
What characterizes a prime number ?
Aggregating the knowledge of the above question, the unique even prime number is 2, all the others are odd. So with this we know of a general form that the primes are not all odd numbers and by exception the only even number 2.
#include <iostream>
#include <stdlib.h>
using namespace std;
#define LINHA 10
#define COLUNA 5
int main() {
int matriz[LINHA][COLUNA];
int number = 0;
// ADICIONA NÚMEROS ALEATÓRIOS
for(int l = 0; l < LINHA; l++) {
for(int c = 0; c < COLUNA; c++) {
matriz[l][c] = (rand() % 1000);
}
}
//PERCORRE TODA A MATRIZ BIDIMENSIONAL, PROCURANDO OS NÚMEROS PRIMOS
for(int l = 0; l < LINHA; l++) {
for(int c = 0; c < COLUNA; c++) {
number = matriz[l][c];
// UTILIZANDO DIVISÃO SUCESSIVA DE 2 ATÉ O NÚMERO ENCONTRADO NA MATRIZ
// SEGUINDO O CONCEITO DE NÚMEROS PRIMOS, ONDE:
// 1)Um número primo é um número natural maior do que 1
// 2) Únicos divisores positivos são 1 e ele mesmo
for(int p = 2; p <= number; p++) {
if(number%p == 1)
cout << matriz[l][c] << ": EH PRIMO E SE ENCONTRA NA LINHA: " << l << " e na COLUNA: " << c << endl;
}
}
}
return 0;
}
And the OUTPUT will be:
NOTE:
Just need correct the repetitions of OUTPUT terminal.
Welcome to Sopt. Dude, what’s your question?
– Filipe L. Constante
Thank you. .
– Matheus Vargas
You shouldn’t define
divisores
as 0 for each new value checked? In fact, why not create a function of its own to check if a number is prime?– Woss
I’m having trouble doing this. And my intention is not to use functions, if you can help me without using function thank you.
– Matheus Vargas
Note that you need to reset the divisor variable before testing each element of the matrix. This is before the loop: for (int c=1; c<=matrix[i][j]; c++). Your if test (splitters == 2) must also be inside the loop of the variable j and not after such a loop.
– anonimo