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

check for prime no

Discussion in 'C' started by imported_jitendra kumar, Aug 28, 2011.

  1. imported_jitendra kumar

    imported_jitendra kumar New Member

    Joined:
    Aug 4, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    i am writing a prog for prime no pl. tell me where is mistake in it?


    Code:
    #include<stdio.h>
    void main(){
    int i,j,k;
    printf("Enter a number");
    scanf("%d",&i);
    for(j=2;j<i;j++){
    if(i%j==0){
    printf("the given number is not a prime number");
    continue;
    }
    else{printf("a prime number");
    break;
    }
    }
    }
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    please modify as
    Code:
    #include<stdio.h>
    void main(){
    int i,j,k,c=0;
    printf("Enter a number");
    scanf("%d",&i);
    for(j=2;j<=i;j++){
    if(i%j==0)
    c++;
    }
    if(c==1)
    printf("Prime");
    else
    printf("Not a prime");
    }
     
  3. charlesth

    charlesth New Member

    Joined:
    Aug 6, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    I have check your Program.you have some little bit mistake in program but no problem i have modify.

    void main()
    {
    int i=2;
    int count=0;
    int n;
    printf("enter a no");
    scanf("%d",&n);
    for(i=2;i<n/2;i++)
    {
    if(n%i==0)
    {
    count++;
    }
    if(count>1)
    {
    prinf("prime");
    else
    printf("not prime");
    }
     

Share This Page