You're online now.

Hurray! you are online now.

Prime number program in c

Prime Number is a number which divisible by 1 and own so we called the number is prime. The condition is number is greater than 1. A number is divisible by itself or 1 so called the prime number.

// Method 1: To check given number is prime or not c program.
    #include<stdio.h>
    
    int main()
    {
        int n;
        printf("Enter the number : ");
        scanf("%d", &n);
        int count = 0;
        if(n==1){
            count = 1;
        }
        for(int i=2; i<n; i++){
            if(n%i==0){
                 count++;
            }
        }
    
        if(count==0){
           printf("%d is prime", n);
        }
        else{
           printf("%d is not prime", n);
        }
        return 0;
    
    }
// Method 2: To check the given number is prime or not c program.
    
    #include<stdio.h>
    
    int main()
    {
        int n;
        printf("Enter the number : ");
        scanf("%d", &n);
        int count = 0;
        int flag = 0;
        if(n==1){
            flag = 1;
        }
        for(int i=2; i<=n/2; i++){
            if(n%i==0){
                flag = 1;
            }else{
                count++;
            }
        }
        
        if(flag==0){
            printf("%d is prime", n);
        }
        else{
            printf("%d is not prime", n);
        }
    
        return 0;
    }
    
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...