Gadgets

Thursday, 3 December 2015

Print even numbers in java

Print even numbers in java
Any integer number is divisible by 2 and produces zero remainder, that integer number is even number.
import java.util.Scanner;
public class EvenNumbers {
    public static void main(String[] arg){
        int number;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any number :");
        number=sc.nextInt();
        System.out.println("Even Numbers are");
        for(int i=0;i<=number;i++){
          if(i%2==0){
              System.out.print(i+" ");
          } 
        }       
    }   
}
/*
Output:
Enter any number :
20
Even Numbers are
0 2 4 6 8 10 12 14 16 18 20

*/

Wednesday, 25 November 2015

if condition program in java

if condition program in java
If condition in java:
“if “ is a conditional statement is java, it is check the condition only once. If condition is true then conditional block will be execute. If condition is false then “if” block will be exit and execute the else block.
Syntax:
if(condition){
               .........
               statments ........
}
               else{
                       ...........
                     statmetns ........
               }
 public class IfCondTest {
    public static void main(String args[])  {
        int a=30,b=20;
        if(a>b)  {
           System.out.println("a is greater");
        }
        else{
            System.out.println("b is greater"); 
        }
    }    
}
Output:
a is greater
Example for else if condition:
public class IfCondTest {
    public static void main(String args[])  {
        int a=10,b=20;
        if(a>b) {
           System.out.println("a is greater");
        }
        else if(b>a)  {
            System.out.println("b is greater"); 
        }
        else{
           System.out.println("a and b are equal");  
        }
    }    
}
Output:
b is greater
 
Nested If condition (if condition inside if condition block):
public class IfCondition {
    public static void main(String args[])  {
        int a=10,b=20;
        if(a<b) {
           System.out.println("a is lessthen b");
            if(b>a) {
            System.out.println("b is greater"); 
        }
        }        
        else{
           System.out.println("a and b are equal");  
        }
    }    
}
Output:
a is lessthen b
b is greater

Multiple conditions inside if: 
if(a<b&&b>c) in this case both conditions should be true,
if(a<b||b>c) in this case at least one condition should be true,
public class IfCondition {
    public static void main(String args[])   {
        int a=10,b=20,c=30;
        if(a<b&&b>c)  {
           System.out.println("a is lessthen b");            
        }       
        else{
           System.out.println("a greaterthen b");  
        }
    }    
}
Output:
a greaterthen b

Tuesday, 24 November 2015

for loop Program in java

For loop Program in java
  1. for loop is the loop statement in java programming and for loop is used to execute set of statements until the condition is true.
  2. for loop checks the contrition and executes the set of the statements.
  3. for loop contain the following statements such as “Initialization”, “Condition” and “Increment/Decrement” statement.
for(var-initialization; condition; increment/decrement)


For loop Example:
public class ForLoopTest {
    public static void main(String a[]){
        int i;
        for(i=1;i<=5;i++){
            System.out.println(i);
        }
    }    
}
Output:
1
2
3
4
5 
Error free example:
It is an error free but it prints infinite numbers 
public static void main(String a[]){
        boolean exp = true;
         for(int i=1; exp ; i++){
              System.out.println(i);
         }
    }
Compile time error:
It gives compile time error because int exp = true;. Here data type is int value is boolean(true) 
public static void main(String a[]){
        int exp = true;
         for(int i=1; exp ; i++){
              System.out.println(i);
         }
    }
For each loop:
For-each loop introduced in java5 version, it is an advanced of for loop and it is more reliable.
public class ForeachLoop {
   public static void main(String args[]){  
   int a[]={10,20,30,40};
   for(int i:a){  
   System.out.println(i);  
   }   
 }       
}
Output:
10
20
30
40

while and do while loop programs in java

While and do while loops
while loop: 
It will check the condition first then if the condition is true, then the inside actions loop will be executed. This will continue until condition is true. When condition will become false, loop will exit.
public class WhileTest {
    public static void main(String a[])  {
        int i=5;
        System.out.println("print numbers between 5 to 15");
        while(i<=15)  {            
            System.out.println(i);
            i++;
        }
    }
}
 
Output:
print numbers between 5 to 15
5
6
7
8
9
10
11
12
13
14
15 
do while loop: 
First loop will execute and then condition is check. It will check the condition first then if the    condition is true, then the inside actions loop will be executed. This will continue until condition  is true. When condition will become false, loop will exit. 
public class DoWhileTest {
    public static void main(String a[])  {
        int i=10;
        System.out.println("numbers between 10 to 20");
        do {            
            System.out.println(i);
            i++;
        }while(i<=20);
    }    
}
Output:
numbers between 10 to 20
10
11
12
13
14
15
16
17
18
19
20

Factorial Program in Java

Factorial Program in Java
Example for Factorial is 
4!=4*3*2*1
5!=5*4*3*2*1
 
import java.util.Scanner;
public class Factorial {
    public static void main(String[] arg){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number : ");
        int num1=sc.nextInt();
        int fact = 1;
        for(int i=1;i<=num1;i++){
        fact=fact*i;       
        }
         System.out.println(num1+"! is = "+fact);    
    }    
}
 
Output:
Enter number : 
5
5! is = 120

Fibonacci series program in java

Fibonacci series program in java
The main aim of Fibonacci series that we add current number+ previous number= result, here I shown in the diagram clearly. Initially first we add current number two times after that we add current number+ previous number.

public class Fibonacci {
    public static void main(String s[]){
        int n=10,i,f0=1,f1=1,f2=0;
               for(i=1;i<=n;i++)
               {
                 f2=f0+f1;
                 f0=f1;
                 f1=f2;
                 f2=f0;
          System.out.println(f2);
               }
    }    
}
 
Output:
1
2
3
5
8
13
21
34
55
89
Explanation in diagram



Print numbers in triangle format

 
Print numbers in right triangle format 
public class Triangle {
    public static void main(String[] arg){
        for(int i=1;i<=5;i++){  // loop for number of rows
            for(int j=1;j<=i;j++){
                System.out.print(j); // loop for print numbers
            }
            System.out.println();// new line
        }
    }    
} 
Output:
1
12
123
1234
12345
 Print numbers in left triangle format 
  public class Triangle {
     public static void main(String[] arg){
        for(int i=0;i<=6;i++){   // loop for number of rows     
            for(int space=i;space<6;space++){  // loop for spaces for each row
                System.out.print(" ");
            }                   
            for(int j=0;j<i;j++){   // loop for print stars (*)
                System.out.print("*");
            }
            System.out.println("");// new line
            }       
      }    
Output:
                
     *
    **
   ***
  ****
 *****
******
 
Print numbers in triangle format 
public class Triangle {
       public static void main(String[] arg){
        for(int i=0;i<=6;i++)  // loop for number of rows
                                              {            
            for(int space=i;space<6;space++){ // loop for spaces for each row
                System.out.print(" ");
            }                   
            for(int j=0;j<i*2+1;j++){ 
 
            for(int j=0;j<i;j++){  // loop for print stars (*)
 
                System.out.print("*");
            }
            System.out.println("");// new line
            }          
        }    
}
 
Output:
      *
     ***
    *****
   *******
  *********
 ***********
*************
 

Monday, 23 November 2015

Display alphabets from A to Z


Display alphabets from ‘A’ to ‘Z’
public class Alphabets {
    public static void main(String[] arg){
        for(char ch=’A’;ch<=’Z’;ch++){
            System.out.print(ch+", ");
        }
    }    
}
 
Output:
D:\Java>java Alphabets
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,