
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Factorial of a Number Using Recursion in Go
Examples
Factorial of 5 = 5*4*3*2*1 = 120
Factorial of 10 = 10*9*8*7*6*5*4*3*2*1 =
Approach to solve this problem
- Step 1: Define a function that accepts a number (greater than 0), type is int.
- Step 2: If the number is 1, then return 1.
- Step 3: Otherwise, return num*function(num-1).
Program
package main import "fmt" func factorial(num int) int{ if num == 1 || num == 0{ return num } return num * factorial(num-1) } func main(){ fmt.Println(factorial(3)) fmt.Println(factorial(4)) fmt.Println(factorial(5)) }
Output
6 24 120
Advertisements