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

C Twinprimes finder

Discussion in 'C' started by SpamAreI, Feb 12, 2008.

  1. SpamAreI

    SpamAreI New Member

    Joined:
    Feb 12, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi I'm trying to write a short program to find twinprimes (a prime with a difference of 2 to the prime below it). I've been scanning and rewriting it for hours, but can't figure out what's wrong :/

    Code:
    
    #include<stdio.h>
    main()
    {
      int noprime=0, num=2, count=0;
      int primes,test1,firstP;
      printf("How many twin primes do you want to find?");
      scanf("%d",&primes);
      while(count<primes){
        noprime=0;
        for(test1=1;test1<num;test1++){
          if(num%test1==0){
            noprime=1;
            break;
          }
          if(noprime){
            printf("%d is not prime\n",num);
            num++;
          }
          else{
            if(num-firstP==2){
              printf("%d is a prime and a twin prime in the prime pair %d%d\n",num,firstP,num);
              firstP=num;
              num++;
              count++;
            }
            else{
              printf("%d is prime but not a twin prime\n",num);
              firstP=num;
              num++;
            }
          }
        }
      }
    }
     
    Last edited by a moderator: Feb 12, 2008
  2. SpamAreI

    SpamAreI New Member

    Joined:
    Feb 12, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    nevermind, I got it o_O
     

Share This Page