1   /**
2    * Ohjelmalla lasketaan välin summa
3    * @author Vesa Lappalainen
4    * @version 1.0, 16.02.2003
5    */
6   public class Valinsum {
7   
8     /**
9      * Lasketaan yhteen luvut 1..ylaraja
10     * @param ylaraja summan yläraja
11     * @return summa
12     */
13    public static int valin_summa(int ylaraja) {
14      int i,summa=0;
15      for (i=1; i<=ylaraja; i++)
16        summa += i;
17      return summa;
18    }
19  
20    public static int valin_summa_2(int ylaraja) {
21      int i,summa;
22      for (summa=0, i=1; i<=ylaraja; i++)
23        summa += i;
24      return summa;
25    }
26  
27    public static int valin_summa_3(int i) {
28      int s;
29      for (s=0; i >= 0; s += i--);
30      return s;
31    }
32  
33    public static void main(String[] args)  {
34      int summa = valin_summa(100);
35      System.out.println("Summa = " + summa);
36      summa = valin_summa_2(100);
37      System.out.println("Summa = " + summa);
38      summa = valin_summa_3(100);
39      System.out.println("Summa = " + summa);
40    }
41  }
42