Monday, 4 August 2014

Java Code to find the Area of Circle

Area of a Circle Using Java

A Java program to accept the radius of a Circle from user and calculate the area.


//A small java program to calculate the area of a circle
import java.util.Scanner;

public class JavaCircleArea {

    public static void main(String args[])
   {
      int radius;
      System.out.println("Enter the Radius of the Circle ");

      Scanner in = new Scanner(System.in);
      radius = in.nextInt(); 

      double areaCircle = Math.PI*radius*radius;
      System.out.println("Area of the Circle = "+areaCircle);
   }
    
}


Output:


Enter the Radius of the Circle 
3
Area of the Circle = 28.274333882308138

Wednesday, 2 July 2014

Odd or Even Program in Java

Odd or Even Check using Java Code

A Java program to accept a number from keyboard and check whether it is Odd or Even


//A small java program to check the accepted number is Odd or Even
import java.util.Scanner;

public class OddEven {

    public static void main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();

      if ( x % 2 == 0 )
         System.out.println("You entered an even number.");
      else
         System.out.println("You entered an odd number.");
   }
    
}

Output:


Enter an integer to check if it is odd or even 
5
You entered an odd number.

Saturday, 17 May 2014

JAVA Program to Find the Factorial of a Number

Factorial of a number USING JAVA

A program to accept a number from keyboard and print the Factorial.


//A small java program to calculate the Factorial
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{           final int fact;
             System.out.println("Enter a number to find the Factorial");
             Scanner in = new Scanner(System.in);
             fact=in.nextInt();
             System.out.println( "Factorial of " + fact + " is " + factorial(fact));
}

public static int factorial(int n)
{ 
             int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}

}

Output:


Enter a number to find the Factorial
5
Factorial of 5 is 120

Saturday, 10 May 2014

Lambda in Java 8

Lambda Expression in Java


Quick notes on How to use the Lambda feature in new Java


Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Java code can be improved with the inclusion of lambda expressions.

Syntax of Lambda Expression



(argument) -> (body)



A lambda expression is composed of three parts.


Argument List Arrow Token          Body
(int x, int y)             ->                     x + y

Simple Example for Lambda Expression


(int x, int y) -> x + y

() -> 42

(String s) -> { System.out.println(s); }


           The first expression takes two integer arguments, named x and y, and uses the expression form to return x+y. The second expression takes no arguments and uses the expression form to return an integer 42. The third expression takes a string and uses the block form to print the string to the console, and returns nothing.

Friday, 18 April 2014

Multiplication Table using Java

Simple Multiplication Table in Java

Java Code to print Multiplication Table


//A small java program to print the Multiplication Table


import java.util.Scanner;
class MultiplicationTable
{
   public static void main(String args[])
   {
      int n, c;
      System.out.println("Enter an integer to print it's multiplication table");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      System.out.println("Multiplication table of "+n+" is :-");
      for ( c = 1 ; c <= 10 ; c++ )
         System.out.println(n+"*"+c+" = "+(n*c));
   }
}


Output:

        The output of the program is as shown below

Mutiplication Table