I think the problem is in your for.. loop.
But try this:
where there is this;
#
if (c==0)
#
printf("Prime");
#
x++;
#
primecount++
put this;
#
if (c==0){
#
printf("Prime");
primecount++
}
#
x++;
I guess it works.
Otherwise your code is too complicated. Try making use of functions.
roottybrian
Junior Poster in Training
54 posts since Aug 2008
Reputation Points: 10
Solved Threads: 10
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int x;
int y;
int i;
int c;
int primecount;
int average = average + ;
printf( "Enter two positive integers --> " );
scanf("%d%d" , &x, &y );
while (y >= x )
{
c=0;
printf ( "\n %d:\t" , x );
for(i=2;i<x;i++)
if(x%i==0)
{
printf(" %d ",i);
c++;
}
/** the following code should be outside the FOR loop.
You are testing one number (2) and printing "Prime"
if x%2 != 0. Then you increment x AND primecount
no matter what.
Next time through the loop you are testing x+1 with 3,
then x+2 with 4, and so on.... **/
if (c==0)
printf("Prime");
x++;
primecount++
printf( "\nThere are %d Prime numbers.\n", primecount );
printf( "The average of the prime numbers is %.2f\n", average );
printf( "Enter two positive integers --> " );
scanf("%d%d" , &x, &y );
}
return 0;
}
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
#include <stdio.h>
pls use this program it is working.
int main ()
{
int sayi, sayi2, k1, k2, flag;
int total=0,count=0;
printf (" enter first integer number : ");
scanf (" %d", &sayi);
printf ("enter second integer number : ");
scanf (" %d", &sayi2);
for (k1=sayi; k1 < sayi2 ;k1++)
{
flag = 1;
for (k2 = 2; k2 < k1; k2 ++)
{
if (k1 % k2 == 0)
{
flag = 0;
break;
}
}
if (flag == 1) {
printf (" %d prime number \n", k1 );
total=total+k1;
count=count+1;
}
}
printf ("\n total of prime number = %d ",total);
printf ("\n average = %d", total/count);
getchar();getchar();
return 0;
}
burcin erek
Junior Poster in Training
62 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1