954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

prime number question

i 'm trying to make this program of a prime number in c language but ive not been able to do so,can any 1 of u plz help me out...i want to do 2 things...
1)First,test whether a number is prime or not
2)generate the series of all the prime numbers till the limit of that number...for example..i want to test whther a number 16 is prime or not and then print all the series of prime numbers upto 16....how can i do this...any help would be much appreciated ...hope any1 can write the code so that i can understand,by looking at that, :cry:
Thx.

galmca
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

If you look at the part called Similar Threads at the bottom of this webpage you will find a lot of threads with examples.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

i have looked at them..but didn't quite get exactly what i wanted...ive written this code i hope any1 of u will be able to help me to generate the series upto a specific number of all those prime numbers which come in between by adding those extra lines in my code...so that i can learn..my program is working perfectly for testing whether a number is prime or not...now what i wanna do is to genetare the series of all prime numbers which come in between...hope any 1 of u can help!
thx

galmca
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

It is easy..

For each number from 2 to TheSpecificNumber
                    If number is a prime number
                                  Print number
                    Else
                                  Dont Print it
                    End if
          End For
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

oh my code is this...

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
   {
     if(n%i==0)
       {
         c=c+1;
        }
   }
if(c==2)
printf("number is prime");
else
printf("number is not prime");
getch();
}

It's working till now...up to here,.!
now all i ned is to generate the series of prime numbers that come in between a particular number.hope any1 of u can help me with this...by making those additional changes in my code!

galmca
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

an easy solution is:

startX, endX;
for(i=startX; i<=endX; i++) {
   for(j=2; j<i; j++) {
      p=i%j;
      if(p!=0) {
         // you know it is not prime...  break the loop
    } //now get the prime number when p==0 after checking all the values for j.
} // once u got the i is prime, print it.... n let the loop continue
orko
Junior Poster
164 posts since Apr 2006
Reputation Points: 46
Solved Threads: 11
 

This program should solve your problem.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
printf("number is prime\n");
else
printf("number is not prime\n");

n=n-1;
while(n >= 1)
{ 
c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
printf("This number is prime %d\n",n);
n=n-1;
}
}
fearfact_53
Newbie Poster
1 post since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Get rid of void main - it's not standard . While you're at it, you can trash the old crappy header conio.h , and start using code tags . Thanks.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Well, maybe this might help.

Its a mathematical fact that any prime number greater than 3 can be represented by (6a+1) or by (6a-1), where a is a parameter. Simply try to generate all such numbers and avoid repetition.

This should be faster than the method of checking divisibility.

abhinav.sharma
Newbie Poster
13 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

i want to know how to dispaly a series of number. Supose an user input a number(40). The progrme will generate and display like this way:

1-prime
2-not prime
3-prime
4-not prime
5-prime
6-prime
upto
40-prime

plz solve my proble. I will b grateful to u.

towhidd
Newbie Poster
2 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

i want to know how to dispaly a series of number. Supose an user input a number(40). The progrme will generate and display like this way:

1-prime 2-not prime 3-prime 4-not prime 5-prime 6-prime upto 40-prime

plz solve my proble. I will b grateful to u.


One, you've already asked this here: http://www.daniweb.com/forums/thread126749.html
Two, this thread is over a year old. Stick with the thread you already have. If you show some effort, you'll get better help than you have so far.
Three, I can't make any sense of the example you gave. 6 is not prime, 2 is prime.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

And one is not prime.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This is the code for the 2 Questions you have asked for:
1st Problem Code:

#include<stdio.h>
#include<conio.h>
main()
{
      int num,i;
      
      printf("\nEnter the number to be checked as prime:  ");
      scanf("%d",&num);
      
      for(i=2;i<=num-1;i++)
      {
         if(num%i==0)
         {
                  printf("\nIt's not a Prime Number.");
                  break;
         }
      }
      if(i==num)
      printf("\nIt's a Prime Number.");
      getch();
}

2nd Problem Code:

