ı have a problem pls help

10-element array of those averages in a program to find prime

my code:


#include<stdio.h>
#include<stdlib.h>

int main()
{
int array[10],i,a,j;
printf("enter the numbers\n");
for(j=0;j<10;j++){
scanf("%d",&array[j]);
}
a=1;
for(j=0;j<10;j++)
for (i=2;i<=array[j]-1;i++)
{
if (array[j]%i==0)
a=0;
}

for(j=0;j<10;j++){
if (a!=0)
printf("%d number is prime\n",array[j]);
else
printf("%d number isn't prime\n",array[j]);
}

system("pause");
return 0;
}



but is isn't working. please help me
Last edited on

It is always going to show the last value of a. Make a an array of may be bool.

bool a[10];

and in your loop, do this:

a[j] = 0; //so for each number in array, you will have equivalent true or false value in a

Also, as soon you find a number is not prime, break from the loop. Otherwise what will happen, if that number is non divisible from the next number, it will say it's prime when actually it is not as it was divisible from some other previous number.

Also, to check for primes, you dont have to go till that number, till half of that number is fine.

so lets say to check for prime for 10, you just have to check till 5.

** Always put your code in code tags, indentation is important if you want more replies.
Last edited on
Topic archived. No new replies allowed.