Question

In: Computer Science

Code in Golang: Write a boolean function called share with one integer parameter which determines whether...

Code in Golang:

Write a boolean function called share with one integer parameter which determines whether the sum of the digits in the inputted integer and the inputted integer share a factor other than one. If so, return true, otherwise return false. Examples: 121 sum is 4, so the function returns false 242 sum is 8, so the function returns true

Solutions

Expert Solution

Please refer to the screenshot of the code to understand the indentation of the code and output

Code

package main
import ("fmt")
func share(number int) bool { // function share declaration
var status bool = false // set the return status to false at first
remainder := 0 // to hold the remainder
sumResult := 0 // to hold the sum of digits of the input number
var num int = number // passing number to num variable
for num != 0 { // for loop runs till num is not equal to zero
remainder = num % 10 // mod func to find remainder
sumResult += remainder // adding remainder to sum
num = num / 10 // dividing num by 10 to get the next digit
}
var a int
// initialized a to 2 to avoid 1 as common factor
for a = 2; a <= sumResult; a++{ // loop runs till a is less than or equal to sumResult that we found above
// checks if both sumResult and number have same factors or not other than one
if(sumResult % a == 0 && number %a ==0){
status = true // set the status to true
break // if condition is true , break the loop
}
}
return status
}

func main() { // main function
var number int // declared variable number
fmt.Print("Enter Number: \n")
fmt.Scanln(&number) // taking input
if (share(number)){ // if function returns true, it prints share a common factor
fmt.Print("Both number and sum of digits of that number share a common factor")
}else{
fmt.Print("Do not share a factor")
}
}

Output

Enter Number:                                                                                                                                    

246                                                                                                                                              

Both number and sum of digits of that number share a common factor

Code Screenshot

Output Screenshot


Related Solutions

Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
- Write a function with a parameter called A (which is between 1 and 10) and...
- Write a function with a parameter called A (which is between 1 and 10) and prints the multiplication table for 1 to A. • Note: Do not need to draw the horizontal and vertical lines. • Example for A = 10.
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000). Hint:...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT