Program for prime numbers

Here is a program to find whether the entered number is a prime or not. A prime number is a one, whose divisors are 1 and the number itself.

Logic: The program expects the user to enter the number to check for prime property. The for loop in the program traces the iteration till the number, while in each of iteration it checks the present number is the divisor of the entered or not. If it is, it sets the flag to 1 and breaks off, which effectively prints out the result through the if block.

The same algorithm is modified slightly, and developed a program to print all the prime number till the user defined range.

Program to find whether a number is a prime or not

#include<stdio.h>
void main()
{
int i, prime = 1, n;
clrscr();
printf(“\n\n\t ENTER A NUMBER…: “);
scanf(“%d”, &n);
for(i=2; i<n; i++)
{
if(n%i == 0)
{
prime = 0;
break;
}
}
if(prime)
printf(“\n\n\t THE NUMBER %d IS A PRIME NUMBER”, n);
else
printf(“\n\n\t THE NUMBER %d IS NOT A PRIME NUMBER”, n);
getch();
}

Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

One thought on “Program for prime numbers

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in