Program of Prime Number in C:
Hello and Welcome to www.TechWithCode.com
Today we will learn about how to write a C Program to check whether a number is prime or not.
As We Know that A Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers.
Actually, we have to implement the above definition of prime number in c programming.
So let's see the program of prime number in c. In this C program, we will take input from the user and check that whether this number is Prime or not.
Problem Statement⤵️
Write a C program that takes a number as input and check whether this number is Prime or Not.
Source Code⤵️
//Summary: Check whether a number is prime or not[ www.TechWithCode.com ]
#include <stdio.h>
int main() {
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i) {
if(n%i==0) {
flag=1; break;
}
}
if (flag==0)
printf("\n\n%d is a prime number.\n\n",n);
else
printf("\n\n%d is not a prime number.\n\n",n);
return 0;
}
No comments:
Do not add any link in the comments.
For backlink, contact us.