-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathstrongnumber.go
82 lines (57 loc) · 1.48 KB
/
strongnumber.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* This is Program in go language to check whether a given number is
Strong number or not. Strong number is a special number whose
sum of factorial of digits is equal to the original number.
Eg: 145 is strong number. Since, 1! + 4! + 5! = 145
*/
package main
import (
"fmt"
)
//global variable
var a int
//This function will return us the factorial of a given number
func factorial(n int)int {
if(n == 0){
return 1
}else{
return n*factorial(n-1)
}
}
//This function tells us if the given number is a strong number or not
func strongnumber() {
var number int
number = a
var sum int
sum = 0
var digits int
/* we calculate the sum of the factorials of individual digits
of the number given*/
for number!=0 {
digits = number%10
sum += factorial(digits)
number /= 10
}
// checking if the sum is same as orginal number
if (sum == a){
fmt.Print("The given number is a strong number!")
}else{
fmt.Print("The given number is not a strong number!")
}
}
//driver function
func main() {
fmt.Print("Enter the number we need to check :")
// Taking the input for the number we need to check
fmt.Scan(&a)
//calling the strongnumber function
strongnumber()
}
/*
Simple I/O :
a) Is a strong number :
Enter the number we need to check :40585
The given number is a strong number!
b) Is not a strong number :
Enter the number we need to check :45
The given number is not a strong number!
*/