/*
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
*/
List Even Numbers Java
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++);
}
}