Java Example

Java Factorial Example
class Factorial{  
 void fact(int  n){  
  int fact=1;  
  for(int i=1;i<=n;i++){  
   fact=fact*i;  
  }  
 System.out.println("factorial is "+fact);  
}  
public static void main(String args[]){  
 new Factorial().fact(5);
}  
}  
Output:Factorial is 120
Java Factorial Using Recursion Example
public class JavaFactorialUsingRecursion {
        public static void main(String args[]){
                int number= 5;
                System.out.println("Factorial of the number is: " + fact(number));
        }
        static int fact(int a)
        {
                if(<= 1)
                        return 1;
                else
                        //else call the same function with the value - 1
                        return a * fact(a-1);
        }
}
Output: Factorial of the number is: 120
Java Fibonacci series Example
In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.
Fibonacci Series in Java without using recursion
package com.om.test;
public class FibonacciExample {
       public static void main(String[] args) {
             int n1 =0, n2=1, n3, i, count=10;   
             System. out .print(n1 +" " +n2 ); //printing 0 and 1   
        for (i =2; i<= count;++ i)
//loop starts from 2 because 0 and 1 are already printed    
             {   
              n3n1n2;   
              System. out .print(" " +n3 );   
              n1n2;   
              n2n3;   
             }   
      }
}
Output: 0 1 1 2 3 5 8 13 21 34 55

Other Way :
public class Fibonacci {
       public static void main(String a[]) {
             int Count = 10;
             int [] feb = new int [Count ];
             feb [0] = 0;
             feb [1] = 1;
             for (int i = 2; i < Counti++) {
                   feb [i ] = feb[ i - 1] + feb [i - 2];
            }
             for (int i = 0; i < Counti++) {
                  System. out .print(feb [i ] + " ");
            }
      }
}
Output: 0 1 1 2 3 5 8 13 21 34

Fibonacci Series using recursion in java
public class FibonacciRecursion {
       public static int fibonaci( int num ) {
             if (num == 0) {
                   return 0;
            } else if (num == 1) {
                   return 1;
            } else {
                   return fibonacinum - 1) + fibonacinum - 2);
            }
      }
       public static void main(String[] args) {
             int number = 10;
             for (int i = 0; i <= numberi++) {
                  System. out .print(fibonacii) + " " );
            }
      }
}
Output: 0 1 1 2 3 5 8 13 21 34 55
Prime Number Example
Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

package com.om.test;
public class PrimeNumber {
       public static void main(String[] args) {
             // Loop get prime numbers between 1 to 50
             for (int i = 2; i < 50; i++) {
                   boolean isPrimeNumber = true;
                   for (int j = 2; j < ij++ ) {
                         if (i % j == 0)
                               isPrimeNumber = false ;
                         break ;
                  }
                   if (isPrimeNumber ) {
                        System. out .print(i + " ");
                  }
            }
      }
}
Output: 2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

Java Even Number Example
package com.om.test;
public class EvenNumber {
       public static void main(String[] args) {
             int num = 20;
             for (int i = 1; i <= numi++) {
   // mod (%) will find even number by matching reminder 0 Only print even number
                   if (i % 2 == 0) {
                        System. out .print(i + " ");
                  }
            }
      }
}
Output: 2 4 6 8 10 12 14 16 18 20 

Java Odd Number Example
package com.om.test;
public class OddNumber {
       public static void main(String[] args) {
             int num = 20;
             for (int i = 1; i <= numi++) {
                   // mod will find odd number by not matching with 0
                   if (i % 2 != 0) {
                        System. out .print(i + " ");
                  }
            }
      }
}
Output:-1 3 5 7 9 11 13 15 17 19 

Java swapping two numbers without using third variable.
package com.om.test;
public class SwapTwoNumbers {
       public static void main(String[] args) {
             int a = 40, b = 5;
             a = a * b;
             b = a / b;
             a = a / b;
            System. out .println("a= " + a);
            System. out .println("b= " + b);
      }
}
Output:-
a= 5
b= 40

Java Palindrome
A palindrome number is a number that is same after reverse. For example 454
package com.om.test;
public class Palindrome {
       public static void main(String[] args) {
             int r , sum = 0, temp;
             int n = 454; 
       // It is the number variable to be checked for palindrome

             temp = n ;
             while (n > 0) {
                   r = n % 10; // getting remainder
                   sum = (sum * 10) + r;
                   n = n / 10;
            }
             if (temp == sum)
                  System. out .println("palindrome number " );
             else
                  System. out .println("not palindrome" );
      }
}
Output:-palindrome number
Java Armstrong Number
Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So: 1+125+27=153  
package com.om.test;
public class Armstrong {
       public static void main(String[] args) {
             int c = 0, atemp;
             int n = 153; // It is the number to check armstrong
             temp = n ;
             while (n > 0) {
                   a = n % 10;
                   n = n / 10;
                   c = c + ( a * a * a);
            }
             if (temp == c)
                  System. out .println("armstrong number" );
             else
                  System. out .println("Not armstrong number" );
      }
}
Output:-armstrong number

Java Post Increment (n++) and Pre Increment (++n).
Post Increment (n++): It increases the value of variable by 1 after execution of the statement.
Pre Increment (++n): It increases the value of variable by 1 before execution of the statement.
public class Postincrement {
       public static void main(String[] args) {
             int n = 10;
            System. out .println(n );
            System. out .println(n ++);
            System. out .println(n );
      }
}
Output:-
10
10
11
public class Preincrement {
       public static void main(String[] args) {
             int n = 10;
            System. out .println(n );
            System. out .println(++n );
            System. out .println(n );
      }
}
Output:-
10
11
11
Sum of 1st 10 Natural Numbers
public class Sum {
       public static void main(String[] args) {
             int n , sum = 0;
             for (n = 1; n <= 10; n++) {
                   sum += n ; // or sum=sum+n;
            }
            System. out .println(sum );
      }
}
Output:-55

Example to understand the types of variables
public class DataType {
 int data=50; //instance variable  
 static int m=100; //static variable  
void method(){  
 int n=90; //local variable  
 }  
}


No comments:

Post a Comment