I only just recently started learning C++ a few months ago. So i'm not sure how to go about this. In the function int prime(num)the num value should be returned to int(main), I've tried multiple times to fix it, but simply could not figure it out. #include "stdafx.h"
#include <iostream>
using namespace std;
bool IsPrime(int num)
{
if (num != 2) {
if (num < 2 || num % 2 == 0) {
return false;
}
for (int i = 3; (i*i) <= num; i += 2) {
if (num % i == 0) {
return false;
}
}
}
return true;
}
int Prime(int n)
{
int checkprime;
int nthplace=0;
int num=0;
n;
IsPrime(num);
if (true)
for (int num = 2; num <= 10001; num++)
{
checkprime = 0;
for (int i = 2; i <= num / 2; i++)
{
if (num%i == 0)
{
checkprime = 1;
break;
}
}
if (checkprime == 0)
nthplace++;
if (nthplace == n)
cout << n << " Prime num is: " << num << endl;
}
return num++;
}
int main()
{
char yon;
int n;
int num=0;
do {
cout << "Which nth Placed Prime num Are You Looking For? ";
cin >> n;
Prime(n);
num; // num isn't being returned
cout << n << " Prime num is: " << num << endl;
cout << "Would You Like To Find Another Prime num? (Y/N) ";
cin >> yon;
yon = yon | 0x20;
while (yon != 'y' && yon != 'n') {
cout << "Invalid Response, please enter \"Y\" or \"N\": ";
cin >> yon;
yon = yon | 0x20;
}
} while (yon == 'y');
cout << "\nThank you for using this awesome program\n" << endl;
return 0;
}
I realize there is a redundancy in the functions, but I'm more focused on the problem of the value not returning.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
bool IsPrime(int num)
{
if (num != 2) {
if (num < 2 || num % 2 == 0) {
returnfalse;
}
for (int i = 3; ( i*i ) <= num; i += 2) {
if (num % i == 0) {
returnfalse;
}
}
}
returntrue;
}
int Prime(int n)
{
int checkprime;
int nthplace = 0;
int num = 0;
n;
IsPrime(num);
if (true)
for (int num = 2; num <= 10001; num++)
{
checkprime = 0;
for (int i = 2; i <= num / 2; i++)
{
if (num%i == 0)
{
checkprime = 1;
break;
}
}
if (checkprime == 0)
nthplace++;
if (nthplace == n)
cout << n << " Prime num is: " << num << endl;
}
return num++;
}
int main()
{
char yon;
int n;
int num = 0;
do {
cout << "Which nth Placed Prime num Are You Looking For? ";
cin >> n;
Prime(n);
num; // num isn't being returned
cout << n << " Prime num is: " << num << endl;
cout << "Would You Like To Find Another Prime num? (Y/N) ";
cin >> yon;
yon = yon | 0x20;
while (yon != 'y' && yon != 'n') {
cout << "Invalid Response, please enter \"Y\" or \"N\": ";
cin >> yon;
yon = yon | 0x20;
}
} while (yon == 'y');
cout << "\nThank you for using this awesome program\n" << endl;
return 0;
}