Wednesday, October 17, 2012

Java Exercise #25: 50 Prime Numbers

This program is to print prime numbers.

Exercise: Write a program that prints first 50 prime numbers in five lines (10 prime numbers per line)


The first 50 prime numbers are: 
2  3  5  7  11  13  17  19  23  29
31  37  41  43  47  53  59  61  67  71
73  79  83  89  97  101  103  107  109  113
127  131  137  139  149  151  157  163  167  173
179  181  191  193  197  199  211  223  227  229


Download Code: 50 Prime Numbers

Tuesday, October 16, 2012

Java Exercise #24: Sum Skipping Numbers

This program is an exercise to use the keyword continue in loops.

Exercise: Write a program that sums integers from 0 to 20 but skipping the numbers 10 and 11.


The Sum is: 189


Download Code: Sum Skipping Numbers

Monday, October 15, 2012

Java Exercise #23: Sum Till 100

This program is an exercise to use the keyword break in loops.

Exercise: Write a program that sums integers from 0 to up, until the sum is greater than or equal to 100.


Sum is: 105
Number is: 14

Download Code: Sum Till 100

Sunday, October 14, 2012

Java Exercise #22: Number Pyramid

This program is to print a number pyramid.

Exercise: Write a program that prompts the user to enter an integer and then prints the pyramid wuth those many number of rows as shown below.


Enter the number of lines (between 1 and 15): 11
                                1
                             2  1  2
                          3  2  1  2  3
                       4  3  2  1  2  3  4
                    5  4  3  2  1  2  3  4  5
                 6  5  4  3  2  1  2  3  4  5  6
              7  6  5  4  3  2  1  2  3  4  5  6  7
           8  7  6  5  4  3  2  1  2  3  4  5  6  7  8
        9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9
    10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10
 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11


Download Code: Number Pyramid

Saturday, October 13, 2012

Java Exercise #21: Greatest Common Divisor

This program is to print Greatest Common Divisor(GCD) for two integers.

Exercise: Write a program that takes two integers as input from user and prints the GCD as below.


Enter the first integer: 125
Enter the second integer: 2525
Greatest Common Divisor for 125 and 2525 is 25

Download Code: GCD

Friday, October 12, 2012

Java Exercise #20: Multiplication Table

This program is to print multiplication table. This uses nested for loop

Exercise: Write a program that prints the multiplication table as below.


        Multiplication Table
       1   2   3   4   5   6   7   8   9
----------------------------------------
1 |    1   2   3   4   5   6   7   8   9
2 |    2   4   6   8  10  12  14  16  18
3 |    3   6   9  12  15  18  21  24  27
4 |    4   8  12  16  20  24  28  32  36
5 |    5  10  15  20  25  30  35  40  45
6 |    6  12  18  24  30  36  42  48  54
7 |    7  14  21  28  35  42  49  56  63
8 |    8  16  24  32  40  48  56  64  72
9 |    9  18  27  36  45  54  63  72  81

Download Code: Multiplication Table

Thursday, October 11, 2012

Java Exercise #19: Sentinel Value

This program is to take series of input from user as ints and then sum them up.

Exercise: Write a program that requests user to enter integers util the value entered is zero (which is used as sentinel value). Sum all the integer values and display it.

Enter an int value (Progam exits if the value is 0): 5
Enter an int value (Progam exits if the value is 0): 3
Enter an int value (Progam exits if the value is 0): 2
Enter an int value (Progam exits if the value is 0): 1
Enter an int value (Progam exits if the value is 0): 0
The sum is 11


Download Code: Sentinel Value