List Even Numbers Java

  1. /*

  2. List Even Numbers Java Example

  3. This List Even Numbers Java Example shows how to find and list even

  4. numbers between 1 and any given number.

  5. */

  6.  

  7. public class ListEvenNumbers {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. //define limit

  12. int limit = 50;

  13.  

  14. System.out.println("Printing Even numbers between 1 and " + limit);

  15.  

  16. for(int i=1; i <= limit; i++){

  17.  

  18. // if the number is divisible by 2 then it is even

  19. if( i % 2 == 0){

  20. System.out.print(i + " ");

  21. }

  22. }

  23. }

  24. }

  25.  

  26. /*

  27. Output of List Even Numbers Java Example would be

  28. Printing Even numbers between 1 and 50

  29. 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

  30. */

Java Pyramid

  1. /*

  2. Java Pyramid Example

  3. This Java Pyramid example shows how to generate pyramid or triangle like

  4. given below using for loop.

  5.  

  6. *

  7. **

  8. ***

  9. ****

  10. *****

  11. */

  12.  

  13. public class JavaPyramid1 {

  14.  

  15. public static void main(String[] args) {

  16.  

  17. for(int i=1; i<= 5 ;i++){

  18.  

  19. for(int j=0; j < i; j++){

  20. System.out.print("*");

  21. }

  22.  

  23. //generate a new line

  24. System.out.println("");

  25. }

  26. }

  27. }

  28.  

  29. /*

  30. Output of the above program would be

  31. *

  32. **

  33. ***

  34. ****

  35. *****

  36. */

Java Palindrome Number

  1. /*

  2. Java Palindrome Number Example

  3. This Java Palindrome Number Example shows how to find if the given

  4. number is palindrome number or not.

  5. */

  6.  

  7.  

  8. public class JavaPalindromeNumberExample {

  9.  

  10. public static void main(String[] args) {

  11.  

  12. //array of numbers to be checked

  13. int numbers[] = new int[]{121,13,34,11,22,54};

  14.  

  15. //iterate through the numbers

  16. for(int i=0; i < numbers.length; i++){

  17.  

  18. int number = numbers[i];

  19. int reversedNumber = 0;

  20. int temp=0;

  21.  

  22. /*

  23. * If the number is equal to it's reversed number, then

  24. * the given number is a palindrome number.

  25. *

  26. * For example, 121 is a palindrome number while 12 is not.

  27. */

  28.  

  29. //reverse the number

  30. while(number > 0){

  31. temp = number % 10;

  32. number = number / 10;

  33. reversedNumber = reversedNumber * 10 + temp;

  34. }

  35.  

  36. if(numbers[i] == reversedNumber)

  37. System.out.println(numbers[i] + " is a palindrome number");

  38. else

  39. System.out.println(numbers[i] + " is not a palindrome number");

  40. }

  41.  

  42. }

  43. }

  44.  

  45. /*

  46. Output of Java Palindrome Number Example would be

  47. 121 is a palindrome number

  48. 13 is not a palindrome number

  49. 34 is not a palindrome number

  50. 11 is a palindrome number

  51. 22 is a palindrome number

  52. 54 is not a palindrome number

  53. */

Infinite For loop

  1. /*

  2.   Infinite For loop Example

  3.   This Java Example shows how to create a for loop that runs infinite times

  4.   in Java program. It happens when the loop condition is always evaluated as true.

  5. */

  6.  

  7. public class InfiniteForLoop {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. /*

  12.   * Its perfectely legal to skip any of the 3 parts of the for loop.

  13.   * Below given for loop will run infinite times.

  14.   */

  15. for(;;)

  16. System.out.println("Hello");

  17.  

  18. /*

  19.   * To terminate this program press ctrl + c in the console.

  20.   */

  21. }

  22. }

  23.  

  24. /*

  25. Output would be

  26. Hello

  27. Hello

  28. Hello

  29. Hello

  30. ..

  31. ..

  32. */

