0

Please can anyone help in debugging this code to show if 13 is a prime number

#include <iostream> #include <cmath> using namespace std; int main () { double a, b, num1, num2; a = 13; b = 1; num1 = a/a; num2 = a/b; If (num1 ==b && num2 ==a) { cout << "The value 13 is a prime number"; } else {cout << "The value 13 is not a prime number"; } return0; }

13th Apr 2020, 5:20 AM
Fagbire Oluwatosin Deborah
4 odpowiedzi
+ 2
your condition for checking prime number is kinda complex and wrong, I inputted a = 14 and its saying 14 is prime number. Prime number condition: A number divisible by only 1 and itself 2 is the only even prime number. but according to your code. a = 14 b = 1 num1 = a/a = 14/14 = 1 num2 = a/b = 14/1 = 14 if(num1==b && num2==a) is always true for non prime number.
13th Apr 2020, 5:36 AM
minirkk
minirkk - avatar
+ 1
There are some typos: 'If' should be 'if' 'return0' should be 'return 0' ... And then, the test does not check if it is a prime number as the condition will always be true for all values of a...
13th Apr 2020, 5:42 AM
ifl
ifl - avatar
+ 1
#include <iostream> using namespace std; int main() { int x,d,c; c=0; cin>>x; for(d=1;d<=x;d++){ if(x%d==0){ c++; } d++; } if(c==2){ cout<<"number is prime "; }else{ cout<<"number is not prime "; } return 0; }
18th May 2020, 4:40 PM
kojakiii
kojakiii - avatar
0
Can anyone help with a c++ program to show if 13 is a prime number
17th Apr 2020, 7:37 AM
Fagbire Oluwatosin Deborah