Tutorial 6_Q1

Display all prime numbers between two Intervals using a function

#include<stdio.h>                                                                next                                                            
int PrimeNumber(int n);
int PrimeNumber(int n)
{
int j , x = 1;
for (j = 2; j <= n/2; j++)
{
if (n%j == 0)
{
x = 0;
break;
}  
}
return x;
}

int main ()
{
int n1,n2,i,x;
printf("Enter the starting value :");
scanf("%d",&n1);
printf("Enter the ending value :");
scanf("%d",&n2);
printf("prime numbers between %d and %d :",n1,n2);
for (i = n1 + 1; i < n2; i++)
{
x = PrimeNumber(i);
if (x == 1)
{
printf("%3d ",i);
}
}
return 0;
}


0 comments:

Post a Comment