Generate Pyramid For a Given Number

  1. /*

  2. Generate Pyramid For a Given Number Example

  3. This Java example shows how to generate a pyramid of numbers for given

  4. number using for loop example.

  5. */

  6.  

  7. import java.io.BufferedReader;

  8. import java.io.InputStreamReader;

  9.  

  10. public class GeneratePyramidExample {

  11.  

  12. public static void main (String[] args) throws Exception{

  13.  

  14. BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));

  15.  

  16. System.out.println("Enter Number:");

  17. int as= Integer.parseInt (keyboard.readLine());

  18. System.out.println("Enter X:");

  19. int x= Integer.parseInt (keyboard.readLine());

  20.  

  21. int y = 0;

  22.  

  23. for(int i=0; i<= as ;i++){

  24.  

  25. for(int j=1; j <= i ; j++){

  26. System.out.print(y + "\t");

  27. y = y + x;

  28. }

  29.  

  30. System.out.println("");

  31. }

  32. }

  33. }

  34.  

  35. /*

  36. Output of this example would be

  37.  

  38. Enter Number:

  39. 5

  40. Enter X:

  41. 1

  42.  

  43. 0

  44. 1 2

  45. 3 4 5

  46. 6 7 8 9

  47. 10 11 12 13 14

  48.  

  49. ----------------------------------------------

  50.  

  51. Enter Number:

  52. 5

  53. Enter X:

  54. 2

  55.  

  56. 0

  57. 2 4

  58. 6 8 10

  59. 12 14 16 18

  60. 20 22 24 26 28

  61.  

  62. ----------------------------------------------

  63.  

  64. Enter Number:

  65. 5

  66. Enter X:

  67. 3

  68.  

  69. 0

  70. 3 6

  71. 9 12 15

  72. 18 21 24 27

  73. 30 33 36 39 42

  74. */

Fibonacci Series Java

  1. /*

  2. Fibonacci Series Java Example

  3. This Fibonacci Series Java Example shows how to create and print

  4. Fibonacci Series using Java.

  5. */

  6.  

  7. public class JavaFibonacciSeriesExample {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. //number of elements to generate in a series

  12. int limit = 20;

  13.  

  14. long[] series = new long[limit];

  15.  

  16. //create first 2 series elements

  17. series[0] = 0;

  18. series[1] = 1;

  19.  

  20. //create the Fibonacci series and store it in an array

  21. for(int i=2; i < limit; i++){

  22. series[i] = series[i-1] + series[i-2];

  23. }

  24.  

  25. //print the Fibonacci series numbers

  26.  

  27. System.out.println("Fibonacci Series upto " + limit);

  28. for(int i=0; i< limit; i++){

  29. System.out.print(series[i] + " ");

  30. }

  31. }

  32. }

  33.  

  34. /*

  35. Output of the Fibonacci Series Java Example would be

  36. Fibonacci Series upto 20

  37. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

  38. */

multiple variables in for loop

  1. /*

  2.   Declare multiple variables in for loop Example

  3.   This Java Example shows how to declare multiple variables in Java For loop using

  4.   declaration block.

  5. */

  6.  

  7. public class DeclaringMultipleVariablesInForLoopExample {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. /*

  12.   * Multiple variables can be declared in declaration block of for loop.

  13.   */

  14.  

  15. for(int i=0, j=1, k=2 ; i<5 ; i++)

  16. System.out.println("I : " + i + ",j : "+ j + ", k : " + k);

  17.  

  18. /*

  19.   * Please note that the variables which are declared, should be of same type

  20.   * as in this example int.

  21.   */

  22.  

  23. //THIS WILL NOT COMPILE

  24. //for(int i=0, float j; i < 5; i++);

  25. }

  26. }

multiple variables in for loop

  1. /*

  2.   Declare multiple variables in for loop Example

  3.   This Java Example shows how to declare multiple variables in Java For loop using

  4.   declaration block.

  5. */

  6.  

  7. public class DeclaringMultipleVariablesInForLoopExample {

  8.  

  9. public static void main(String[] args) {

  10.  

  11. /*

  12.   * Multiple variables can be declared in declaration block of for loop.

  13.   */

  14.  

  15. for(int i=0, j=1, k=2 ; i<5 ; i++)

  16. System.out.println("I : " + i + ",j : "+ j + ", k : " + k);

  17.  

  18. /*

  19.   * Please note that the variables which are declared, should be of same type

  20.   * as in this example int.

  21.   */

  22.  

  23. //THIS WILL NOT COMPILE

  24. //for(int i=0, float j; i < 5; i++);

  25. }

  26. }