Difference between 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.

Program Output
class Demo
{
	public static void main(String args[])
	{		
		int n=10;
		System.out.println(n);
		System.out.println(n++);
		System.out.println(n);
	}
}
      





10
10
11
      

Program Output
class Demo
{
	public static void main(String args[])
	{		
		int n=10;
		System.out.println(n);
		System.out.println(++n);
		System.out.println(n);
	}
}
      





10
11
11
      
 

Prime Number

Logic: Prime Number are divisible by itself only.

Not divisible by any Number Divisible by 2 ...no need to check further Divisible by 3 ...no need to check further

7%2=1
7%3=1
7%4=3
7%5=2
7%6=1

8%2=0
8%3=
8%4=
8%5=
8%6=
8%7=
9%2=1
9%3=0
9%4
9%5
9%6
9%7
9%8
Numbers are not divisible by more than half of the number
No need to check upto 6 check upto 3 only
No need to check upto 7 check upto 4 only No need to check upto 8 check upto 4 only

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Prime
{
	public static void main(String args[])
	{		
		int n, i, res;
		boolean flag=true;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter a No.");
		n=scan.nextInt();
		for(i=2;i<=n/2;i++)
		{
			res=n%i;
			if(res==0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
			System.out.println(n + " is Prime Number");
		else
			System.out.println(n + " is not Prime Number");
	}
}
      
Please Enter a No.: 7
7 is Prime Number
 

Fibonacci Series ( 1 1 2 3 5 8 13...)

Logic: Sum of previous two numbers will give us next number.

prev next sum
  shifted to prev shifted to next
1 1 2
1 2 3
2 3 5
3 5 8
5 8 13
8 13 ...
13 ... ...
prev will give you fibonacci series

Program Output
class Fibonacci
{
	public static void main(String args[])
	{		
		int prev, next, sum, n;
		prev=next=1
		for(n=1;n<=10;n++)
		{
			System.out.println(prev);
			sum=prev+next;
			prev=next;
			next=sum;
		}
	}
}
      
1
1
2
3
5
8
13
...      
 

Sum of 1st 10 Natural Numbers

Logic: Sum of previous two numbers will give us next number.

sum n sum
sum+n  
0 1 1
1 2 3
3 3 6
6 4 10
10 5 15
15 6 21
21 7 28
28 8 36
36 9 45
45 10 55

Program Output
class Sum10
{
	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);
	}
}
      
55   
 

Sum of Square of 1st 10 Natural Numbers

Logic: Sum of previous two numbers will give us next number.

sum n*n sum
sum+n*n  
0 1*1 1
1 2*2 5
5 3*3 14
14 4*4 30
30 5*5 55
55 6*6 91
91 7*7 140
140 8*8 204
204 9*9 285
285 10*10 385

Program Output
class SumSq10
{
	public static void main(String args[])
	{		
		int n, sum=0;
		for(n=1;n<=10;n++)
		{
			sum+=n;*n    //or sum=sum+n;*n
		}
		System.out.println(sum);
	}
}
      
385  
 

Factorial

Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1

prod n prod
prod*n  
1 5 5
5 4 20
20 3 60
60 2 120
120 1 120

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Factorial
{
	public static void main(String args[])
	{		
		int n, i, prod=1;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter a No.");
		n=scan.nextInt();
		for(i=n;i>=1;i--)
		{
			prod*=i;	//prod=prod*i;
		}
		System.out.println("Factorial of " + n + " is  " + prod);
	}
}
      
Please Enter a No.: 5
Factorial of 5 is 120
    
 

Biggest of 3 Numbers using Logical Operators

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Biggest3
{
	public static void main(String args[])
	{		
		int n1, n2, n3, big;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter No 1: ");
		n1=scan.nextInt();
		System.out.println("Please Enter No 2: ");
		n2=scan.nextInt();
		System.out.println("Please Enter No 3: ");
		n3=scan.nextInt();
		if(n1>n2 && n1>n3)
			big=n1;
		else if(n2>n1 && n2>n3)
			big=n2;
		else
			big=n3;
		System.out.println("Biggest No: " + big);
	}
}
      
Please Enter No 1: 5
Please Enter No 2: 23
Please Enter No 3: 14
Biggest No: 23
    
 

Biggest of 3 Numbers using Nested If

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Biggest3
{
	public static void main(String args[])
	{		
		int n1, n2, n3, big;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter No 1: ");
		n1=scan.nextInt();
		System.out.println("Please Enter No 2: ");
		n2=scan.nextInt();
		System.out.println("Please Enter No 3: ");
		n3=scan.nextInt();
		if(n1>n2)
		{
			if(n1>n3)
				big=n1;
			else
				big=n3;
		}
		else
		{
			if(n2>n3)
				big=n2;
			else
				big=n3;
		}
		System.out.println("Biggest No: " + big);
	}
}
      
Please Enter No 1: 5
Please Enter No 2: 23
Please Enter No 3: 14
Biggest No: 23
    
 

Swap 2 Numbers using 3rd Variable

Logic:

n1 n2 temp
5 23 0
5 23 5
23 23 5
23 5 5

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Swap
{
	public static void main(String args[])
	{		
		int n1, n2, temp;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter No 1: ");
		n1=scan.nextInt();
		System.out.println("Please Enter No 2: ");
		n2=scan.nextInt();
		temp=n1;
		n1=n2;
		n2=temp;
		System.out.println("First No: " + n1);
		System.out.println("Second No: " + n2);

	}
}
      
Please Enter No 1: 5
Please Enter No 2: 23
First No: 23
Second No: 5
    
 

Swap 2 Numbers without using 3rd Variable

Logic:

n1 n2
10 5
15 5
15 10
5 10

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Swap
{
	public static void main(String args[])
	{		
		int n1, n2, temp;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter No 1: ");
		n1=scan.nextInt();
		System.out.println("Please Enter No 2: ");
		n2=scan.nextInt();
		n1=n1+n2;
		n2=n1-n2;
		n1=n1-n2
		System.out.println("First No: " + n1);
		System.out.println("Second No: " + n2);

	}
}
      
Please Enter No 1: 10
Please Enter No 2: 5
First No: 5
Second No: 10
    
 

Sum of Digits

Logic: 513 -> 5+1+3=9

n res n sum
513     0
513%10 3   3
513/10   51 3
       
51%10 1   4
51/10   5 4
       
5%10 5   9
5/10   0 9

Program Output
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class SumDigits
{
	public static void main(String args[])
	{		
		int n, res;
		Scanner scan= new Scanner(System.in);
		System.out.println("Please Enter No.: ");
		n1=scan.nextInt();
		while(n>0)
		{
			res=n%10;
			n=n/10;
			sum+=res;	//sum=sum+res;
		}
		System.out.println("Second No: " + n2);

	}
}
      
Please Enter No 1: 10
Please Enter No 2: 5
First No: 5
Second No: 10