Before we get into the program, we need to know what is the meaning of prime numbers??, Prime numbers are numbers that are only divisible by the number 1 (one) and the number itself.
(number% 1 = 0 and the number% number = 0, the other is not).
So it can be concluded only 2 zeros.
Here we will use "%" modulus, which serves to calculate the rest of the division.
let us prove:
1% 1 = 0
2% 1 = 0
2% 2 = 0
3% 1 = 0
3% 2 = 1
3% 3 = 0
4% 1 = 0
4% 2 = 0
4% 3 = 1
4% 4 = 0
5% 1 = 0
5% 2 = 1
5% 3 = 2
5% 4 = 1
5% 5 = 0
Now you can see, the prime numbers (2, 3, 5), there are only 2 zeros.
Let's get into the program:
Code:
#include <stdio.h>
main()
{
int n, a, b, prima;
printf("Masukkan n = "); // "n" will limit any number to be checked
scanf("%d", &n);
for(a=1;a<=n;a++) // "a" is the
number to be divided
{
prima=0; // "prima" is the variable that will determine
the prime or not
for(b=1;b<=a;b++)
{
if (a%b==0)
prima++;
}
if (prima==2)
printf ("%d ",a);
}
puts("");
}
Output:
Feel free to discuss with me ^_^.

