Trying to figure out what I am doing wrong.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kapua51
    New Member
    • Sep 2006
    • 1

    Trying to figure out what I am doing wrong.

    Let me start off by saying that I am new to programming in C++. I know this may be basic but can someone help me in telling me what I am doing wrong with this code?

    #include "conio.h"
    #include <iostream>
    #include <cmath>
    #include <stdio.h>
    using namespace std;

    int main()
    {
    double n;
    double d;
    double y;
    double fmod;

    for (n = 3; n <=100; n++)
    {for (d = (n - 1); d>=2; d--);
    fmod(n,d);}
    cout<<n<<"is not prime";
    if (fmod=0)
    cout<<n<<"is not prime";
    else
    cout<<n<<"is prime"<<endl;

    return 0;
    }
  • f1engineer
    New Member
    • Sep 2006
    • 5

    #2
    #include "conio.h"
    #include <iostream>
    #include <cmath>
    #include <stdio.h>
    using namespace std;

    int main()
    {
    double n;
    double d;
    double y;
    double fmod;

    for (n = 3; n <=100; n++)
    {
    for (d = (n - 1); d >= 2; d--) //Here You havn't semicolon
    fmod(n,d);
    }
    cout<<n<<"is not prime";
    if (fmod=0)
    cout<<n<<"is not prime";
    else
    cout<<n<<"is prime"<<endl;

    return 0;
    }

    Comment

    • David Debonnaire
      New Member
      • Aug 2006
      • 1

      #3
      Originally posted by f1engineer
      #include "conio.h"
      #include <iostream>
      #include <cmath>
      #include <stdio.h>
      using namespace std;

      int main()
      {
      double n;
      double d;
      double y;
      double fmod;

      for (n = 3; n <=100; n++)
      {
      for (d = (n - 1); d >= 2; d--) //Here You havn't semicolon
      fmod(n,d);
      }
      cout<<n<<"is not prime";
      if (fmod=0) //**** Testing for equality : 'if (fmod == 0) ....'
      cout<<n<<"is not prime";
      else
      cout<<n<<"is prime"<<endl;

      return 0;
      }
      //**** Testing for equality : 'if (fmod == 0) ....'

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Generally speaking you should only use floating point variables (float and double) if you need to and you should not test the for equality to anything as floating point arithmatic is not acurate due to rounding errors.

        You are trying to calculate the primeness (prime or not prime) of integers (only integers can be prime or not prime), therefore you have used the wrong basic types to implement this program. The calculations should be intinsically integer based so so should your variables.

        This will also make the program faster and less prone to rounding errors.

        Comment

        Working...