Lecture Notes on 3 Jul 2013 // Illustration of labeled break int sum = 0; loop1: for (int i = 0; i < 10; i++) { loop2: for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { sum = sum + i * j * k; if (sum > 50) { break loop1; } } } } // Program to print the nth Fibonacci term import java.util.*; public class Fib { public static void main (String[] args) { Scanner sc = new Scanner (System.in); System.out.print ("Enter Fibonacci term: "); int n = sc.nextInt(); if ((n == 0) || (n == 1)) { System.out.println (n); } else { int f1 = 0; int f2 = 1; int f = f1 + f2; for (int i = 0; i < n - 2; i++) { f1 = f2; f2 = f; f = f1 + f2; } System.out.println (f); } } } // Program to compute Pythagorean triplets less than 100 public class Triples { public static void main (String[] args) { for (int c = 100; c >= 2; c--) { for (int b = c - 1; b >= 1; b--) { for (int a = b; a >= 1; a --) { if ((a * a + b * b) == c * c) { System.out.println (a + ", " + b + ", " + c); } } } } } } // Program to compute the longest Collatz sequence less than a million import java.util.*; public class Collatz { public static void main (String[] args) { int maxSeqLength = 0; int maxN = 1; for (int i = 2; i < 1000000; i++) { int seqLength = 1; int n = i; while (n > 1) { if (n % 2 == 0) n = n / 2; else n = 3 * n + 1; seqLength++; } if (seqLength > maxSeqLength) { maxSeqLength = seqLength; maxN = i; } } System.out.println ("Max sequence length: " + maxSeqLength); System.out.println ("Number with max sequence length: " + maxN); } }