Sunday, January 27, 2019

Factorial of a given number using recursion.
public lass FactorialRecursion{
        public static void main(String[] ar){
             System.out.print("Enter a number : ");
             int n;
             Scanner sc=new Scanner(System.in);
             n=sc.nextInt();
             double ans=fact(n);
             System.out.println("The factorial of "+n+" = "+ans);
             sc.close();
        }

        public static double fact(int num){
               if(num==1){
                     return(1);
               }else{
                     return(num*(fact(num-1))); //Calling itself again with a different value.
                 }
        }
}

O/P: Enter a number : 5
The factorial of 5 = 120.0
with out recursion: 
public class Factorial {
       public static void main(String[] args) throws NumberFormatException, IOException  {
               BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Enter  the Number");
               int no=Integer.parseInt(br.readLine());
               int fact=1;
               for(int i=1;i<=no;i++){
                     fact=fact*i;
               }
               System.out.println("Factorial is: "+ fact);
       }


}

Related Posts:

  • @FindBy, @FindBys, @FindAll, @CacheLookup - Selenium API All are used to mark a field in a page object. @FindBy: It is an alternative mechanism for locating the element or a list of elements. Used in conjunction with PageFactory this allows users to quickly and easily cre… Read More
  • Palindrome Number Program in Java Palindrome Number: A Palindrome number is a number that remains the same when its digits are reversed. We get total 90 numbers between 100 and 1000. public class PalindromeNumber { public static void main(String[] a… Read More
  • pom.xml in Maven A Project Object Model or POM is the fundamental unit of work in Maven. It contains the project configuration details used by Maven. Some of the configuration that can be specified in the POM are the project dependencies, th… Read More
  • 2nd highest salary of an employee? MySQL: Sub queries in SQL are great tool for this kind of scenario, here we first select maximum salary and then another maximum excluding result of subquery mysql> SELECT max(salary) FROM Employee WHERE salary NOT IN (S… Read More
  • Explain public static void main(String[] args){} public static void main(String[] args){} or public static void main(String... args){}: Java main method is the entry point of any java program. Its syntax is always "public static void main(String[] args)". You can only… Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts