Go Language - Program to Check Krishnamurthy Number

Hello everyone, today we will create a Go program to check if the given number is an Krishnamurthy number or not. Krishnamurthy number is also referred to as a Strong number.
A number is said to be Krishnamurthy if the factorial sum of all its digits is equal to that number.


For example,

Number = 40585
  = 4! + 0! + 5! + 8! + 5!
  = 1 * 2 * 3 * 4 + 1 + 1 * 2 * 3 * 4 * 5 + 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 + 1 * 2 * 3 * 4 * 5
  = 24 + 1 + 120 + 40320 + 120
  = 40585


Example 1: Go program that checks if the given number is a Krishnamurthy number or not.

/*Go program to check whether the given
* number is a Krishnamurthy number or not.
*/
package main

import "fmt"

// Main function
func main() {

//sample 1
number1 := 40585
KrishnamurthyNumber(number1)

//sample 2
number2 := 13789
KrishnamurthyNumber(number2)

//sample 3
number3 := 1009
KrishnamurthyNumber(number3)

}

func KrishnamurthyNumber(number int) {

//initialize sum to 0
sum := 0

//create a copy of the number
tempNumber := number

//perform operation until tempNumber will not equal to 0
for tempNumber != 0 {
/* calculate the factorial of the last digit
of the tempNumber and then add it to the sum
*/
sum = sum + Factorial(tempNumber%10)

// replace the value of tempNumber by tempNumber/10
tempNumber = tempNumber / 10
}
//If sum == number equal, number is krishnamurthy number
if sum == number {
fmt.Println(number, "is Krishnamurthy Number")
} else {
fmt.Println(number, "is not a Krishnamurthy Number")
}
}

//Method to calculate the factorial of the number
func Factorial(number int) int {
fact := 1
for i := 1; i <= number; i++ {
fact *= i
}
return fact
}

Output:




Example 2: Go program that checks if the number from the standard input is a Krishnamurthy number or not.

/*Go program to check whether the number from the standard input
* is a Krishnamurthy number or not.
*/
package main

import "fmt"

// Main function
func main() {

var number int
fmt.Println("Enter an integer value : ")

_, err := fmt.Scanf("%d", &number)

if err != nil {
fmt.Println(err)
}

KrishnamurthyNumber(number)

}

func KrishnamurthyNumber(number int) {

//initialize sum to 0
sum := 0

//create a copy of the number
tempNumber := number

//perform operation until tempNumber will not equal to 0
for tempNumber != 0 {
/* calculate the factorial of the last digit
of the tempNumber and then add it to the sum
*/
sum = sum + Factorial(tempNumber%10)

// replace the value of tempNumber by tempNumber/10
tempNumber = tempNumber / 10
}

//If sum == number equal, number is krishnamurthy number
if sum == number {
fmt.Println(number, "is Krishnamurthy Number")
} else {
fmt.Println(number, "is not a Krishnamurthy Number")
}
}

//Method to calculate the factorial of the number
func Factorial(number int) int {
fact := 1
for i := 1; i <= number; i++ {
fact *= i
}
return fact
}

Output:

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

Registration and Login with Spring Boot + Spring Security + Thymeleaf

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download - REST API Example

Node JS mini projects with source code - free download

ReactJS, Spring Boot JWT Authentication Example