Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Very Beginner Questions. Loops. 2nd post

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Very Beginner Questions. Loops. 2nd post

    here is the problem:

    Write a program that computes the following sum:

    sum = 1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + .... + 1.0/N
    N is an integer limit that the user enters.

    Enter N
    4

    Sum is: 2.08333333333
    here is what I have:

    import java.util.Scanner;										
     
    class nSum														
    {
    	public static void main ( String[] args )					
    	{
    		Scanner scan = new Scanner ( System.in );				
     
    		System.out.println("Enter an integer: ");				
    		int N = scan.nextInt();	
     
    		double sum 	 = 1.0;								
    		double count = 0.0;
     
    		while( N > 0 )											
    			{			
    				count = count + 1.0;							
    				sum = sum / count;											
    				N = (N - 1);							
     
    			}
     
    		System.out.println("The sum is: " + sum);				
     
    		System.out.println("");							
    	}	
    }

    what am I doing wrong here? as far as I can tell the problem is within the while loop but the math looks right. when it's run, it seems to be doing integer math instead of floating point math and I have no idea why. any help would be greatly appreciated! :/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Very Beginner Questions. Loops. 2nd post

    Can you explain what the problem is? Add some comments that shows what you want the program's output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    davidanthony129 (February 9th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Very Beginner Questions. Loops. 2nd post

    hey thanks for the response. basically the program should just ask for an integer and print out a sum. the sum will be determined by the user like the example in the quote the user chose 4 so the program went "1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 and the sum was 2.08333333333. if this would perform the math using floating point it would be correct but for some reason my code keeps using integer math dropping the remainders forcing the output to forever be 1.0 :/

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Very Beginner Questions. Loops. 2nd post

    Look at the statement where sum is assigned a value from an expression. Does that expression look like the terms in the formula you posted?

    Can you post the full console from when you execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Very Beginner Questions. Loops. 2nd post

    can't figure out how to copy from dos but it reads:

    C:\Users\David\Desktop\Source Files>java nSum
    Enter an integer:
    4
    The sum is: 0.04166666666664

    --- Update ---

    okay I got it figured out. my math was right but what was happening was the loop kept doing the same equation over and over so it wasn't adding the 1st iteration to the 2nd to the 3rd and so on and so forth to get the sum of all of them. but it works now. i can post the code if necessary. if not, consider it solved

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Very Beginner Questions. Loops. 2nd post

    Glad you figured it out.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    davidanthony129 (February 9th, 2014)

Similar Threads

  1. [SOLVED] Very Beginner Questions. Need Help Please :/
    By davidanthony129 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 9th, 2014, 02:21 AM
  2. Few Beginner Questions
    By New Coder in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 13th, 2013, 07:14 AM
  3. Some Beginner questions
    By itzraininz in forum Object Oriented Programming
    Replies: 10
    Last Post: February 25th, 2013, 10:23 AM
  4. Few questions from beginner
    By Mefimess in forum Java Theory & Questions
    Replies: 2
    Last Post: November 6th, 2012, 11:03 AM
  5. Beginner: some questions
    By norenberg in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2012, 05:57 PM