pages

Friday, 9 April 2021

c-program to check weather a number is prime number or not in c-programming

Q.no program to check weather a number is prime number or not in c-programming.

In this section, we are learn how to check weather a number is prime number or not by entering the values by the user.

prime number: The number is only divisible by 1 and itself is called prime number.
  example:   if n=12
                       then 12 is not a prime number 
                      its factor are 1,2,3,4,6,12     
                   
                      if n=7
                      then 7 is not a prime number
                      its factor is 1 and 7 only.

#include<stdio.h>
#include<conio.h>
main()
{
    int x,i;
    printf("enter a natural number\n");
    scanf("%d",&x);
    for(i=2;i<=x-1;i++)
    {
        if(x%i==0)
            break;
    }
    if(i==x)
    {
        printf("prime number");
    }
    else
    {
        printf("not a prime number");
    }
    getch();
}

output:- x=5   prime number .

                    x=8   not a prime number.


For more c-programming:-

1.program to print all even and odd number from 1 to n natural number

2.program to print all prime number form 1 to n natural number.

3.program to check Armstrong number

No comments:

Post a Comment