Problem Description
This program takes a number and checks whether a given number is prime or not.
Problem Solution
1. Take a number as input.
2. Check if the number is divisible by any of the natural numbers starting from 2.
3. If it is, then it is not a prime number. Otherwise it is a prime number.
4. Exit.
Program/Source Code
Here is source code of the C program to check if a given number is prime. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to check whether a given number is prime or not
* and output the given number with suitable message.
*/
#include
#include
void main()
{
int num, j, flag;
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);
}
Program Explanation
1. Take a number as input and store it in the variable num.
2. If the number is lesser than or equal to 1, then print the output as ” It is not a prime number”.
3. Initialize the variable flag to zero.
4. Using for loop, check if the input number is divisible by any of the natural numbers starting from 2.
5. If it is, then assign the variable flag with 1.
6. Print the output as “It is a prime number”, if the variable flag ==0.
7. Otherwise print the output as “It is not a prime number” and exit.
Runtime Test Cases
Enter a number
23
23 is a prime number
Enter a number
15
15 is not a prime number
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Read Two Integers M and N & Swap their Values
» Next Page - C Program to Find the GCD and LCM of Two Integers
1
Deep Dive @ Sanfoundry:
C++ Programming Examples on Numerical Problems & Algorithms
C Programming Examples on Strings
C Programming Examples on Mathematical Functions
C# Programming Examples on Mathematics
C# Programming Examples on Events
C Programming Examples on Numerical Problems & Algorithms
C# Programming Examples on Matrix
C Programming Examples on Bitwise Operations
Java Programming Examples on Mathematical Functions
#include
void main()
{
int num, j, flag;
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);
}
Program Explanation
1. Take a number as input and store it in the variable num.
2. If the number is lesser than or equal to 1, then print the output as ” It is not a prime number”.
3. Initialize the variable flag to zero.
4. Using for loop, check if the input number is divisible by any of the natural numbers starting from 2.
5. If it is, then assign the variable flag with 1.
6. Print the output as “It is a prime number”, if the variable flag ==0.
7. Otherwise print the output as “It is not a prime number” and exit.
Runtime Test Cases
Enter a number
23
23 is a prime number
Enter a number
15
15 is not a prime number
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Read Two Integers M and N & Swap their Values
» Next Page - C Program to Find the GCD and LCM of Two Integers
1
Deep Dive @ Sanfoundry:
C++ Programming Examples on Numerical Problems & Algorithms
C Programming Examples on Strings
C Programming Examples on Mathematical Functions
C# Programming Examples on Mathematics
C# Programming Examples on Events
C Programming Examples on Numerical Problems & Algorithms
C# Programming Examples on Matrix
C Programming Examples on Bitwise Operations
Java Programming Examples on Mathematical Functions
Comments
Post a Comment