9. Find the average of prime numbers in
a group of N numbers using function.
#include <stdio.h>
#include <stdlib.h>
{
int num1, num2, i, j;
clrscr();
printf("Enter the value of
num1 and num2 \n");
scanf("%d %d",
&num1, &num2);
{
printf("There are no primes
upto %d\n", num2);
exit(0);
}
else
{
avgprime();
}
getch();
}
avgprime(int
num1, int num2)
{
int i, j, temp, sum=0, count=0,
flag;
float avg=0.0;
printf("Prime numbers are
\n");
temp = num1;
if ( num1 % 2 == 0)
{
num1++;
}
for (i = num1; i<= num2; i = i
+ 2)
{
flag = 0;
for (j = 2; j <= i / 2; j++)
{
if ((i % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("%d\n",
i);
sum=sum+i;
count++;
}
}
avg=(float)sum/count;
printf("\nAverage of prime
numbers=%f",avg);
printf("\nNumber of primes
between %d & %d = %d\n", temp, num2, count);
}
No comments:
Post a Comment