·
1 min read
Слушать

Optimized Approach (Checking Up to √N)


Optimized Approach (Checking Up to √N)

Instead of checking up to num/2, we can optimize by checking up to √num:


c

Copy

Edit

#include

#include


int main() {

    int num, i, isPrime = 1;

    

    printf("Enter a number: ");

    scanf("%d", &num);


    if (num <= 1) {

        isPrime = 0;

    } else {

        for (i = 2; i <= sqrt(num); i++) {

            if (num % i == 0) {

                isPrime = 0;

                break;

            }

        }

    }


    if (isPrime)

        printf("%d is a Prime number.\n", num);

    else

        printf("%d is not a Prime number.\n", num);https://docs.vultr.com/clang/examples/check-whether-a-number-is-prime-or-not

0
Give Award

chetankumar kumar

How to Get the ASCII Value of a Character in C In C programming, each character has a unique ASCII (American Standard Code for Information Inter…

Other author posts

Comments
You need to be signed in to write comments

Reading today

Ryfma
Ryfma is a social app for writers and readers. Publish books, stories, fanfics, poems and get paid for your work. The friendly and free way for fans to support your work for the price of a coffee
© 2025 Ryfma. All rights reserved 12+