Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 391593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:04:36+00:00 2026-05-12T16:04:36+00:00

I am trying to come up with a method that takes an integer and

  • 0

I am trying to come up with a method that takes an integer and returns a boolean to say if the number is prime or not and I don’t know much C; would anyone care to give me some pointers?

Basically, I would do this in C# like this:

static bool IsPrime(int number)
{
    for (int i = 2; i < number; i++)
    {
        if (number % i == 0 && i != number)
            return false;
    }
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:04:36+00:00Added an answer on May 12, 2026 at 4:04 pm

    OK, so forget about C. Suppose I give you a number and ask you to determine if it’s prime. How do you do it? Write down the steps clearly, then worry about translating them into code.

    Once you have the algorithm determined, it will be much easier for you to figure out how to write a program, and for others to help you with it.

    edit: Here’s the C# code you posted:

    static bool IsPrime(int number) {
        for (int i = 2; i < number; i++) {
            if (number % i == 0 && i != number) return false;
        }
        return true;
    }
    

    This is very nearly valid C as is; there’s no bool type in C, and no true or false, so you need to modify it a little bit (edit: Kristopher Johnson correctly points out that C99 added the stdbool.h header). Since some people don’t have access to a C99 environment (but you should use one!), let’s make that very minor change:

    int IsPrime(int number) {
        int i;
        for (i=2; i<number; i++) {
            if (number % i == 0 && i != number) return 0;
        }
        return 1;
    }
    

    This is a perfectly valid C program that does what you want. We can improve it a little bit without too much effort. First, note that i is always less than number, so the check that i != number always succeeds; we can get rid of it.

    Also, you don’t actually need to try divisors all the way up to number - 1; you can stop checking when you reach sqrt(number). Since sqrt is a floating-point operation and that brings a whole pile of subtleties, we won’t actually compute sqrt(number). Instead, we can just check that i*i <= number:

    int IsPrime(int number) {
        int i;
        for (i=2; i*i<=number; i++) {
            if (number % i == 0) return 0;
        }
        return 1;
    }
    

    One last thing, though; there was a small bug in your original algorithm! If number is negative, or zero, or one, this function will claim that the number is prime. You likely want to handle that properly, and you may want to make number be unsigned, since you’re more likely to care about positive values only:

    int IsPrime(unsigned int number) {
        if (number <= 1) return 0; // zero and one are not prime
        unsigned int i;
        for (i=2; i*i<=number; i++) {
            if (number % i == 0) return 0;
        }
        return 1;
    }
    

    This definitely isn’t the fastest way to check if a number is prime, but it works, and it’s pretty straightforward. We barely had to modify your code at all!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.