FreeLearningBlogs

free learning,tutorial,programs

View Blog

Responsive Ads Here

Wednesday, 27 April 2016

Java Switch Statement Example

Program that shows the use of Switch Statement.

Method 1



class Switch{

public static void main(String args[]){

int n;
n=9;
switch(n){

case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("Jun");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid Choice");
break;
}

}
}

Output

September


Method 2


import java.util.*;

class Switch{
public static void main(String args[])
{
int n;

Scanner in  = new Scanner(System.in);
System.out.print("Enter N Number ==> ");
n = in.nextInt();
switch(n)
{
case 1:
System.out.print("One");
break;

case 2:
System.out.print("Two");
break;

case 3:
System.out.print("Three");
break;

case 4:
System.out.print("Four");
break;

case 5:
System.out.print("Five");
break;

default:
System.out.println("Invalid Choice");
break;
}
}
}


Output


Enter N Number ==> 5
Five

Java Largest Three Number Example

Program that will find Largest Number from given Three Numbers

Method 1

class LargestThreeNo{

public static void main(String args[]){

int a=10,b=20,c=30;
if(a>b){
if(a>c){
System.out.println("a is Largest");
}else
{
System.out.println("c is Largest");
}
}else{

if(b>c){

System.out.println("b is Largest");
}else{

System.out.println("c is Largest");
}
}

}

}


Output

c is Largest 


Method 2

class LargestThreeNo{

public static void main(String args[]){


int a=10,b=20,c=30;

int large;
large = (a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println("Largest No ==> " + large);
}

}


Output

Largest No ==> 30 


Method 3

import java.util.*;

class LargestThreeNo{


public static void main(String args[]){


int a=10,b=20,c=30;

int large;
Scanner in = new Scanner(System.in);
  System.out.print("Enter a b and c Number ==> ");
  a = in.nextInt();
  b = in.nextInt();
  c = in.nextInt();
large = (a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println("Largest No ==> " + large);
}

}


Output

Enter a b and c Number ==> 10 20 30
Largest No ==> 30 


Method 4

import java.util.*;

class LargestThreeNo{


public static void main(String args[]){


int a=10,b=20,c=30;

int large;
a = Integer.parseInt(args[0]);
     b = Integer.parseInt(args[1]);
     c = Integer.parseInt(args[2]);

large = (a>b)?((a>c)?a:c):((b>c)?b:c);

System.out.println("Largest No ==> " + large);
}

}


Output



Method 5

import java.util.*;

class LargestThreeNo{

int large;
LargestThreeNo()
{
  int a,b,c;
  Scanner in = new Scanner(System.in);
  System.out.print("Enter a b and c Number ==> ");
  a = in.nextInt();
  b = in.nextInt();
  c = in.nextInt();
  large = (a>b)?((a>c)?a:c):((b>c)?b:c);
}

void display()

{
System.out.println("Largest No ==> " + large);
}
}
class main{
public static void main(String args[]){
LargestThreeNo l =new LargestThreeNo();
l.display();
}

}

Output



Enter a b and c Number ==> 10 20 30

Largest No ==> 30



Java Largest Two Number Example

Program that will find Largest Number from given Two Numbers

Method 1


class LargeTwoNumber{

public static void main(String args[])
{
int a=10,b=20;

if(a>b){

System.out.println("Largest Two Number is= "+a);
}else{

System.out.println("Largest Two Number is ="+b);
}
}
}

Output

Largest Two Number is = 20


Method 2

class LargeTwoNumber{

public static void main(String args[])
{
int a=10,b=20;

int c = (a>b)?a:b;

System.out.print("Largest Two Number is = " + c);
}
}


Output



Largest Two Number is = 20

Method 3


import java.util.*;
class LargeTwoNumber{

public static void main(String args[])
{
int a,b,c;

Scanner in = new Scanner(System.in);
System.out.print("Enter a and b Number ==> ");
a = in.nextInt();
b = in.nextInt();
c = (a>b)?a:b;
System.out.print("Largest Two Number is = " + c);
}
}


Output



Enter a and b Number ==> 10 20
Largest Two Number is = 20

Method 4

import java.util.*;
class LargeTwoNumber{

public static void main(String args[])
{
int a,b,c;

a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);

c = (a>b)?a:b;

System.out.print("Largest Two Number is = " + c);
}
}

Output




Method 5

import java.util.*;
class LargeTwoNumber{
int a,b,c;
LargeTwoNumber()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a and b Number ==> ");
a = in.nextInt();
b = in.nextInt();
c = (a>b)?a:b;
}

void display()
{

System.out.println("Largest Two Number ==> " + c);
}
}
class main{
public static void main(String args[]){

LargeTwoNumber l = new LargeTwoNumber();
l.display();
}
}



Output



Enter a and b Number ==> 10 20
Largest Two Number is = 20

Java Factorial Program Example

Program that will be display Factorial  Number

Method 1:


class factorial
{
public static void main(String args[])
{
int fact=1,n=5;

for(int i=1;i<=n;i++)
{
fact = fact*i;
}
System.out.println("Factorial  "+n+" Number Is  = "+fact);
}
}

Output

Factorial 5 Number Is = 120


Method 2:

class factorial
{
public static void main(String args[])
{
int fact=1,n=5;
int i=1;

while(i<=n)
{
fact=fact*i;
i++;
}
System.out.println("Factorial "+n+" Number Is = "+fact);
}
}


Output 

Factorial 5 Number Is = 120


Method 3

class factorial
{
public static void main(String args[])
{
int fact=1,n=5;
int i=1;

do
{
fact = fact*i;
i++;
}while(i<=n);
System.out.println("Factorial "+n+" Number Is = "+fact);
}
}



Output 

Factorial 5 Number Is = 120


Method 4


import java.util.*;
class factorial
{
public static void main(String args[])
{
int fact=1,n;
int i=1;

Scanner in = new Scanner(System.in);
System.out.print("Enter Factorial Number ==> ");
n = in.nextInt();
for(i=1;i<=n;i++)
{
fact = fact*i;
}
System.out.println("Factorial "+n+" Number Is = "+fact);
}
}




Output 

Enter Factorial Number ==> 5
Factorial 5 Number Is = 120








Java Area of Circle Program Example

Write a Program to Calculate Area of Circle(area = PI*r*r)

Method 1:


class AreaOfCircle{

public static void main(String args[])
{
float pi = 3.14f;
int r = 5;
float area;

area = (pi*r*r);

System.out.println("Area of Circle = " + area);
}
}

Output

Area of Circle = 78.5


Method 2:


import java.util.*;

class AreaOfCircle{



public static void main(String args[])
{
float pi = 3.14f;
int r;
float area;

Scanner in = new Scanner(System.in);

System.out.print("Enter r Number ==> ");
r = in.nextInt();
area = (pi*r*r);
System.out.println("Area of Circle = " + area);
}
}



OutPut

Enter r Number ==> 5

Area of Circle = 78.5