Monday 7 July 2014

Write a Program in C to Determine Whether a Number is Prime or Not.


Program in C to Determine Whether a Number is Prime or Not.
"A prime number is one, which is divisible only by 1 or itself."

To determine whether a number is prime or not, we have to divide the number successively by all numbers from 2 to one less than itself.

If remainder of any of these divisions is zero, the number is not a prime.

If no division yields a zero then the number is a prime number.



Program in C to Determine Whether a Number is Prime or Not


Steps:

  1. Declare integers num and i inside main()
  2. Prompt the message allow the input using printf() and scanf()
  3. Test whether a Number is prime or not using while() function.

Code:

#include <stdio.h>

int main()
{

int num, i;


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

i=2;

while (i<=num-1)
{

if (num%i==0)
{
printf("Not a prime number\n");
break;
}

i++;
}

if (i==num)

printf("Prime number\n");

return 0;
}



Related Posts: 



No comments:

Post a Comment

Powered by Blogger.

مساحات للبيع

اعلان

معلومات عنا

إعلانات

إجمالي مرات مشاهدة الصفحة

Search This Blog

اخبار عالمية

مقالات

أخبار

أخر المعلقين

فيديوهات

سلايدر

Tags

Contact us

متتبعون المدونة

Categories

Advertisement

40% Off

Translate

صور متنوعة

Facebook

Facebook users

Recent in Mens

FlatBook

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus vehicula tortor, vel cursus elit. Donec nec nisl felis. Pellentesque ultrices sem sit amet eros interdum, id elementum nisi ermentum.Vestibulum rhoncus vehicula tortor, vel cursus elit. Donec nec nisl felis. Pellentesque ultrices sem sit amet eros interdum, id elementum nisi fermentum.




Comments

Contact Us

Name

Email *

Message *