Prime Numbers in C?

Posted by Ali Azam Rana on Stack Overflow See other posts from Stack Overflow or by Ali Azam Rana
Published on 2012-11-25T10:55:08Z Indexed on 2012/11/25 11:03 UTC
Read the original article Hit count: 237

Filed under:
|
|
|
|

FIRST PROGRAM

#include<stdio.h>
void main()
{
    int n,c;
    printf("enter a numb");
    scanf("%i",n);
    for(c=2;c<=n;c++)
    {
        if(n%c==0)
            break;
    }
    if(c==n)
        printf("\nprime\n");
    else
        printf("\nnot prime\n");
    getchar();
}

SECOND PROGRAM

#include <stdio.h>
int main()
{
    printf("Enter a Number\n");
    int in,loop,rem,chk;
    scanf("%d",&in);
    for (loop = 1; loop <=in; loop++)
    {
        rem = in % loop;
        if(rem == 0)
        chk = chk +1;
    }
    if (chk == 2)
        printf("\nPRIME NUM ENTERED\n");
    else
        printf("\nNUM ENTERED NOT PRIME\n");
    getchar();
}

the 2nd program works other was the one my friend wrote the program looks fine but on checking it by stepping into we found that the if condition in first program is coming true under every input so whats the logical error here please help me found out......

© Stack Overflow or respective owner

Related posts about c

    Related posts about loops