+ 3
I have made a program on prime number but when i run the program and enter a number it says it's not a prime number.what's wrong
37 Respostas
+ 4
Mirielle🐶 Actually 1 is a prime number by definition, so only 0 should be excluded.
Disha Dey The best solution is to create a function which returns whether or not an integer is a prime number:
#include<iostream>
bool is_prime(int n) {
if (n == 0) {
return false;
} else if (n < 0)
{
n *= -1;
}
for (int i = 2; i < (n/2)+1; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main(int argc, const char *argv[]) {
std::cout << is_prime(31) << std::endl;
return 0;
}
https://code.sololearn.com/cMufni2rO4mT/?ref=app
https://code.sololearn.com/c8VMWT7STHK7/?ref=app
https://code.sololearn.com/wg6lZE1tg98c/?ref=app
If you want output instead of boolean return value, just add another function:
void print_prime(const int n) {
std::cout << n << " is ";
if (!is_prime(n)) {
std::cout << "not ";
}
std::cout << "a prime number!" << std::endl;
}
and call this from your int main() function instead.
+ 2
Try this code for beginners
please upvote if it helped *_*
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int n, f, p=0;
cout<<"enter any no to check if it is prime";
cin>>n;
for(f=1;f<=n;f++)
{
if (n%f==0)
p++;
}
cout<<\n<<p;
if(p==2)
cout<<"it is a prime number";
else
cout<<"it is not a prime number";
getch();
}
+ 1
Disha Dey It's working fine with logic but errors in class declarations and naming. ...
1) Remove 3rd line int main() put class prime{
2)void display () missing {
3) comment clrscr()
4) make main() return type int, not void
+ 1
#include <iostream>
using namespace std;
class prime {
public: int num, i, j = 0;
public:
void display(){
cout << "Enter number: ";
cin >> num;
for (i = 2; i < num; i++)
{
if ((num % i) == 0)
{
j++;
}
}
if (j == 0 && num !=1)
cout <<" is a prime number";
else
cout <<" is not a prime number";
}
};
int main()
{
//clrscr();
prime p;
p.display();
}
0
But the answer is coming it is not a prime number even if i write a prime number or not
0
Disha Dey run this exactly same..
#include <iostream>
using namespace std;
class prime{
int num, i, j = 0;
public:
void display(){
cout << "Enter number: ";
cin >> num;
for (i = 1; i <= num; i++)
if ((num % i) == 0)
j++;
if (j == 2)
cout <<num<<" is a prime number";
else
cout <<num<<" is not a prime number";
}};
int main()
{
// clrscr();
prime p;
p.display();
return 0;
}
0
In 4th line when i write j=0 a error comes " cannot initialize a class member here"
0
Variable j you declared as private, you can use it in any where in that class but not outside.
And in your program, am not getting any warnings, as I pasted same code in above post. Run it once...
In case, if you assign outside class, then error comes...
0
#include <iostream>
using namespace std;
int main() {
int num, i, j;
public:
void display()
cout << "Enter number: ";
cin >> num;
for (i = 1; i <= num; i++)
{
if ((num % i) == 0)
{
j++;
}
}
if (j == 0)
cout <<" is a prime number";
else
cout <<" is not a prime number";
}
};
void main()
{
clrscr();
prime p;
p.display();
return 0;
}
If i write like this and run the program and i enter a number for example 17 it says it's not a prime number
0
You are checking as j==0 instead of j==2.
0
Then what should i write.I'm confused little bit.
0
Is this above program, running on your compiler correctly ?
Prime 17
1%17=0 =>j=1
17%17=0 =>j=2. //for any prime number only 2 factors exists. So condition must be
Then
if(j==2)
cout<<"prime"; //else no... Your condition in 1st posted code is correct. Keep the same..
0
But answer is not coming
0
Line 12 to 20 copy and paste in your compiler. It will be same any compiler..
https://code.sololearn.com/cbDpNuG6v8Vf/?ref=app
0
write j=0 a error comes " cannot initialize a class member here" and if i remove and run the program then i am not getting prime number
0
I'm not able to understand how you getting error? While the Program I posted above giving me correct output without errors..
What are the changes with above one comparing with your which you are running..?
Edit:
Are your writing in if like j==2 or j=2?
0
Please write more readable
0
void main()
{
int a;
clrscr();
printf(“ Enter a number “);
scanf(“%d”,&a);
if(a==1)
printf(“ %d is not prime “, a);
else if(a==2 || a==3 || a==5 || a==7)
printf(“ %d is prime “, a);
else
{
if(a%2==0 || a%3==0 || a%5==0 || a%7==0)
printf(“ %d is composite “, a);
else
printf(“ %d is prime “, a);
}
getch();
}
0
It is a c program