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
Golang Program to Print the Numbers in a Range (1, upper) without Using any Loops
Steps
- Define a recursive function.
- Define a base case for that function that the number should be greater than zero.
- If the number is greater than 0, call the function again with the argument as the number minus 1.
- Print the number.
| Enter the upper limit: 5 1 2 3 4 5 |
Enter the upper limit: 15 1 2 . . 15 |
Example
package main
import (
"fmt"
)
func printNo(number int){
if number >= 1{
printNo(number-1)
fmt.Println(number)
}
}
func main(){
var upper int
fmt.Print("Enter the upper limit: ")
fmt.Scanf("%d", &upper)
printNo(upper)
}
Output
Enter the upper limit: 5 1 2 3 4 5
Advertisements