Search
 
SCRIPT & CODE EXAMPLE
 

CPP

prime number program in c

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;
}
Comment

prime numbers c

#include<stdio.h>
void main()
{
    int num;
    int prime = 1;
    printf("Enter a number = ");
    scanf("%d",&num);


    for(int i = 2; i < num; i++)
    {
        if(num % i == 0)
        {
            prime = 0;
            break;
        }
    }
    if(prime == 1 && num>0 && num !=1)
        printf("%d is a prime number.", num);
    else
        printf("%d isn't a prime number.", num);
    
}
Comment

Prime Number Check Program in C

#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:
Comment

prime number in c

#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);
        }
    }
}
Comment

prime number c program

int isPrime(int n) {
  for (int i = 2; i < n; i++) if (n % i == 0) return 0; 
  return 1;
}
Comment

prime number in c

#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;  
 }    
Comment

PREVIOUS NEXT
Code Example
Cpp :: qt label set text color 
Cpp :: PI IN C++ WITH CMATH 
Cpp :: #pragma once in main file what is it for 
Cpp :: c++ main environment variables 
Cpp :: input a string in c++ 
Cpp :: c++ vector add only unique elements 
Cpp :: cpp float to int 
Cpp :: c++ vector element search 
Cpp :: Array sum in c++ stl 
Cpp :: how to make a hello world program in c++ 
Cpp :: sfml mouse button pressed 
Cpp :: extends c++ 
Cpp :: iterating string in cpp 
Cpp :: dynamically generating 2d array in cpp 
Cpp :: how to run a c++ program in the background 
Cpp :: what is the short cut way to find the max and min element in an array in c++ 
Cpp :: remove first element from vector c++ 
Cpp :: c++ extend class 
Cpp :: prints out the elements in the array c++ 
Cpp :: multiline comment in c++ 
Cpp :: cpp convert vector to set 
Cpp :: vector of strings initialization c++ 
Cpp :: do while loop c++ loops continuously 
Cpp :: vector fin element c++ 
Cpp :: armstrong number in cpp 
Cpp :: ray sphere intersection equation 
Cpp :: log base 10 c++ 
Cpp :: append string cpp 
Cpp :: check if whole string is uppercase 
Cpp :: c++ pi float 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =