Jump to content

C program to detect prime numbers

- - - - -

  • Please log in to reply
8 replies to this topic

#1
sobersaber

sobersaber

    Newbie

  • Members
  • Pip
  • 6 posts
Hi, this is a program to detect prime numbers or not. I understand what the program does except for the i++ part. Can someone tell me why does the program have i++ included?

#include <stdio.h>

int main()

{

    int num,i;

    printf("Enter a number");

    scanf("%d",&num);

    i=2;

    while(i<=num-1)

    {

        if(num%i==0)

        {

        printf("Not a prime number\n");

        break;

    }

[B]    i++;

[/B]}



if(i==num)

    printf("Prime number\n");

}   


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
What is about this? :)

#include <stdio.h>

int main()

{

    int num,i;

    printf("Enter a number");

    scanf("%d",&num);

    i=2;

    while(i<=num-1)

    {

        if(num%i==0)

        {

            printf("Not a prime number\n");

            break;

        }

        i++;

    }



    if(i==num)

        printf("Prime number\n");

}



#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
i++ is just fancy way of saying i = i + 1

Also, when checking for prime's, the fastest way I know of is checking up to square of that number.w
#include <iostream>
#include <cmath>

bool is_prime(int n) {
    if (n < 2)  // 0 and 1 and not primes
        return false;

    int sn = sqrt(abs(n));  // abs if negative values is passed, and sqrt for speed

    for (int i = 2; i < sn + 1; ++i)
        if ((n % i) * n == 0)
            return false;
    return true;
}

int main() {

    for (int i = 0; i < 20; ++i)
        if (is_prime(i))
            std::cout << i << " is prime\n";

    return 0;
}

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
nerio

nerio

    Newbie

  • Members
  • PipPip
  • 14 posts

Flying Dutchman said:

Also, when checking for prime's, the fastest way I know of is checking up to square of that number.

Also, when checking for prime's, the fastest way I know of is checking up to square root of that number.
I am a student of C language.

#5
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
i++ here is increment of that number. i++ also same with i=i+1. For example, it starts from 2. And increments by one. It checks until num-1. Firstly it checkes 2, then 3,4,5, etc. If you dont put increment, then your program wont work. It ll only work with 2.

#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

The program is running in a loop where it determines whether given number is a prime or not.
The logic is that it loop goes on until one of the following happens. i++ provides a next number each turn the loop runs

1. number is dividable by i
2. i reaches equal to number.

if you remove i++: the loop would go on forever:

Munir

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

nerio said:

Also, when checking for prime's, the fastest way I know of is checking up to square root of that number.
Yes, I meant that and didn't notice I forgot. Thank you for correcting :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Just to add a reference to Sieve of Eratosthenes algorithm for finding all prime numbers in range 2-N for any given N. It operates very fast, and can be memory optimized by spending 1 bit per number, meaning that N/8 bytes are required to find all prime numbers up to N.

#9
sobersaber

sobersaber

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks all! I now understand the entire program. It took me a while to understand the logic, it wasn't easy!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users