Lovely photo

By Mahesh biradar

Read Number from Console and Check if it is a Palindrome Number


  1. /*
  2.   Read Number from Console and Check if it is a Palindrome Number
  3.   This Java example shows how to input the number from console and
  4.   check if the number is a palindrome number or not.
  5. */
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. public class InputPalindromeNumberExample {
  10. public static void main(String[] args) {
  11. System.out.println("Enter the number to check..");
  12. int number = 0;
  13. try
  14. {
  15. //take input from console
  16. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17. //parse the line into int
  18. number = Integer.parseInt(br.readLine());
  19. }
  20. catch(NumberFormatException ne)
  21. {
  22. System.out.println("Invalid input: " + ne);
  23. System.exit(0);
  24. }
  25. catch(IOException ioe)
  26. {
  27. System.out.println("I/O Error: " + ioe);
  28. System.exit(0);
  29. }
  30. System.out.println("Number is " + number);
  31. int n = number;
  32. int reversedNumber = 0;
  33. int temp=0;
  34. //reverse the number
  35. while(n > 0){
  36. temp = n % 10;
  37. n = n / 10;
  38. reversedNumber = reversedNumber * 10 + temp;
  39. }
  40. /*
  41. * if the number and it's reversed number are same, the number is a
  42. * palindrome number
  43. */
  44. if(number == reversedNumber)
  45. System.out.println(number + " is a palindrome number");
  46. else
  47. System.out.println(number + " is not a palindrome number");
  48. }
  49. }
  50. /*
  51. Output of the program would be
  52. Enter the number to check..
  53. 121
  54. Number is 121
  55. 121 is a palindrome number
  56. */

prime numbers java

  1. /*
  2.   Prime Numbers Java Example
  3.   This Prime Numbers Java example shows how to generate prime numbers
  4.   between 1 and given number using for loop.
  5. */
  6. public class GeneratePrimeNumbersExample {
  7. public static void main(String[] args) {
  8. //define limit
  9. int limit = 100;
  10. System.out.println("Prime numbers between 1 and " + limit);
  11. //loop through the numbers one by one
  12. for(int i=1; i < 100; i++){
  13. boolean isPrime = true;
  14. //check to see if the number is prime
  15. for(int j=2; j < i ; j++){
  16. if(i % j == 0){
  17. isPrime = false;
  18. break;
  19. }
  20. }
  21. // print the number
  22. if(isPrime)
  23. System.out.print(i + " ");
  24. }
  25. }
  26. }
  27. /*
  28. Output of Prime Numbers example would be
  29. Prime numbers between 1 and 100
  30. 1 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
  31. */




list Odd numbers java

  1. /*

  2. List Odd Numbers Java Example

  3. This List Odd Numbers Java Example shows how to find and list odd

  4. numbers between 1 and any given number.

  5. */

  6.  

  7. public class ListOddNumbers {

  8.  

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

  10.  

  11. //define the limit

  12. int limit = 50;

  13.  

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

  15.  

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

  17.  

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

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

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

  21. }

  22. }

  23. }

  24. }

  25.  

  26. /*

  27. Output of List Odd Numbers Java Example would be

  28. Printing Odd numbers between 1 and 50

  29. 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

  30. */



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. }

Calculate Average value of Array elements using Java

  1. /*

  2. Calculate Average value of Array elements using Java Example

  3. This Java Example shows how to calculate average value of array

  4. elements.

  5. */

  6. public class CalculateArrayAverageExample {

  7.  

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

  9.  

  10. //define an array

  11. int[] numbers = new int[]{10,20,15,25,16,60,100};

  12.  

  13. /*

  14. * Average value of array elements would be

  15. * sum of all elements/total number of elements

  16. */

  17.  

  18. //calculate sum of all array elements

  19. int sum = 0;

  20.  

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

  22. sum = sum + numbers[i];

  23.  

  24. //calculate average value

  25. double average = sum / numbers.length;

  26.  

  27. System.out.println("Average value of array elements is : " + average);

  28. }

  29. }

  30.  

  31. /*

  32. Output of Calculate Average value of Array elements using Java Example would be

  33. Average value of array elements is : 35.0

  34. */