Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

7 Aug 2012

C program to print Prime Numbers Between 1 and n

/ * c program to print Prime Numbers Between 1 and N * /
#include<stdio.h>
void main()
{
int num,i=1,j,count;
//clrscr();
printf("Enter Num value To Print Prime Numbers between 1 and Num: ");
scanf("%d",&num);
printf("Prime Numbers upto %d :\n \n",num);

while(i<=num)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0) //checking whether num is dvisible by j
count++;
}
if(count==2)  //if num is divisible by 2 numbers,then it is prime
printf("%d ",i);
i++;
}
printf("\n\n");

//getch(); 
}
Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )



23 comments:

  1. Check out programming video tutorials on this YOUTUBE Channel

    ReplyDelete
  2. Check out programming video tutorials on this YOUTUBE Channel

    ReplyDelete
  3. what a lovely programme
    thanks a lot

    ReplyDelete
  4. this is a PYTHON program to calculate the prime factors of a number: ( quite fast and self made :p)


    number=int(input('enter the number to be factorized'));
    print ('the entered number was %d'%number);
    divisor=2;
    import math
    sr=math.sqrt(number);
    i=0;
    factors=dict({});
    while number%2==0:
    nwnumber=number/divisor;
    factors[i]=divisor;
    number=nwnumber;

    i=i+1;
    if number==0:
    break;

    divisor=3;
    nwnumber=number;

    while number!=1:


    if nwnumber%divisor==0 :
    factors[i]=divisor;
    nwnumber=nwnumber/divisor;
    i=i+1
    continue;
    elif nwnumber==1:
    break;

    elif math.sqrt(nwnumber)>divisor:
    divisor=divisor+2
    i=i+1
    else:
    i=i+1;
    factors[i]=nwnumber;
    break;

    print ('the prime factors of the entered number are :');
    print dict.values(factors)








    ReplyDelete
  5. print this
    23 21 24 19 26 15 28 11 30 7 36

    ReplyDelete
  6. I cam across this program. It seems interesting and easy to understand.
    http://www.codingalpha.com/prime-number-c-program/#include

    int main()
    {
    int count=2,num;
    printf("Enter a Number:\t");
    scanf("%d",&num);
    for(count=2;count<=num-1;count++)
    {
    if(num%count==0)
    {
    printf("%d is not a Prime Numer\n",num);
    break;
    }
    }
    if(count==num)
    {
    printf("%d is a Prime Number\n",num);
    }
    }Source: http://www.codingalpha.com/prime-number-c-program/

    ReplyDelete
  7. This point has dependably been one of my most loved subjects to peruse about. I have observed your post to be exceptionally energizing and brimming with great data. I will check your different articles in the blink of an eye.. Buzz Applications

    ReplyDelete
  8. A much faster algorithm would be to Check for only those numbers whose remainder is 1 or 5 when divided by 6 , but this algorithm excludes 2 and 3. So those could be taken care of

    ReplyDelete
  9. A much faster algorithm would be to Check for only those numbers whose remainder is 1 or 5 when divided by 6 , but this algorithm excludes 2 and 3. So those could be taken care of

    ReplyDelete
  10. A much faster algorithm would be to Check for only those numbers whose remainder is 1 or 5 when divided by 6 , but this algorithm excludes 2 and 3. So those could be taken care of

    ReplyDelete
  11. my program exits time limit...
    what I do...

    ReplyDelete
    Replies
    1. i did not get you, can u plz explain clearly ?

      Delete
  12. my program exits time limit...
    what I do...

    ReplyDelete
  13. Prime Number Program in C

    #include
    #include
    void main()
    {
    int num,res=0;
    clrscr();
    printf("\nENTER A NUMBER: ");
    scanf("%d",&num);
    res=prime(num);
    if(res==0)
    printf("\n%d is a prime number",num);
    else
    printf("\n%d is not a prime number",num);
    getch();
    }
    int prime(int n)
    {
    int i;
    for(i=2;i<=n/2;i++)
    {
    if(n%i!=0)
    continue;
    else
    return 1;
    }
    return 0;
    }

    ReplyDelete
  14. Prime Number Distribution Series (PNDS), developed by the NSI team of mathematicians2 which computes the exact count of primes numbers.

    ReplyDelete
  15. What is a prime number? This definition explains what a prime number is and lists examples. See also different types of primes and links to information about prime number distribution .

    ReplyDelete