Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

How to solve this in java eclipse square of all numbers and sum

Given the sequence of numbers: [1,2,3,4,5,6,7,8,9,10], design a program that will square each number in the sequence and return the sum of all the squares added together.

i need help to do this in java any idea with outputs and comments

i currently have a half working solution

import java.util.Scanner;
public class interview2 {
    public static void main(String [] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int num = sc.nextInt();
        System.out.println("Your squared number is: " + square(num));
    }
    public static int square(int num) 
    {
        return num * num;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public static void main(String[] args) {
        int sum = 0;
        for(int i = 1; i < 11; i++)
            sum += square(i); /*you can use the power
            function e.g pow(i,2)*/
        System.out.println(sum);
    }

public static int square(int num) { return num * num; } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...