Skip to main content

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA :

Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1) and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.

   Now To represent properties and behaviour in java , there are some standard naming conventions we should follow.

Class name should always starts with Uppercase letter like

class Student {
//Code to be executed
}

Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case. like

class Student {
int studentId ;
String studentName ;
//Code to be executed
}

Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case. like

class Student {
int studentId ;
String studentName ;

public int getStudentId(){
return this. studentId;
}
public String getStudentName(){
return this. studentName;
}
//Code to be executed
}

Next is constant name and all constant should be declared in CAPITAL LETTERS only like

class Student {

public static int MIN_AGE = 18 ;
public static int MAX_AGE = 60 ;

//Code to be executed
}

Now we have learned about  java properties and behaviour , variable declaration , operators and loops along with naming conventions of class, methods and variable in our all 5 free java session as below 

  1. BASIC OVERVIEW OF JAVA (SESSION 1)
  2. PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
  3. JAVA OPERATORS (SESSION 3)
  4. LOOPS IN JAVA (SESSION 4)
  5. NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)

Now we can write our very first java program. Yeah there are a lot of concepts like interface, Abstract class , method overloading and overriding and  keywords like this, final etc and Collection Framework is pending , which we will learn once i will start OOPS concept series . But trust me writing simple Java program which can be asked in interview for Fresher to 5 years experience guys can be done by this 5 session knowledge . Just little use of your brain and you can do it.

Let's start with very simple program to find out the Factorial of any Number and below is the code .


public class Factorial {

public static void main(String[] args) {
int inputForFact = 5;
int factorial= 1;

while (inputForFact > 0) {
if (inputForFact == 1) {
System.out.println(factorial);
break;
} else {
factorial *= inputForFact;
inputForFact--;
}
}
}
}


In above program I have calculated factorial of a fix number i.e 5. So i have declared 2 properties as explained in my previous post BASIC OVERVIEW SESSION 1 .

One is the int number to be calculated and another is to hold the final result of factorial initiated with a int value of 1 , its a primitive type explained in previous post JAVA PRIMITIVE TYPES SESSION 2

Next I have used a for loop with a condition which is checking if input number(inputForFact) is greater than 0 or not, if it is true it's going inside loop and executing the code mentioned inside loop brackets as explained in my previous post JAVA LOOPS SESSION 4

Inside loop i have a decisional making condition to check if input number(inputForFact) is equal to 1 or not if its 1 print the final value(factorial) and break the loop, if not then it will go to else condition and multiplying the input number(inputForFact)) with current factorial value(factorial: initial value is 1) and again back storing into same factorial value(factorial) and decrementing the input number(inputForFact) by 1 , I already explained this 2 operator *= and ++ in my previous post JAVA OPERATORS SESSION 3 .

  So in this case input number(inputForFact) will be decremented by one  4 times as number starts with 5 so after 4 times decrement  it will be 1 then our IF condition will get satisfied and loop will be break after printing Final value(factorial) of Factorial.

That's all about calculating factorial and whatever we have used in calculating factorial of a number, everything has been covered in my last 5 session post , nothing new, so just go through all the session if you have any doubts.


OOPS CONCEPT :
  1. OOPS CONCEPTS IN JAVA
  2. INHERITANCE IN JAVA
  3. ABSTRACTION IN JAVA
  4. POLYMORPHISM IN JAVA

Comments

Post a Comment

Popular posts from this blog

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Selection sort Implementation

  Selection sort  : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). class SelectionSort { public static void sort ( int [] array) { int n = array. length ; for ( int i = 0 ; i < n - 1 ; i++) { int min = i ; for ( int j = i + 1 ; j < n ; j++) { if (array[j] < array[min]) { min = j ; } } swap (array , i , min) ; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ; array[i] = array[j] ; array[j] = temp ; } private static void print ( int [] array) { System. out .print( "[" ) ; for ( int i = 0 ; i < array. lengt...