Welcome to our website

Here we Provide C, C++, Java, Oracle and More Tutorials for online searchings for Tutorials.
  • Fully Responsive

    • We Response with in 24 hours regarding any doubts in our Tutorials

Factorial Program in Java with examples.

Factorial Program in Java 

Factorial:  Factorials are products of every whole number from 1 to n Factorials are introduced in algebra. The value of 0! is 1, according to the convention for an empty product. The notation n! was introduced by the French mathematician Christian Kramp in 1808.

Formula:

n! = n(n-1)(n-2)(n-3)(2)(1)

 (or)

n! = n.(n-1)!.

Example:

4! =  4*(4-1)*(4-2)*(4-3)*(4-4)
4! =  4*3*2*1
4! = 24

Factorial program:

class Fact
{
 public static void main(String args[])
 {
  int fact = 1,j = 6;
  for(int i = 1;i<=j;i++)
  {
   fact = fact * i ;
  }
 System.out.println("Fact of 6 = "+fact);
 }
}

Output:

Fact of 6 = 720

Factorial program using user entered value:

import java.util.Scanner;
class Fact1
{               
 public static void main(String args[])
 {
  Scanner s = new Scanner(System.in); 
  int fact = 1;
  System.out.print("Enter the value of j = ");
  int j = s.nextInt();
  for(int i = 1;i<=j;i++)
  {
   fact = fact * i ;
  }
 System.out.println("Fact of " + j + " = " +fact);
 }
}

Output:

Enter the value of j = 10
Fact of 10 = 3628800

Factorial using recursion:

class FactorialRecursion
  int factorial(int n){   
  if (n == 0)   
    return 1;   
  else   
    return(n * factorial(n-1));   
 }             
 public static void main(String args[])
 { 
  int i,fact = 1; 
  FactorialRecursion f1 = new FactorialRecursion();
  fact = f1.factorial(5);  
  System.out.println("Factorial of 5 = "+fact);   
 } 

Output:

Factorial of 5 = 120

Recursion program using user entered value:

import java.util.Scanner;
class FactorialRecursion1
  int factorial(int n){   
  if (n == 0)   
    return 1;   
  else   
    return(n * factorial(n-1));   
 }   
 public static void main(String args[])
 { 
  int i,fact = 1; 
  Scanner s = new Scanner(System.in);
  FactorialRecursion1 f1 = new FactorialRecursion1();
  System.out.print("Enter the value of J = ");
  int j = s.nextInt();
  fact = f1.factorial(j);  
  System.out.println("Factorial of J = "+fact);   
 } 

output:

Enter the value of J = 6
Factorial of J = 720

Share:

Java if Else with Examples and programs

Java if statement: The if statement is decision-making statement and is used to control the flow of executions of statements. If is the two-way decision statement and is used in conjunction with a Test expression.
Declaration of if statement:
if(test expression)

Explanation: In the above declaration the if statement allows the computer to evaluate the expression first and then depending on whether the value of the expression (means condition) is “True” or “False” it transfers the control to next statement. The program has to follow two paths one for the true condition another for the false condition.
SIMPLE IF STATEMENT
If (test expression)
          {
            statement-block;
           }
           statement-n;

The statement block may be a single statement or a group of statements. If the test expression is true, the statement block will be executed otherwise the statement-block will be skipped and the execution will jump to the statement-n;

Examples using Integers:

class IfExample
 {
  public static void main(String[] args)
  {
    int i = 6, j = 7;
    if(i<j)
    {
 System.out.println("J is Greater then I");
    }
  }
}

Output: 
J is Greater then I

Example 2:
class IfExample1
{
public static void main(String[] args)
{
 if(8>6)
 {
 System.out.println(" Welcome to TutorialsWeb ");
 }
}
}

Output:
Welcome to TutorialsWeb

else: It is a keyword using this keyword we can create the alternate block for “if” condition using “else” means always optional, it is recommended to use. When we have an alternate block of “if” condition. When we are working with “if” and “else” only one block will be executed and when the if a condition is false then only else part will be executed.

if …else Statement:
The if …else statement is an extension of the simple if statement. The Syntax of if …else statement is below:


if(test expression)
{
 True-block statement(s);
}
else
{
 False-block statement(s);
}
statement(x);

Explanation: If the Test expression is true, then the true-block statement(s), immediately following the if statements are executed. Or the False-block Statements are executed. In another case, either true-block (or) False-block will be executed, not the both. Then the control is transferred subsequently to the statement-x;
Program:
import java.util. *;
class IfElseExample
{
public static void main(String[] args)
{
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter I value");
 int i = s1.nextInt();
 System.out.println("Enter J value");
 int j = s1.nextInt();
 if(i > j)
 {
 System.out.println("I is Greater then J");
 }
 else
 {
 System.out.println("J is Greater then I");
 }
System.out.println("Out Of If Else Loop");
}
}

Output:
Enter I value
15
Enter J value
14
I is Greater then J
Out Of If Else Loop

Explanation:
1)    Two integers “I” and “J” are declared. Input values are received for both “I” and “J” using Scanner.
2)     In the above program if else condition statement is used to check whether “I” is greater than “J”.
3)    If “I” is greater than “J”, the Print message “I is Greater then J” is Printed.
4)    If “J” is greater than “I”, the else part will be executed. The Print message “J is Greater then I” is Printed.
5)    After completing the else part the statement-x will be executed.

Nested If…else Statements in Java: When a series of decision is involved then we have to use more than one if…else statements in the nested form shown below.

Explanation: In the above logic of execution if the Test condition-1 is True, it continues to the Test condition-2. If the Test condition-2 is True, the statement-1 will be evaluated other-wise else statement-2 evaluated. If the condition-1 is false, the statement-3 will be executed, and then the control is transferred to the statement-x;

