C program to check whether a given number is prime or not and output the given number with suitable message

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to check whether a given number is prime or not and output the given number with suitable message */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
int num, j, flag;

clrscr();

printf(“Enter a number\n”);
scanf(“%d”, &num);

if ( num <= 1)
{
printf(“%d is not a prime numbers\n”, num);
exit(1);
}

flag = 0;

for ( j=2; j<= num/2; j++)
{
if( ( num % j ) == 0)
{
flag = 1;
break;
}
}

if(flag == 0)
printf(“%d is a prime number\n”,num);
else
printf(“%d is not a prime number\n”, num);
}
/*————————
Output
RUN 1
Enter a number
34
34 is not a prime number

RUN 2
Enter a number
29
29 is a prime number
—————————–*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to reverse a given integer number and check whether it is a palindrome. Output the given numbers with suitable message
  2. Sample – check if a number is prime
  3. C program to check whether a given integer number is positive or negative
  4. C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings
  5. C program to find and output all the roots of a quadratic equation, for non-zero coefficients.

Leave a Comment

Previous post:

Next post: