help, trying to find prime number
ok, i just started working with c++, and im doing a basic project trying to find all the prime numbers under 100.
here is what i have right now:
Code:
/*variables needed num1, num2, index.
if num1 % num2 = 0 index++, if index = 0 print num1. */
#include <iostream>
using namespace std;
int main ()
{
int num1, num2;// the 2 numbers we divide to find the remainder
int remainder, index=0; // remainder determines if the number is prime or not
for(num1=1; num1 <= 100; num1++) // we take each number under 100
{
index = 0;// we reset index back to 0
for(num2=1; num2< num1; num2++) //and mod it be each number lower than itself
{
remainder = num1%num2; // find out the remainder of it
if(remainder != 0) // and if there is none
{
index++; // we increment index once.
}
}
if (index != 0) // if we didnt increment index during that operation
{
cout << num1 << " is a prime number\n"; // it means that num1 is a prime number
}
}
cin.get();
return 0;
}
issue is, i get 77-100 as prime numbers, nothing before 77 at all, and not only the prime numbers between 77 and 100, but rather 77, 78, 79, 80, etc.
i know i probably made a logic error somewhere along the line.
can someone please look through the code tell me where im wrong. because ive been staring at it for an hour w/ no luck so far.