By Mahesh biradar
Read Number from Console and Check if it is a Palindrome Number
/*- Read Number from Console and Check if it is a Palindrome Number
- This Java example shows how to input the number from console and
- check if the number is a palindrome number or not.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class InputPalindromeNumberExample {
- public static void main(String[] args) {
- System.out.println("Enter the number to check..");
- int number = 0;
- try
- {
- //take input from console
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //parse the line into int
- number = Integer.parseInt(br.readLine());
- }
- catch(NumberFormatException ne)
- {
- System.out.println("Invalid input: " + ne);
- System.exit(0);
- }
- catch(IOException ioe)
- {
- System.out.println("I/O Error: " + ioe);
- System.exit(0);
- }
- System.out.println("Number is " + number);
- int n = number;
- int reversedNumber = 0;
- int temp=0;
- //reverse the number
- while(n > 0){
- temp = n % 10;
- n = n / 10;
- reversedNumber = reversedNumber * 10 + temp;
- }
- /*
- * if the number and it's reversed number are same, the number is a
- * palindrome number
- */
- if(number == reversedNumber)
- System.out.println(number + " is a palindrome number");
- else
- System.out.println(number + " is not a palindrome number");
- }
- }
- /*
- Output of the program would be
- Enter the number to check..
- 121
- Number is 121
- 121 is a palindrome number
- */
prime numbers java
- /*
- Prime Numbers Java Example
- This Prime Numbers Java example shows how to generate prime numbers
- between 1 and given number using for loop.
- */
- public class GeneratePrimeNumbersExample {
- public static void main(String[] args) {
- //define limit
- int limit = 100;
- System.out.println("Prime numbers between 1 and " + limit);
- //loop through the numbers one by one
- for(int i=1; i < 100; i++){
- boolean isPrime = true;
- //check to see if the number is prime
- for(int j=2; j < i ; j++){
- if(i % j == 0){
- isPrime = false;
- break;
- }
- }
- // print the number
- if(isPrime)
- System.out.print(i + " ");
- }
- }
- }
- /*
- Output of Prime Numbers example would be
- Prime numbers between 1 and 100
- 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
- */
list Odd numbers java
/*
List Odd Numbers Java Example
This List Odd Numbers Java Example shows how to find and list odd
numbers between 1 and any given number.
*/
public class ListOddNumbers {
public static void main(String[] args) {
//define the limit
int limit = 50;
System.out.println("Printing Odd numbers between 1 and " + limit);
for(int i=1; i <= limit; i++){
//if the number is not divisible by 2 then it is odd
if( i % 2 != 0){
System.out.print(i + " ");
}
}
}
}
/*
Output of List Odd Numbers Java Example would be
Printing Odd numbers between 1 and 50
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
*/
List Even Numbers Java
/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
public class ListEvenNumbers {
public static void main(String[] args) {
//define limit
int limit = 50;
System.out.println("Printing Even numbers between 1 and " + limit);
for(int i=1; i <= limit; i++){
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
}
/*
Output of List Even Numbers Java Example would be
Printing Even numbers between 1 and 50
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
*/
Java Pyramid
/*
Java Pyramid Example
This Java Pyramid example shows how to generate pyramid or triangle like
given below using for loop.
*
**
***
****
*****
*/
public class JavaPyramid1 {
public static void main(String[] args) {
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
System.out.print("*");
}
//generate a new line
System.out.println("");
}
}
}
/*
Output of the above program would be
*
**
***
****
*****
*/
Java Palindrome Number
/*
Java Palindrome Number Example
This Java Palindrome Number Example shows how to find if the given
number is palindrome number or not.
*/
public class JavaPalindromeNumberExample {
public static void main(String[] args) {
//array of numbers to be checked
int numbers[] = new int[]{121,13,34,11,22,54};
//iterate through the numbers
for(int i=0; i < numbers.length; i++){
int number = numbers[i];
int reversedNumber = 0;
int temp=0;
/*
* If the number is equal to it's reversed number, then
* the given number is a palindrome number.
*
* For example, 121 is a palindrome number while 12 is not.
*/
//reverse the number
while(number > 0){
temp = number % 10;
number = number / 10;
reversedNumber = reversedNumber * 10 + temp;
}
if(numbers[i] == reversedNumber)
System.out.println(numbers[i] + " is a palindrome number");
else
System.out.println(numbers[i] + " is not a palindrome number");
}
}
}
/*
Output of Java Palindrome Number Example would be
121 is a palindrome number
13 is not a palindrome number
34 is not a palindrome number
11 is a palindrome number
22 is a palindrome number
54 is not a palindrome number
*/
Infinite For loop
/*
Infinite For loop Example
This Java Example shows how to create a for loop that runs infinite times
in Java program. It happens when the loop condition is always evaluated as true.
*/
public class InfiniteForLoop {
public static void main(String[] args) {
/*
* Its perfectely legal to skip any of the 3 parts of the for loop.
* Below given for loop will run infinite times.
*/
for(;;)
System.out.println("Hello");
/*
* To terminate this program press ctrl + c in the console.
*/
}
}
/*
Output would be
Hello
Hello
Hello
Hello
..
..
*/
Generate Pyramid For a Given Number
/*
Generate Pyramid For a Given Number Example
This Java example shows how to generate a pyramid of numbers for given
number using for loop example.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class GeneratePyramidExample {
public static void main (String[] args) throws Exception{
BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter Number:");
int as= Integer.parseInt (keyboard.readLine());
System.out.println("Enter X:");
int x= Integer.parseInt (keyboard.readLine());
int y = 0;
for(int i=0; i<= as ;i++){
for(int j=1; j <= i ; j++){
System.out.print(y + "\t");
y = y + x;
}
System.out.println("");
}
}
}
/*
Output of this example would be
Enter Number:
5
Enter X:
1
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
----------------------------------------------
Enter Number:
5
Enter X:
2
0
2 4
6 8 10
12 14 16 18
20 22 24 26 28
----------------------------------------------
Enter Number:
5
Enter X:
3
0
3 6
9 12 15
18 21 24 27
30 33 36 39 42
*/
Fibonacci Series Java
/*
Fibonacci Series Java Example
This Fibonacci Series Java Example shows how to create and print
Fibonacci Series using Java.
*/
public class JavaFibonacciSeriesExample {
public static void main(String[] args) {
//number of elements to generate in a series
int limit = 20;
long[] series = new long[limit];
//create first 2 series elements
series[0] = 0;
series[1] = 1;
//create the Fibonacci series and store it in an array
for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2];
}
//print the Fibonacci series numbers
System.out.println("Fibonacci Series upto " + limit);
for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
}
}
}
/*
Output of the Fibonacci Series Java Example would be
Fibonacci Series upto 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
*/
multiple variables in for loop
/*
Declare multiple variables in for loop Example
This Java Example shows how to declare multiple variables in Java For loop using
declaration block.
*/
public class DeclaringMultipleVariablesInForLoopExample {
public static void main(String[] args) {
/*
* Multiple variables can be declared in declaration block of for loop.
*/
for(int i=0, j=1, k=2 ; i<5 ; i++)
System.out.println("I : " + i + ",j : "+ j + ", k : " + k);
/*
* Please note that the variables which are declared, should be of same type
* as in this example int.
*/
//THIS WILL NOT COMPILE
//for(int i=0, float j; i < 5; i++);
}
}
multiple variables in for loop
/*
Declare multiple variables in for loop Example
This Java Example shows how to declare multiple variables in Java For loop using
declaration block.
*/
public class DeclaringMultipleVariablesInForLoopExample {
public static void main(String[] args) {
/*
* Multiple variables can be declared in declaration block of for loop.
*/
for(int i=0, j=1, k=2 ; i<5 ; i++)
System.out.println("I : " + i + ",j : "+ j + ", k : " + k);
/*
* Please note that the variables which are declared, should be of same type
* as in this example int.
*/
//THIS WILL NOT COMPILE
//for(int i=0, float j; i < 5; i++);
}
}
Calculate Average value of Array elements using Java
/*
Calculate Average value of Array elements using Java Example
This Java Example shows how to calculate average value of array
elements.
*/
public class CalculateArrayAverageExample {
public static void main(String[] args) {
//define an array
int[] numbers = new int[]{10,20,15,25,16,60,100};
/*
* Average value of array elements would be
* sum of all elements/total number of elements
*/
//calculate sum of all array elements
int sum = 0;
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
//calculate average value
double average = sum / numbers.length;
System.out.println("Average value of array elements is : " + average);
}
}
/*
Output of Calculate Average value of Array elements using Java Example would be
Average value of array elements is : 35.0
*/