C Program to Check Number is prime or Not prime

                                      C Program 



 C Program to Check Number is prime or Not prime

Examples :-



#include<stdio.h>
#include<conio.h>
void main()
{
int a, i, flag=0;
printf("enter a positive integer");
scanf("%d", &a);
if(a==0||a==1)
flag=1;
for(i=2;i<=a/2;i++)
{
if(a%i==0)
{
flag =1;
break;
}
}
if (flag==0)
printf("%d is prime number ", a);
else
printf("%d is not prime number",a);
getch();
return 0;
}




C Program to Check Number is prime or Not prime

Explain Program

Step :1

#include <stdio.h>

 Standard Input Output library, which is required for functions like printf() and scanf().

Step :2

void main()

This is the main function, where program execution starts.

Step :3

int a, i, flag = 0;

 stores the number  a, b entered by the user.

Step :4

printf ("enter a positive integer");

scanf ("%d", &a);

 user to enter a number.

 scanf () reads the input and stores it in variable a.

Step :5

if (a == 0 || a == 1)

    flag=1;

0 and 1 are not prime numbers by definition.

Step :6

for (i = 2; i <= a / 2; i++)

{

    if (a % i == 0)

    {

        flag = 1;

        break;

    }

}

loop check if any number between 2 and a/2 divides a completely

Step :7

if (flag == 0)

    printf("%d is prime number ", a);

else

    printf("%d is not prime number", a);

If no divisor was found (flag is still 0), the number is prime.


Output 

 enter a positive integer 133
   133 is not prime number


Comments

Popular posts from this blog

how to add background image in html.

HTML pre tag

How to insert image in html

Html anchor tag

Inline css in html