Program:                                                         
import java.util.*;
class NestedIfelseExample
{
 public static void main(String[] args)
 {
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter the Number");
 int n = s1.nextInt();
 if(n > 0)
 {
 if(n == 0)
 {
  System.out.println("It is Zero");
 }
 else
 {
 System.out.println("Number is greater than Zero");
 }
 }
 else
 {
 System.out.println("Number is less than Zero");
 }
 }
}

Output:
Enter the Number
12
Number is greater than Zero

The Else If Ladder: Another way of putting ifs together when multipath decisions are involved. A multipath decision of ifs in which the statement associated with each else is an if.  The syntax of Else If Ladder is shown below:
Explanation: In the above Else If Ladder the conditions are evaluated from the top of the ladder to downwards. When the condition found to be True, the statement associated with it is executed and the control is transferred to the statement-x skipping the rest of the ladder. When all the n conditions become False, then the final else with default statement will be executed.
Program:

import java.util.*;
class ElseIfLadderExample
{
 public static void main(String[] args)
 {
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter the Marks");
 int m = s1.nextInt();
 if(m<50)
    { 
     System.out.println("fail"); 
    } 
    else if(m >= 50 && m < 60)
    { 
     System.out.println("D grade"); 
    } 
    else if(m >= 60 && m < 70)
    { 
     System.out.println("C grade"); 
    } 
    else if(m >=70 && m < 80)
    { 
     System.out.println("B grade"); 
    } 
    else if(m >= 80 && m < 90)
    { 
     System.out.println("A grade"); 
    }
    else if(m >=90 && m < 100)
    { 
     System.out.println("A+ grade"); 
    }
    else
    { 
     System.out.println("Invalid!"); 
    } 
  }
}

Output:
Enter the Marks
66
C grade

Share:

Polymorphism in Java with example

Polymorphism in Java

What is Polymorphism in Java?


Defining the multiple functions (or) methods with the same name associated with the same object is called as polymorphism.

Types of polymorphism: There are two types of polymorphism. They are:

1) Static polymorphism (or) Compile time polymorphism.

2) Dynamic polymorphism (or) Run-time polymorphism. 


Static polymorphism (or) Compile time polymorphism: Defining multiple functions (or) methods with the same name within the same class by changing the data types of the parameters is called as Static polymorphism (or) Compile time polymorphism.

Example using program:

class StaticPoly
{
 void fun1()
{
 System.out.println("Fun1() of Static Polymorphism");
}
void fun1(int x)
{
 System.out.println("Fun1(int x) of Static Polymorphism");
}
void fun1(int x, int y)
{
 System.out.println("Fun1(int x, int y) of Static Polymorphism");
}
public static void main(String[] args)
{
 StaticPoly p1 = new StaticPoly();
 p1.fun1();
 p1.fun1(1);
 p1.fun1(1,2);
}
}

Output:

Fun1() of Static Polymorphism.
Fun1(int x) of Static Polymorphism.
Fun1(int x, int y) of Static Polymorphism.


Q) What happens in static polymorphism at the time of compilation?

A) In static polymorphism out of multiple methods with the same name the method which has to get executed would be decided at the time of compilation itself. So we call the static polymorphism as compile time polymorphism.


Rules-based on which the Polymorphism work in Java:

1) Whenever a function call is made by passing a value has an argument then JVM searches for the method defined to accept the same data type of the value that is passed.

2) There is a method defined to accept the same data type is existing JVM would not be considered any other method whose parameter value matches to the passed argument. The first priority given to the method defined to accept the same data type of the value we pass has an argument.
3) When a function call is made by passing value has an argument & if there is no method is defined to accept the data type value passed then it may reach to the alternate method.
4) The alternate method is a method where parameter value where we passed matches the argument but the data type of the parameter and the type of the value what we pass has argument would not be same.

Example:

class Poly
{
 void funA(float f)
 {
  System.out.println(" F = " +f);
 }
 void funB(boolean b)
 {
  System.out.println(" B = " +b);
 }
public static void main(String[] args)
 {
  Poly p1 = new Poly();
  p1.funA(10);
 }
}

Output: F = 10.0
Explanation: In the above program the statement “p1.funA(10)” the value “10” is passed to the funA(). At calling position first the value “10” with an integer is not declared so it passed to the void funA(float f). Where float is alternate method is acceptable to the Integer value we passed.
Dynamic polymorphism (or) Run-time polymorphism: Defining functions (or) methods in the subclass with the same name and the same signature of the superclass function is called as method overloading (or) Dynamic polymorphism.
Example:
Super class program:
class A
{
 int i,j;
 void funA()
 {
  System.out.println("FunA() of A");
 }
}
Sub class program:
class D extends A
{
 int i,x;
 void funA()
{
 System.out.println("FunA() of class B");
}
public static void main(String[] args)
{
 D b1 = new D();
 b1.funA();
}
}

Output:

FunA() of class B.

Explanation: Whenever we are over-riding a superclass method in sub-class the signature and the prototype of the sub-class method must be the same has super-class method without which where a compilation error would occur. Whatever the rules permitted in static polymorphism is also applicable in method over-riding.
Note: In Dynamic polymorphism, multiple methods with the same name & with the same data types of the parameters has to get executed would be decided dynamically at the runtime based conditions.

Share:

Search This Blog

Powered by Blogger.

Blog Archive

Factorial Program in Java with examples.

Factorial Program in Java  Factorial:   Factorials are products of every  whole number  from 1 to n  Factorials   are introduced in  al...

Unordered List

Pages