#include<stdio.h>
#include<conio.h>
main()
{
      int i,j,num;
      printf("\n\tEnter the number upto which we have to find the prime number: ");
      scanf("%d",&num);
      printf("\n");
      for(i=2;i<=num;i++)
      {
                         for(j=2;j<=i-1;j++)
                         {
                           if(i%j==0)
                           {
                               break;
                           }
                         }
                         if(i==j)
                        printf("\t%d",i);
      }
      getch();
}
pradeepchhetri
Newbie Poster
1 post since Aug 2009
Reputation Points: 9
Solved Threads: 0
 

This is the second time this old thread came back to life. Let this thread RIP!
Use code tags, don't use conio.h, there is no int infront of your main and don't post code without OP doing any work!

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 
public class primeNumber {
    public void isPrimeNumber(int UserInput){
        int j = 0;
        
        for(int i = 2 ; i<UserInput ; ++i ){
            if((i%2)==0){
                j = i/2;
            }else{
                j = (i+1)/2;
            }
            int c = 0;
            while(j>1){
                if((i%j)==0){
                  c = c + 1;  
                }
                j--;
            }
            if(c==0){ System.out.println(i + " is a prime number" );
            }
        }
}
    public static void main(String[] args){
        primeNumber pn = new primeNumber();
        pn.isPrimeNumber(100);
    }}

Not in C ..But hope this helps...Correct me if I have missed any thing in the logic..This gives the list of prime number within a range

Arindam_joy
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

I hope not because 1 is not a prime number* and 2 is.

You have already got code that checks a number to see if it is prime in your own post (although it code stand some optimisation). All you have to do is repeat that in a loop for every number you wish to check, better still put it in a function and then call that function from the loop.

* What is 1 not considered prime? - in short it would break too many other Maths theorems if it was.

Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
 
Not in C ..But hope this helps


Sorry, it does not help anyone. The post owner might be dead by now.

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

You only need to check the multiplicands until the square root of the input, the rest is just repeating...
As soon as you find a match, then searching more is not needed, so just "break" the loop.
Here's an example:

int is_it_prime(unsigned long  x)
{
        unsigned long  i=0; int iip=1;
        if (x>2)        for (i=2;i<=ceil(sqrt(x)); i++)
                if ((x % i)==0)
                {
                        iip=0;
                        break;
                }
        if (x==1) iip=0;
        if (x==2) iip=1;
        return iip;
}
cupofdenial
Newbie Poster
3 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

#include
#include
void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p

tayyab569
Newbie Poster
2 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I just made this for u. it display like what u need..
I modified from my earlier code so that it displays not just prime but also the non-prime.

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    int i,j,k;
    int* primeNum;
    int number;
    int n;
	char con[1]={};

	for(k=0;;k++)
	{
	    printf("\nUntil what number? ");
        scanf("%d",&number);
        primeNum = (int*) malloc(number*sizeof(int));

        for(n=0;n<number;n++)//simpan smua integer sampai n ke dalam array primeNum[n]
        {
            primeNum[n]=n+1;
        }

        for(i=1;i<=number;i++)
        {
            if(primeNum[i]!=0)
            {
                for(j=(i+1);j<=number;j++)
                {
                    if(primeNum[j]!=0)
                    {
                         if((primeNum[j]%primeNum[i])==0) //kalaw xdak baki set 0.
                            primeNum[j]=0;
                    }
                }
            }
        }

        for(i=1;i<number;i++)
        {
            if(primeNum[i]!=0)
            {
                printf("\n%3d - prime",primeNum[i]);
            }
            else if(primeNum[i]==0)
            {
                printf("\n%3d - not prime",primeNum[i]+i+1);
            }

        }

        if(number==1||number==0)
        {
            printf("\n%d is not a prime number",number);
        }

        printf("\n\nDo you want to continue? (y)es or (n)o\t");
        scanf("%s",con);

        if(con[0]=='n')
        {
            printf("\nThank you.\n");
            return 0;
            exit(0);
        }
	}

     return 0;

}
hakibam
Newbie Poster
9 posts since Aug 2010
Reputation Points: 32
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You