There is a famous story about a primary school teacher who w

There is a famous story about a primary school teacher who wanted to occupy his students\' time by making the children compute the sum of 1 + 2 + 3 + ... + 100 by hand. As the story goes, the teacher was astounded when one of the children immediately produced the correct answer: 5050. The student, a child prodigy, was Carl Gauss, who grew up to be one of the most famous mathematicians of the eighteenth century. Repeat Gauss\'s remarkable calculation by writing a loop that will compute and print the above sum. After you have the program working, rewrite it so you can compute 1 + 2 + ... + n where n is any positive integer. Complete the code in Net Beans, do not answer on this worksheet. The TA will check that code.

Solution

// GaussSum.java
public class GaussSum
{

    public static int ngauss(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            sum = sum + i;
            System.out.println(\"N = \" + i + \"\\tSum: \" + sum);
        }

        return sum;
    }

    public static void main(String[] args)
    {
        ngauss(100);
    }
}

/*
output:

N = 1   Sum: 1
N = 2   Sum: 3
N = 3   Sum: 6
N = 4   Sum: 10
N = 5   Sum: 15
N = 6   Sum: 21
N = 7   Sum: 28
N = 8   Sum: 36
N = 9   Sum: 45
N = 10 Sum: 55
N = 11 Sum: 66
N = 12 Sum: 78
N = 13 Sum: 91
N = 14 Sum: 105
N = 15 Sum: 120
N = 16 Sum: 136
N = 17 Sum: 153
N = 18 Sum: 171
N = 19 Sum: 190
N = 20 Sum: 210
N = 21 Sum: 231
N = 22 Sum: 253
N = 23 Sum: 276
N = 24 Sum: 300
N = 25 Sum: 325
N = 26 Sum: 351
N = 27 Sum: 378
N = 28 Sum: 406
N = 29 Sum: 435
N = 30 Sum: 465
N = 31 Sum: 496
N = 32 Sum: 528
N = 33 Sum: 561
N = 34 Sum: 595
N = 35 Sum: 630
N = 36 Sum: 666
N = 37 Sum: 703
N = 38 Sum: 741
N = 39 Sum: 780
N = 40 Sum: 820
N = 41 Sum: 861
N = 42 Sum: 903
N = 43 Sum: 946
N = 44 Sum: 990
N = 45 Sum: 1035
N = 46 Sum: 1081
N = 47 Sum: 1128
N = 48 Sum: 1176
N = 49 Sum: 1225
N = 50 Sum: 1275
N = 51 Sum: 1326
N = 52 Sum: 1378
N = 53 Sum: 1431
N = 54 Sum: 1485
N = 55 Sum: 1540
N = 56 Sum: 1596
N = 57 Sum: 1653
N = 58 Sum: 1711
N = 59 Sum: 1770
N = 60 Sum: 1830
N = 61 Sum: 1891
N = 62 Sum: 1953
N = 63 Sum: 2016
N = 64 Sum: 2080
N = 65 Sum: 2145
N = 66 Sum: 2211
N = 67 Sum: 2278
N = 68 Sum: 2346
N = 69 Sum: 2415
N = 70 Sum: 2485
N = 71 Sum: 2556
N = 72 Sum: 2628
N = 73 Sum: 2701
N = 74 Sum: 2775
N = 75 Sum: 2850
N = 76 Sum: 2926
N = 77 Sum: 3003
N = 78 Sum: 3081
N = 79 Sum: 3160
N = 80 Sum: 3240
N = 81 Sum: 3321
N = 82 Sum: 3403
N = 83 Sum: 3486
N = 84 Sum: 3570
N = 85 Sum: 3655
N = 86 Sum: 3741
N = 87 Sum: 3828
N = 88 Sum: 3916
N = 89 Sum: 4005
N = 90 Sum: 4095
N = 91 Sum: 4186
N = 92 Sum: 4278
N = 93 Sum: 4371
N = 94 Sum: 4465
N = 95 Sum: 4560
N = 96 Sum: 4656
N = 97 Sum: 4753
N = 98 Sum: 4851
N = 99 Sum: 4950
N = 100 Sum: 5050


*/

 There is a famous story about a primary school teacher who wanted to occupy his students\' time by making the children compute the sum of 1 + 2 + 3 + ... + 100
 There is a famous story about a primary school teacher who wanted to occupy his students\' time by making the children compute the sum of 1 + 2 + 3 + ... + 100
 There is a famous story about a primary school teacher who wanted to occupy his students\' time by making the children compute the sum of 1 + 2 + 3 + ... + 100

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site