| | |
Prime Number
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int j; int t; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2 ; i < num; i++ ) if ( ( i == 2 ) or ( i % 2 != 0 ) ) { p = i; } { for ( int j = 2; j > p; j++ ) if ( p % j != 0 ) t = p; cout << t << endl; } return 0; }
I'm using this program to give me the prime numbers that are less than the number entered, but it's not working?
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
The spacing in your code makes it very hard to read:
You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int j; int t; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2 ; i < num; i++ ) if ( ( i == 2 ) or ( i % 2 != 0 ) ) { p = i; } { for ( int j = 2; j > p; j++ ) if ( p % j != 0 ) t = p; cout << t << endl; } return 0; }
You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
The spacing in your code makes it very hard to read:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int j; int t; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2 ; i < num; i++ ) if ( ( i == 2 ) or ( i % 2 != 0 ) ) { p = i; } { for ( int j = 2; j > p; j++ ) if ( p % j != 0 ) t = p; cout << t << endl; } return 0; }
You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.
wrap it in braces
{
blah
}
Also, in your if statement, I think you're missing an else?
{
blah
}
Also, in your if statement, I think you're missing an else?
if ( ( i == 2 ) or ( i % 2 != 0 ) )
{
p = i;
}
else // ????
{
for ( int j = 2; j > p; j++ )
{
if ( p % j != 0 )
t = p;
}
cout << t << endl;
} Last edited by chococrack; Oct 21st, 2008 at 1:29 am.
I would love to change the world, but they won't give me the source code
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:
C++ Syntax (Toggle Plain Text)
for (/* for-loop code */) { // code to be repeated. Inside brackets. } // code that is not repeated. Outside the brackets.
Last edited by VernonDozier; Oct 21st, 2008 at 1:24 am.
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:
C++ Syntax (Toggle Plain Text)
for (/* for-loop code */) { // code to be repeated. Inside brackets. } // code that is not repeated. Outside the brackets.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
Also in addition to the braces, your logic is messed up. See the comments in your code.
Here is some pseudocode to help
C++ Syntax (Toggle Plain Text)
for ( int i = 2 ; i < num; i++ ) /* you can add a special case for 2, you only need to check for the factors of odd numbers which are greater than 2 */ if ( ( i == 2 ) or ( i % 2 != 0 ) ) { /* you don't really need this placeholder. you can just use "i" */ p = i; } { /* this doesn't make sense... you should be checking from 2 upto the odd number. j will always be less than p */ for ( int j = 2; j > p; j++ ) /* this doesn't help either, if your odd number is not divisible by the last number then it will assume it to be prime ? */ if ( p % j != 0 ) t = p; cout << t << endl; }
Here is some pseudocode to help
if number is odd
for i = 2 to number
if number divisible by i
haveFactors = true;
break;
end if
end for
if haveFactors is false, we have a prime number.
end if•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int q; int j; int sum = 0; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2; i < num; i++ ) { if ( ( i == 2 ) or ( i % 2 != 0 ) ) p = i; for ( int j = 1; j <= p; j++ ) { if ( p % j == 0 ) { sum += j; } } { if ( sum == p + 1 ) q = p; } cout << q << endl; } return 0; }
i'm trying to get the ODD numbers but its gives even numbers
![]() |
Similar Threads
- prime number question (C)
- C++ prime number program help (C++)
- Average of prime number between 1 & 100 (C#)
- Print Max Prime Number??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Can somebody please help me write this code
- Next Thread: Pointer to Member Function
Views: 985 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






