1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Correct this c program please

Discussion in 'C' started by imported_c_newbie, Mar 8, 2011.

  1. imported_c_newbie

    imported_c_newbie New Member

    Joined:
    Dec 18, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    the following is a program to print prime numbers after taking user input of the number of prime numbers the user wants to print.. please correct the mistakes in this program.. also tell me if there are any mistakes in the programing style or any area where i need to work more..

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        int num;
        
        printf("Enter number..");
        scanf("%d", &num);
        
        if(num==1)
        printf("\n1");
        
        else if(num==2)
        {
             printf("\n1\n2");
             
        }
        else
        
        {
            printf("\n1\n2\n");
            
            int divisor,i,rem;
            
            for(i=3;i<num;i++)
            {
              divisor=2;
              while(rem!=0 && divisor<i)                
              {
              {
               rem=i%divisor;
               divisor++;
              }
              if(rem!=0)
              {
              printf("%d\n", i);
              }
              else
              continue;
              }        
            }
        } 
        
        getch();
        return 0;
    }
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void main()
    {
    	int reqprimes, primesfound, isprime ;
    	long testno, testfac;
    	
    	printf("no of primes to check : ");
    	scanf("%d", &reqprimes);
    	
    	
    	for( primesfound = 0, testno  = 1; primesfound <= reqprimes; testno++ )
    	{
    		isprime = 1;
    		for( testfac = 2; testfac * testfac <= testno && isprime; testfac++ )
    		{
    			if( testno % testfac == 0 )
    				isprime = 0;
    		}
    		
    		if( isprime )
    		{
    			printf("%ld is prime\n", testno);
    			primesfound++;
    		}
    	}
    }
    
     
  3. manoj kummar

    manoj kummar New Member

    Joined:
    Mar 6, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    yar boss it is very complicated programme...
    i have corrected, but i am giving any number it is display only 1,2,3...

    it is simple prime no.. program.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(void)
    {
    clrscr();
    int i, num=1;
    while(num<=1000)
    {
    i=2;
    while(i<=num)
    {
    if(num%i==0)
    break;
    i++; }
    if(i==num)
    printf("%3d, ", num);
    num++; }
    getch();
    }
     
  4. chandanpatra

    chandanpatra New Member

    Joined:
    Mar 25, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    eriyer has the perfect solution for it with his code fully optimized in order to have minimum iteration to find the list of primes.
     

Share This Page