1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PLEASE READ! Java, print sum of squares

Discussion in 'Software Development' started by ElementOfMagic, Jan 15, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Hello. :)

    I was wondering whether anybody could possibly tell me why, the below Java program doesn't output anything to the screen when I run it. :confused:
    It's supposed to print the sum of square numbers between 1 and 10. It compiles without a problem, but for some reason when I try to execute it, it just jumps straight to the end of the program and I have no idea why?

    Code:
    import java.util.*;
    
    public class SquareProgram {
    	public static void main(String args[])
    	{
    	
    		int n = 10;
    		int square = n*n;
    		
    		square(0);
    		
    		sumOfSquares(0);		
    	
    	}
    
    	public static int square(int n)
    
    	{
    		int square = n*n;
    		
    		return n*n;
    	
    	}
    	
    	public static int sumOfSquares(int n)
    		
    	{
    		int sum = 0;
    		int square = n*n;
    		int i;
    		for (i = 1; i <= n; i++)
    	{
    		sum = sum + square;
    		
    		System.out.println("The sum of squares between 1 and "+n+" is "+sum+"");
    	}	
    		return sum;
    						
    	}
    }
    
    Any help/assistance would be greatly appreciated. :)
     
  2. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    For starters, you need the square line inside the for loop.
    Secondly, you want to be squaring i, not n.
    i.e. you want 1 squared plus 2 squared plus 3 squared.
    not 3 squared plus 3 squared plus 3 squared.

    Also, what do you mean by it jumps to the end? Does it exit before you can see anything?
     
  3. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Thanks, but that doesn't make a difference.
    And yes, that's what jumps to the end means.
     
  4. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    Try putting in an extra command at the end (input commands work well) to force it to delay.
     
  5. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    First of all your program isn't printing anything because you are calling the function sumOfSquares with an argument of 0, which will mean the function never goes into the for loop - where the system.out.println is.

    Also, as Ent said, there is a problem with your code that calculates the sum
    Try this:
    Code:
    public static int sumOfSquares(int n) {
    	int sum = 0;
    	int square;
    	int i;
    	for (i = 1; i <= n; i++) {
    		square = i*i;
    		sum = sum + square;
    	}
    	System.out.println("The sum of squares between 1 and "+n+" is "+sum+"");
    	return sum;					
    }
    
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1085366

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice