int isPrime(long long number) {
if (number == 0 || number == 1 || (number % 2 == 0 && number > 2)) {
return 0;
}
else {
for (long long i=3; i <= (long long)sqrt(number)+1; i++) {
if (number % i == 0) {
return 0;
}
}
return 1;
}
}
bool isPrime(int x = 0)
{
/* Without extra 'count' variable */
//! corner case: 0 and 1 aren't prime numbers
if (x == 0 || x == 1)
return 0;
for (int i = 2; i <= x / 2; i++)
{
if (x > 2 && x % i == 0)
return 0;
}
return 1;
}
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
//logic
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
Program Output:
#include<stdio.h>
void main(){
int num, i;
int cp = 1;
printf("Enter a number = ");
scanf("%d",&num);
if(num > 0){
for(i = 2; i < num; i++){
if(num % i == 0){
cp = 0;
}
}
if(cp == 1){
printf("%d is a prime number.", num);
}
else{
printf("%d isn't a prime number.", num);
}
}
}
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
int isPrime(int n) {
for (int i = 2; i < n; i++) if (n % i == 0) return 0;
return 1;
}
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
int n=7,i,flag=0;
for(i=2;i<n/2;i++){
if(n%i==0){
flag=1;
break;
}
}
if(flag==0){
printf("Prime number");
}else{
printf("not prime number");
}
Code Example |
---|
C :: calculator in c |
C :: arduino digital read |
C :: Succ de ch |
C :: how to genrate a random number in C |
C :: factorial in c using recursion |
C :: write a program in c to check whether the number is armstrong or not |
C :: search array element in c |
C :: merge sort code c |
C :: c bit access union |
C :: nested switch case in c |
C :: c program to add two numbers |
C :: get last char string c |
C :: typedef pointer |
C :: uuidv4 javascript |
C :: c language time() function |
C :: bash while loop n times |
C :: c convert char to int |
C :: how to get input in 2d array in c |
C :: remove string from string c |
C :: c functions example |
C :: c pointers |
C :: how to malloc for matrix in c |
C :: number pattern in c |
C :: Leap year using function in c |
C :: Float and Double Input/Output |
C :: getchar c |
C :: c add char to char array |
C :: c unused parameter |
C :: c programming programiz |
C :: C #ifdef Directive |