Reputation: 13
I don't get how to fix this code so that it works:
int Numbers_Given[97];
int n;
for (n = 3; n <= 100; ++n) {
Numbers_Given[n - 3] = n;
// printf("%d ", Numbers_Given[n - 3]);
}
int i;
for (n = 0; n < 97; n++) {
for (i = 2; i < Numbers_Given[i - 2]; i++) {
if (Numbers_Given[i - 2] % i == 0)
break;
else (printf("%d ", Numbers_Given[i - 2]));
}
}
return 0;
}
It seems like it's just not comprehending the %
. What's going on? It prints out the original array from 3
to 100
, over and over again. Prime numbers are not printed out.
Upvotes: 0
Views: 155
Reputation: 44274
There are multiple problems.
Your array is too small
You are using i-2
as index when you should use n
You are printing before you have checked all %
values.
Try this:
int main()
{
int Numbers_Given[98]; // because highest index is 100-3 = 97
int n;
for (n=3;n<=100;++n)
{
Numbers_Given[n-3]=n;
}
int i;
for (n=0;n<=97;n++)
{
int prime = 1; // Assume that Numbers_Given[n] is a prime
for(i=2;i<Numbers_Given[n];i++) // use Numbers_Given[n]
{
if (Numbers_Given[n]%i==0)
{
prime = 0; // Not a prime so clear the flag
break;
}
}
if (prime) (printf("%d ", Numbers_Given[n])); // Print if it's a prime
}
return 0;
}
BTW: When checking the remainder (%
) you are testing all numbers from 2
to Numbers_Given[n] - 1
. You don't need that, you can stop earlier (i.e. sqrt(Numbers_Given[n])
)
Upvotes: 4
Reputation: 50
First problem, as @user3386109 pointed out, that you should use n<100
instead of n<=100
as Number_Given[97]
is the array itself so last iteration go one past of it which doesn't make any difference in output but avoid using such is a good practice for future codes.
Here the problem is that in inner loop you seem to run it with condition i<Numbers_Given[i-2]
but the problem is the moment you increase the value of i
with i++
the value of Number_given[i-2]
changes and so it never reaches the end. Let's say we take first iteration i=2
then after first iteration i++
changes it to i=3
but now Number_given[i-2]
is referred to Number_given[1]
which is 4
so still i<4
and condition follows so the loops runs and then the next loop i=4
but then again Numbers_Given[i-2]
will be Numbers_Given[2] = 5
so it satisfies the condition i<5
again. Same goes for every loop. So you get the number range over and over.
To solve this change Numbers_Given[i-2]
to Numbers_Given[n]
as now after increment of i
, n
is independent of it and so doesn't change the value and strict the condition.
Moreover even after replacing Numbers_Given[i-2]
to Numbers_Given[n]
the problem is after failing to follow ' if (Numbers_Given[n]%i==0)' statement the else runs and output the number but it will continue to do so for every value of i
so the output will look like
3 5 5 5 7 7 7 7 7 9 11 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 13 13 13 15 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 21 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 25 25 25 27 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 33 35 35 35 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 39 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 45 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 49 49 49 49 49 51 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 55 55 55 57 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 63 65 65 65 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 69 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 75 77 77 77 77 77 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 81 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 85 85 85 87 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 91 91 91 91 91 93 95 95 95 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 99
So to solve this we simply break
our loop whenever it reaches the else
statement meaning we get out prime number the first time we get it and then don't check it again.
So the final code will look like this which outputs all the prime numbers from an array of 3 to 100.
#include <stdio.h>
#include <stdbool.h>
int main()
{
int Numbers_Given[98];
int n;
for (n=3;n<=100;++n)
{
Numbers_Given[n-3]=n;
// printf("%d ", Numbers_Given[n-3]);
}
int i;
for (n=0;n<97;n++)
{
bool flag = 1;
for(i=2;i<Numbers_Given[n];i++){
if (Numbers_Given[n]%i==0){
flag = 0;
break;
}
}
if (flag) {
printf("%d ", Numbers_Given[n]);
}
}
return 0;
}
whose output:
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
P.S:- As others said ask question with complete information like what you expect and instead what appears, error codes you get(if any), etc., lastly edit the question to make it easier for people to interpret.
One more thing 2 is a prime also, :) just a side info.
Upvotes: 0