Sum Programme adding square of numbers 计算程序,将数字的平方相加

huangapple go评论54阅读模式
英文:

Sum Programme adding square of numbers

问题

以下是翻译好的代码部分:

public class Summing {
    public static void main(String args[]) {
        int sum = 0;
        int n = 1;
        int square;

        for (int i = 1; i <= 100; i++) {
            if (i % 13 == 0) {
                sum = sum + i;
            }
        }
        System.out.print("所有可被13整除的数字之和:" + sum);

        while (n <= 100) {
            square = n * n;
            sum = sum + square;
            n++;
        }
        System.out.print("所有数字平方之和:" + sum);
    }
}
英文:

college assignment here and need to do the following:

  1. The sum of all numbers divisible by 13 between 1 and 100 inclusive
  2. The sum of all numbers squared between 1 and 100 inclusive

The output i get when adding the squared numbers is 338714 but correct answer is 338350 - can someone advise where exactly i am going wrong

public class Summing {
public static void main (String args []){
int sum = 0;
int n = 1;
int square;
for (int i = 0; i &lt; 100; i++){
if (i%13==0)
{
sum = sum + i;
}
}
System.out.print(&quot;The sum of all all the numbers is divisible by 13&quot; = sum);

while (n&lt;=100){
square (n*n);
sum = sum + square;
n++;
}
System.out.print(&quot;The sum of all the numbers squares is &quot; + sum );
}
}

答案1

得分: 0

338714 - 338350 = 314 这恰好是你的第一个任务的总和 Sum Programme adding square of numbers
计算程序,将数字的平方相加
你需要重置"sum"变量,因为当进入第二个循环时,它的值是314,这是差值。

英文:

338714 - 338350 = 314 which is exactly the sum of your first mission Sum Programme adding square of numbers
计算程序,将数字的平方相加
You hav to reset "sum" variable, because when getting into the second loop its value is 314 which is the difference.

huangapple
  • 本文由 发表于 2020年9月10日 06:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63820405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定