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.

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

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

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

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!

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

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;
}
}

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.

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.

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.

And one is not prime.

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();
}
commented: Bad code, old thread -1
commented: Your code brings tears to my eyes and not in a good way. +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!

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

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.

Not in C ..But hope this helps

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

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;
}

#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p<i;p=p+1)
{
if(i%p==0)
break;
else
continue;
}
if(i==p)
cout<<" Prime:"<<p;
}
getch();
}

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;

}

this is an old post? my mistake. Just ignore my reply...

commented: Grats for noticing the date (belated or not) and apologising. :) +22
#include<stdio.h>
 #include<conio.h>

    void main()

      {
  int n,i;


      clrscr();
      for(n=3;n<=40;n++)
  {
      for(i=2;i<n;i++)
    {
      if(n%i==0){
      printf("%d is not a prime no \n",n);
      break;
      }

      else{
      printf("%d number is prime \n",n);
      break;
      }
    }
  }

       getche();


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.