SlideShare a Scribd company logo
import java.util.Scanner;
public class Factorial
{
// method using recursion to find factorial of number
public static int factorial(int input)
{
// factorial of 0 is 1
if (input == 0)
{
return 1;
}
// else recursively call the function
else
{
return input * factorial(input - 1);
}
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int input;
System.out.println("Enter a number: ");
input = scan.nextInt();
// store result in answer
int answer = factorial(input);
System.out.println("The factorial of " + input + " is " + answer);
}
}
Solution
import java.util.Scanner;
public class Factorial
{
// method using recursion to find factorial of number
public static int factorial(int input)
{
// factorial of 0 is 1
if (input == 0)
{
return 1;
}
// else recursively call the function
else
{
return input * factorial(input - 1);
}
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int input;
System.out.println("Enter a number: ");
input = scan.nextInt();
// store result in answer
int answer = factorial(input);
System.out.println("The factorial of " + input + " is " + answer);
}
}

More Related Content

PPTX
Lecture09 recursion
PPT
M251_Meeting 9 (Recursion_AdvancedJava).ppt
PPTX
Recursion
PDF
PyOhio Recursion Slides
PPTX
Recursion for GCE AS Computing
PDF
#include stdio.h #include string.h Function swaps to po.pdf
Lecture09 recursion
M251_Meeting 9 (Recursion_AdvancedJava).ppt
Recursion
PyOhio Recursion Slides
Recursion for GCE AS Computing
#include stdio.h #include string.h Function swaps to po.pdf

Similar to import java.util.Scanner;public class Factorial { method usi.pdf (20)

PDF
Recursion examples
PPTX
Programming in c
PDF
81818088 isc-class-xii-computer-science-project-java-programs
PDF
Method overloading, recursion, passing and returning objects from method, new...
PPT
JavaYDL20
PPTX
Simple program recursion problem solving.pptx
PDF
recursion1
PPTX
Recursion
PDF
Please follow the code and comments for description a)CODE #.pdf
PDF
Introduction to Recursion (Python)
PDF
062636636366363773737373733+73737733+7.pdf
PDF
recursion2
PDF
Java Unit 1 Project
PDF
Recursion - Computer Algorithms
PDF
Anjalisoorej imca133 assignment
PDF
Limitless and recursion-free recursion limits!
PPT
Algorithms with-java-1.0
PPTX
Globalandlocal
DOCX
Home work 3
PDF
Recursion
Recursion examples
Programming in c
81818088 isc-class-xii-computer-science-project-java-programs
Method overloading, recursion, passing and returning objects from method, new...
JavaYDL20
Simple program recursion problem solving.pptx
recursion1
Recursion
Please follow the code and comments for description a)CODE #.pdf
Introduction to Recursion (Python)
062636636366363773737373733+73737733+7.pdf
recursion2
Java Unit 1 Project
Recursion - Computer Algorithms
Anjalisoorej imca133 assignment
Limitless and recursion-free recursion limits!
Algorithms with-java-1.0
Globalandlocal
Home work 3
Recursion
Ad

More from aptind (20)

PDF
ssian chemist, Dmitri Mendeleev is often consider.pdf
PDF
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
PDF
               CLOUD COMPUTING -----------------------------------.pdf
PDF
You cannot.SolutionYou cannot..pdf
PDF
ViVi is universally available on Unix systems. It has been around.pdf
PDF
Waterfall methodThe model consists of various phases based on the.pdf
PDF
Hi, I am unable to understand the terminology in .pdf
PDF
The main function of cerebellum is to control the motor movements. H.pdf
PDF
Starting with Main.java, where I tested everythingimport College..pdf
PDF
solution of question no.6inputPresent stateNext stateoutput.pdf
PDF
Sexual reproduction has played the most crucial role in evolution of.pdf
PDF
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
PDF
And is option DIf variable interest rate decrease , asset value wi.pdf
PDF
Hi please find my code.import java.util.HashMap;import java.util.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
PDF
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
PDF
As we understand, when soil particles binds to each other more stron.pdf
PDF
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
PDF
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
PDF
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
               CLOUD COMPUTING -----------------------------------.pdf
You cannot.SolutionYou cannot..pdf
ViVi is universally available on Unix systems. It has been around.pdf
Waterfall methodThe model consists of various phases based on the.pdf
Hi, I am unable to understand the terminology in .pdf
The main function of cerebellum is to control the motor movements. H.pdf
Starting with Main.java, where I tested everythingimport College..pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdf
Given below is the code for the question. Since the test files (ment.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
As we understand, when soil particles binds to each other more stron.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
Ad

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PPTX
Institutional Correction lecture only . . .
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
01-Introduction-to-Information-Management.pdf
Sports Quiz easy sports quiz sports quiz
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Institutional Correction lecture only . . .
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
Module 4: Burden of Disease Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
01-Introduction-to-Information-Management.pdf

import java.util.Scanner;public class Factorial { method usi.pdf

  • 1. import java.util.Scanner; public class Factorial { // method using recursion to find factorial of number public static int factorial(int input) { // factorial of 0 is 1 if (input == 0) { return 1; } // else recursively call the function else { return input * factorial(input - 1); } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int input; System.out.println("Enter a number: "); input = scan.nextInt(); // store result in answer int answer = factorial(input); System.out.println("The factorial of " + input + " is " + answer); } } Solution import java.util.Scanner; public class Factorial {
  • 2. // method using recursion to find factorial of number public static int factorial(int input) { // factorial of 0 is 1 if (input == 0) { return 1; } // else recursively call the function else { return input * factorial(input - 1); } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int input; System.out.println("Enter a number: "); input = scan.nextInt(); // store result in answer int answer = factorial(input); System.out.println("The factorial of " + input + " is " + answer); } }