VS Code (wrong output) - Online IDE (right output)
The VS Code (wrong output) - Firstly, I made the code in different IDE but then I want to try the VS Code editor just to test the water but for some reason the code ran perfectly in the different IDE and Online IDE but got wrong output in VS Code - sorry for my bad english I'm still a first year student.
[Red is VS Code & Yellow is Online IDE - Picture]
#include <iostream>
using namespace std;
int main (){
int num;
bool ans;
int prime_num(int m);
cout << "Enter a number: ";
cin >> num;
ans = prime_num(num);
if (ans == true)
cout << num << " is a prime number" << endl;
else
cout << num << " isn't a prime number" << endl ;
system("pause");
return 0;
}
int prime_num(int m){
bool result;
if (m == 0 || m == 1){
result = false;
}
for (int i = 2; i <= m/2; ++i){
if (m % i == 0){
result = false;
break;
}
else
result = true;
}
return result;
}
VS Code (right output) - Online IDE (wrong output)
*\So, I've tried changing the bool value but then it got inverted for the answer for both environment.*
[Red is VS Code & Yellow is Online IDE - Picture]
#include <iostream>
using namespace std;
int main (){
int num;
bool ans;
int prime_num(int m);
cout << "Enter a number: ";
cin >> num;
ans = prime_num(num);
if (ans == false)
cout << num << " is a prime number" << endl;
else
cout << num << " isn't a prime number" << endl;
system("pause");
return 0;
}
int prime_num(int m){
bool result;
if (m == 0 || m == 1){
result = true;
}
for (int i = 2; i <= m/2; ++i){
if (m % i == 0){
result = true;
break;
}
else
result = false;
}
return result;
}