Results 1 to 3 of 3

Thread: Prime calculates missing in C++

  1. #1
    Join Date
    Oct 2008
    Posts
    30

    Prime calculates missing in C++

    I've made a piece of code that just will not work, what's wrong! It can execute fine, but calculates everything as being non prime!
    Thanks in advance!

    Code:
    # include <iostream> 
    # include <cstdlib> 
    # include <cmath> 
    
    using namespace std; 
    
    int main () 
    ( 
    int t, c, residue; 
    char in; 
    Cout << "Enter the number you want tested for primtalisme, then enter: \ n \ n"; 
    cin>> t; 
    c = t; 
    
    if (t == 0, t == 1, t == 2) 
    ( 
    Cout << "yes"; 
    Cout <<c; 
    Cout << "suffering from primtalisme012 \ n \ n"; 
    return 0; 
    ) 
    
    else 
    ( 
    for (i = 2; i <= (c-1); i + +) 
    ( 
    if (c% in == 0) 
    ( 
    residual = 0; 
    break; 
    ) 
    else 
    ( 
    residual = 1; 
    ) 
    ) 
    
    if (remainder == 0) 
    ( 
    Cout << "yes"; 
    Cout <<c; 
    Cout << "suffering from primtalisme other \ n \ n"; 
    ) 
    else 
    ( 
    Cout << "no"; 
    Cout <<c; 
    Cout << "suffer not the primtalisme \ n \ n"; 
    
    ) 
    ) 
    return 0; 
    )
    PS. I know there are other methods, but why does this not?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Prime calculates missing in C++

    You have written your first if false statements.

    will be
    if (i == 0 | | i == 1 | | i == 2)

    Note two pipes | | instead of,

    And it never come into your first loop because of the type that char when you compare with an int should also be of the type.

    And if your last statement with the rest seem to have reversed incorrectly.

    If residual == 0, then you run into a second number to go up in the input number, but you wonder about the opposite.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Prime calculates missing in C++

    If we try with 3 (3 is a prime number)

    Code:
    for(i=2; i<=(c-1); i++) 
    { 
    if(c%i==0)
    To test at:

    IF 3% 2 EQUAL TO 0
    Prime =

    Which of course, return false. And it is wrong.

Similar Threads

  1. Replies: 3
    Last Post: 28-02-2012, 07:47 PM
  2. Replies: 7
    Last Post: 24-01-2012, 06:49 AM
  3. Problem with C# program which calculates the prime number
    By $Bird$ in forum Software Development
    Replies: 4
    Last Post: 29-11-2010, 12:54 PM
  4. Prime number
    By mad4jack in forum Software Development
    Replies: 3
    Last Post: 03-10-2008, 09:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,371,463.88803 seconds with 17 queries