Program for prime number..

Is there anything wrong with this program???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a,b,c,d;
 cout<<"Enter the number";
 cin>>a;
 c=0;
 b=a;
 while(a>0 && b>0)
 {
  --b;
  c=(a%b); //(Update--Program is unexpectedly stopping here,dunno) why..
  while(c>0 && b>0)
  {
	--b;
	c=(a%b);
	if(c>0 && b==0)
	{
	 cout<<endl<<"Number is prime";
	}
	if(c==0 && b==0)
	{
	 cout<<"Number is not prime";
	}
  }
 }
 getch();
}
Last edited on
1
2
3
4
    while (a>0 && b>0)
    {
        --b;
        c=(a%b);

The while loop says b>0, so b==1 will be allowed. Then b is decremented to give zero. Next divide by zero = error.
Last edited on
I don't think my program is even getting to that point. It is getting stopped even before it enters that loop..
//(Update--Program is unexpectedly stopping here,dunno) why..

Maybe it is because of the divide by zero?
Last edited on
Now I changed my program to this, still no output!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d;
cout<<"Enter the number";
cin>>a;
c=0;
b=a;
while(a>0 && b>1)
{
--b;
c=(a%b);
cout<<"1  a="<<a<<"  b="<<b<<"  c="<<c<<endl;
while(c>0 && b>0)
{
--b;
c=(a%b);
cout<<"2  a="<<a<<"  b="<<b<<"  c="<<c<<endl;
if(c>0 && b==0)
{
cout<<endl<<"Number is prime";
goto stop;
}
if(c==0 || b==0)
{
cout<<"Number is not prime";
goto stop;
}
}
}
stop:
cout<<"0";
getch();
}


P.S.- Those cout statements were to trace the loops.. :-)
Last edited on
Actually, I don't understand why there are two while loops, one should be enough.

Typically in this sort of program, the decision that the number is not prime can be made as soon as any remainder of zero is found, that's the easy test. To find that it actually is prime usually the loop needs to progress right through to the end. If it wasn't found to be "not prime" then by default it must be prime.

When I say "progress right through to the end" I mean test for division by every number apart from 1 and the number itself.
Heyy!! Thanks for the concern... Debugged successfully!!!
In your cout statements you have endl in the wrong place on one and missing on the other.
1
2
cout<<endl<<"Number is prime";
cout<<"Number is not prime";

In the first case the endl will be flushed, but "Number is prime" will not be flushed.
In the second case, nothing will be flushed.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.



This one works!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream.h>
#include<conio.h>
void main()
{
 long unsigned int a,b,c;
 clrscr();
 c=0;
 cout<<"Enter the number"<<endl;
 cin>>a;
 b=2;
 while(b<=(a-1))
 {
  c=(a%b);
  ++b;
  if(c==0)
  {
	cout<<"It is not a prime number";
	goto stop;
  }
  if((b==(a-1))&&(c!=0))
  {
	cout<<a<<" is a prime number";
	goto stop;
  }
 }
 stop:
 getch();
}
Yes, that looks like it would work.

Notice this can be simplified, while (b<=(a-1))
is the same as while (b<a)

Or replace the while with for (b=2; b<a; b++) and remove lines 10 and 14.

You can also move lines 20 to 24 outside the loop. The end result looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    long unsigned int a,b;
    clrscr();
    cout<<"Enter the number"<<endl;
    cin>>a;

    for (b=2; b<a; b++)
    {
        if ((a%b) == 0)
        {
            cout<<"It is not a prime number";
            goto stop;
        }
    }

    cout<<a<<" is a prime number";

stop:
    getch();
}
Last edited on
Could you please extend this program to give prime number less than the given numbers? I've tried it, but could not debug it successfully... :-)
Last edited on
If the program needs to be extended, I'd recommend making a separate function called bool isPrime(unsigned long); or similar, which takes an integer parameter and returns a bool result to indicate whether the number is prime or not.

That way, you can build whatever additional code you like in main() and just call the function as required.
Topic archived. No new replies allowed.