Golang Program to Display Factors of a Number


In this tutorial program, we will learn how to display the factors of a given number in the Go programming language.

A factor of a number is defined as the algebraic expression that divides any given number evenly, with its remainder being zero. All composite numbers will have more than two factors, that include 1 and the number itself also.

For example: by multiplying 3 and 7, we get 21. We say 3 and 7 are factors of 21.

Below is a demonstration of the same −

Input

Suppose our input is = 15

Output

The factors of 15 are: 1 3 5 15

Syntax

For loop syntax:
for initialization; condition; update {
   statement(s)
}

Example-1: Shows How to Display Factors of a Number in Golang Program Inside The Function Main()

Given below are the steps which are used in this Go program code.

Algorithm

  • STEP 1 − Import the package fmt

  • STEP 2 − Start function main()

  • STEP 3 − Declare and initialize the variables

  • STEP 4 − Use for loop to display the factors as num%i == 0

  • STEP 5 − The for loop iterated from i=0 to i<=num

  • STEP 6 − Print the result using fmt.Println()

Example

package main
// fmt package provides the function to print anything
import "fmt"

// start the main() function
func main() {
   // Declare and initialize the variables
   var num = 15
   var i int
    
   fmt.Println("The factors of the number", num, " are = ")
   // using for loop the condition is evaluated. 
   // If the condition is true, the body of the for loop is executed
   for i = 1; i <= num; i++ {
      if num%i == 0 {
         fmt.Println(i)
      }
   } // Print the result
}

Output

The factors of the number 15 are = 
1
3
5
15

Description of The Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt.

  • Now start the function main().

  • Declare and initialize the integer variables num and i.

  • Using the for loop the condition is evaluated.

  • In the program, the ‘for’ loop is iterated until i is false. Variable i is the indexing variable in the for-loop. In each step of iteration, whether num is exactly divisible by i is checked. It is the condition for i to be a factor of num. Then the value of i is incremented by 1.

  • Finally we print the result using an in-built function fmt.Println() and the factors of a number are displayed on the screen.

Example-2: Shows How to Display Factors of a Number in The Golang Program in Two Separate Functions

Given below are the steps which are used in the Go program code.

Algorithm

  • STEP 1 − Import the package fmt.

  • STEP 2 − Create a function factor() outside the main() function.

  • STEP 3 − Declare the variable i.

  • STEP 4 − Use of for loop with condition.

  • STEP 5 − Start the function main().

  • STEP 6 − Call the function factor() to find the factor of a given number.

  • STEP 7 − Print the result using fmt.Println().

Example

package main
// fmt package provides the function to print anything
import "fmt"

// create a function factor() to find the factors of a number
func factor(a int) int {

   // declare and initialize the variable
   var i int
   // using for loop the condition is evaluated
   // If the condition is true, the body of the for loop is executed
   for i = 1; i <= a; i++ {
      if a%i == 0 {
         fmt.Println(i)
      }
   }
return 0
}
// Start the main() function
func main() {
   fmt.Println("The factors of the number 6 are")

   // calling the function factor() to find factor of a given number
   factor(6) 
   
   // Print the result
}

Output

The factors of the number are
1
2
3
6

Description of The Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt.

  • We create a function factor() outside the main() function to find the factors of a given number.

  • We declare the integer variables a and i, variable i is the indexing variable in the for-loop.

  • In the program using for loop the condition is evaluated. The ‘for’ loop is iterated until i is false. In each step of iteration, whether ‘a’ is exactly divisible by i is checked. It is the condition for i to be a factor of ‘a’. Then the value of i is incremented by 1.

  • Next we start the function main().

  • Now we call the function factor() to find factor of a given number.

  • The result is printed using an in-built function fmt.Println() and the factors of a number are displayed on the screen.

Conclusion

In the above two examples, we have successfully compiled and executed the Golang code to display factors of a number.

In the above programming codes, the ‘for’ loop is used to repeat a block of code until the specified condition is met. We are using an in-built function fmt println() function for printing the result on the output screen. In the above examples, we have shown how to implement looping statements in the Go Programming language.

Updated on: 29-Dec-2022

469 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements