I have many errors in this program will someone help I also have to put another for loop the the isRightprime var.

// imports
#include <iostream>
int isRightPrime(int);
int isPrime(int);

// begin main
int main()
{
 // declare variables 
	int rightPrime;
	int Prime;
	int x;
	int y;

using namespace std;  //using namespace system;


int isPrime(long num) // Returns 1 (true) if its prime, or 0 (false) if not
{
if (num < 2) // 1 is not a prime number
return 0;

// if it is > 2 and an even number, then it definitely is not a prime
if (num > 2 && (num % 2) == 0)
return 0;

//considering the fact all can be divided by 1 and itself,
//start checking if there is any other divisor, if found one then no need to continue, it is not a prime
for(int i = 2; i < num; i++ )
{
cout << " divisor: " << i << endl;
if ( (num % i) == 0) // if it is divisible by i
{
// a divisor other than 1 and the number itself
return 0; // no need for further checking
}
}


return 1; // if all hurdles/checks are crossed, heyyyy, its a prime
}

int main()
{
int num;
do {
cout << " enter a number (0 to stop) " << endl;
cin >> num;
if (num) {
if (isPrime(num))
cout << num << " is a prime numebr " << endl;
else
cout << num << " is NOT a prime numebr " << endl;
}
} while (num > 0);

return 0;
}
}

Edited 6 Years Ago by __avd: Added [code] tags.

I just helped someone via PM (I know, not supposed to do that ;)). The same post applies here, so I'll recreate it verbatim.


Here's the way it works.

// #include statements
using namespace std;

// function declarations (include semicolons)

int main()
{
    // function calls
    return 0;
}

// functions (copy what's before main, delete semicolons)

Here's an example

#include <iostream>
using namespace std;

int add(int num1, int num2);
int subtract(int num1, int num2);


int main()
{
  int num1 = 5;
  int num2 = 2;
  int num3 = 8;
  int num4 = 4;

  int num5 = add(num1, num2);
  int num6 = subtract(num3, num4);

  cout << num1 << " + " << num2 << " = " << num5 << endl;
  cout << num3 << " - " << num4 << " = " << num6 << endl;
  return 0;
}


int add(int num1, int num2)
{
    int sum = num1 + num2;
    return sum;
}


int subtract(int num1, int num2)
{
    int difference = num1 - num2;
    return difference;
}

Use the above program as a guide for your code. You have functions within functions, which is a no-no, and you have two main functions, which makes no sense at all. You need to have one and only one main function.

This article has been dead for over six months. Start a new discussion instead.