|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Baffled /w C++ code:
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||
|
*cough* Stock *cough*
|
Im still trying to get used to C++ code and stuff. I can read it and usually understand it, but apprently i cant write it for crap
![]() I thought that making a program that calculates primes wouuld be a decent 'getting used to it again' excercise but ive been left baffled. Heres what i have right now: Code:
#include <iostream.h>
#include <math.h>
int main(){
int n;
int i;
int is_prime;
is_prime = true;
cout << "In ascending order, the prime numbers are: \n\n\n";
for (n=3; n>0; n++)
{
for (i=2; i <= sqrt(double(n); i++)
{
if (n%i == 0)
is_prime = false;
}
if (is_prime == true)
cout << n << "\n";
}
return (0);
}
If it something big, or you have a big suggestion for it, then post here - but if it just a small mistake, just PM me and spare me some humiliation : lachen : Peace ppl
__________________
|
||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||
|
<3 TB303
![]() |
The innermost "if statement" that determines if a number is prime or not never halts its parent "for" loop. Also, is_prime is never reset.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n;
int i;
bool is_prime = true;
cout << "In ascending order, the prime numbers are: \n\n\n";
for (n=3; n>0; n++)
{
for (i=2; i <= sqrt(double(n); i++)
{
if (n%i == 0) {
is_prime = false;
break;
}
}
if (is_prime == true)
cout << n << "\n";
is_prime = true;
}
return (0);
}
__________________
3030303030303030303 <33333333
|
|||||||||||
|
|
|
|
#3 (permalink) | |||||||||
|
*cough* Stock *cough*
|
Quote:
Thanks alot - peace
__________________
|
|||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|