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(); )



21 comments:

  1. what a lovely programme
    thanks a lot

    ReplyDelete
  2. 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
  3. print this
    23 21 24 19 26 15 28 11 30 7 36

    ReplyDelete
  4. 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
  5. 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
  6. 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
  7. 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
  8. my program exits time limit...
    what I do...

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

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

    ReplyDelete
  10. 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
  11. Prime Number Distribution Series (PNDS), developed by the NSI team of mathematicians2 which computes the exact count of primes numbers.

    ReplyDelete
  12. 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
  13. It is nice article to improve knowledge.thank you for sharing article.
    web programming tutorial
    welookups

    ReplyDelete