Check The Given Number Is A Prime Number Or Not

Source code :-

#include<stdio.h>
int main()
{
    int n,i;
    printf("Enter the number: ");
    scanf("%d",&n);
    if(n<2)
    {
        printf("\n%d is not a prime number.\n",n);
         return 1;
    }
    for(i=2;i<=n/2;i++)
    {
        if(n%i==0)
            break;
    }
    if(i>n/2)
        printf("\n%d is a prime number.\n",n);
    else
        printf("\n%d is not a prime number.\n",n);
     return 1;

Input-Output :- 

abhi@hp-15q-laptop:~$ gcc prime_number.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 123
123 is not a prime number.

abhi@hp-15q-laptop:~$ gcc prime_number.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 211
211 is a prime number. 

Print All The Prime Number Of The Given Range

Source code :

#include<stdio.h>
int main()
{
    int n,i,a,b;
    printf("Enter the lower limit of the range: ");
    scanf("%d",&a);
    printf("\nEnter the upper limit of the range: ");
    scanf("%d",&b);
    printf("\nPrime numbers are: \n\n");
    for(n=a;n<=b;n++)
    {
        for(i=2;i<=n/2;i++)
        {
            if(n%i==0)
                break;
        }
        if(i>n/2&&n>1)
            printf("%d ",n);
    }
    return 1;

Input-Output :

abhi@hp-15q-laptop:~$ gcc prime_number.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the lower limit of the range: 1
Enter the upper limit of the range:  20
Prime numbers are:  2 3 5 7 9 11 13 17 19
 

Post a Comment

Previous Post Next Post