Results 1 to 6 of 6
  1. #1
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Question what is wrong with this code

    Hello i have an equality that take the following form:
    y = 1-1/2+1/3-......................+1/x
    x is avariable entered by the user
    I try the following Code but it dosn't Work
    Java Code:
    int i,x;
        double y=1.0 ;
        System.out.print("Please Enter A Number  ");   
        x = console.nextInt();
        for( i = 2;i <= x;i++)
        {      
         if((i % 2) == 0)
          y = y + (1 / i);
          else
          y = y - (1 / i);
          i++;
         }
          System.out.print("The Result OF Y is  "+ y );     
        }

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default


    Please give some more details.

    +1 and -1 are fix.

    in this case x = 6?

    y = 1-1/2+1/3-1/4+1/5-1/6+1

    If i am wrong then please correct me.


    sanjeev

  3. #3
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    First of all i had to add the code to get the input from the user, not sure if you have this back somewhere in your program, but for me "console" was undefined. Just a few "standard" things, define i in your for loop and dont bother incrementing it in your loop. The for loop does that for you(no pun intended). Also, throw brackets into your if statement. It saves lines leaving them out, but if you ever have to add something in the future you'll forget them and have all kinds of errors, better to just get in the practice of putting them in but that's your preference. The root problem you are having is that your "i" is an integer and is doing integer division. Which 1/x where x>1 is going to end up as a decimal which in integer division will be a zero. We counteract this by thrown a "(double)" in front of it to cast it as a double and force it to do float division. The code below worked for me and should work for you.

    Side thought: you mentioned that your equation is y = 1-1/2+1/3-......................+1/x. If that is so then i think your if statement may be backwards. Make it a "!=" to have the even fractions subtracted instead of the odd ones. Right now it is vise verse.
    Java Code:
        int x;
        double y = 1.0;
        Scanner console = new Scanner(System.in);
    
        System.out.print("Please Enter A Number  ");
        x = console.nextInt();
        for (int i = 2; i <= x; i++) {
          if ((i % 2) == 0) {
            y = y + (1 / (double)i);
          } else {
            y = y - (1 / (double)i);
          }
        }
        System.out.print("The Result Of Y is  " + y);
    Last edited by Chris.Brown.SPE; 04-15-2008 at 05:29 PM.

  4. #4
    saint_jorjo is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default

    public static void main(String[] args) {

    double result = 1;
    int n = 4;
    double m=1;
    boolean verno=true;
    for(int i=2; i<=n; i++){
    if(verno==true){
    result = result-m/i;
    System.out.println(result+"result0");
    verno=false;
    }else{result = result+m/i;
    System.out.println(result+"result1");
    verno=true;
    }
    }
    System.out.println(result);
    }

  5. #5
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Is that supposed to do something different jorjo?

  6. #6
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

Similar Threads

  1. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:16 PM
  2. Image problems, what's wrong here.
    By Bluefox815 in forum Java Applets
    Replies: 1
    Last Post: 03-07-2008, 02:45 AM
  3. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM
  4. Is there somethign wrong with this code?
    By Soda in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 04:46 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •