I have been trying to fix this since two eeks and I have no clue what I am doing wrong. I have gotten the program to work so that it can tell if a number is prime or not, but the problem I am having is showing at the end how many prime numbers there are for the two numbers, calculating the average and lastly asking the user again to input numbers. Can anyone help please!

Here is the my code

#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++;
			}
			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;
}
Re: Prime numbers help 80 80

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.

Re: Prime numbers help 80 80
#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;
}
Re: Prime numbers help 80 80
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{

	int x; 
	int y; 
	int i;
	int c;

	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++;
			}
			if (c==0)
				printf("Prime");
			x++;
			
	}
	return 0;
}

rootybrain - Well I tried putting the additional curly brackets and still have no results. If you have any suggestions. Also I just started, and this is my first time with C coding, and my professor wants us to stick with the basics until the later chapters.

WaltP - If I type the above code then it works fine by telling the user if the numbers between x and y are prime and their factor. The real problem for me starts when I want the user to keep typing two integers (loop) until x>y. Also I am having trouble finding the average of the prime numbers between the two integers which I know how to do somewhat and the average of the prime numbers.

Re: Prime numbers help 80 80
#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;
}
Re: Prime numbers help 80 80
#include<stdio.h>
#include<conio.h>
void main()
{
 int flag=0;
 int a=11,i,temp,temp=a;
 for(i=1;i<=temp;i++)
 {
if((a%i)==0)
{flag++;
}
}
if(flag==2)
{
 printf("prime");
 }
Re: Prime numbers help 80 80
#include<stdio.h>
#include<conio.h>
void main()
{
 int flag=0;
 int a=11,i,temp,temp=a;
 for(i=1;i<=temp;i++)
 {
if((a%i)==0)
{flag++;
}
}
if(flag==2)
{
 printf("prime");
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.