<>

Prime Number C Program


C Program which tell input number is prime or not:

#include<stdio.h>
main()
{
    int x,c;

    printf("Enter a number: ");
    scanf("%d",&x);

    for (c = 2 ; c <= x - 1 ; c++ )
    {
        if ( x%c == 0 )
        {
            printf("%d is not prime.\n", x);
            break;
        }
    }
    if ( c == x )
        printf("%d is prime.\n", x);
}


Also by Making Function of it:

#include<stdio.h>
void prime(int);
main()
{
    int x;

    printf("Enter a number: ");
    scanf("%d",&x);

    prime(x);
}
void prime(int x)
{
    int c;

    for (c = 2 ; c <= x - 1 ; c++ )
    {
        if ( x%c == 0 )
        {
            printf("%d is not prime.\n", x);
            break;
        }
    }
    if ( c == x )
        printf("%d is prime.\n", x);
}

C Program which input a integer and print that numbers of prime numbers:

#include<stdio.h>
main()
{
    int n, i = 3, count, c;

    printf("Enter the number you want the prime number: ");
    scanf("%d",&n);

    if ( n >= 1 )
    {
        printf("First %d prime numbers are :\n",n);
        printf("2\n");
    }

    for ( count = 2 ; count <= n ;  )
    {
        for ( c = 2 ; c <= i - 1 ; c++ )
        {
            if ( i%c == 0 )
                break;
        }
        if ( c == i )
        {
            printf("%d\n",i);
            count++;
        }
        i++;
    }
}

C Program which prints the Prime number according to given range:

#include<stdio.h>
main()
{
    int x,c;
    int m,n;

    printf("Enter the First number: ");
    scanf("%d",&m);

    printf("Enter the Last number: ");
    scanf("%d",&n);

    printf("\nAll Prime numbers between these range are: \n");

    for(x=m; x<=n; x++)
    {
        for (c = 2 ; c <= x - 1 ; c++ )
        {
            if ( x%c == 0 )
                break;
        }
        if ( c == x )
            printf("%d\n", x);
    }
}

Rate this posting:
{[['']]}

0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
TOP