- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Find the Smallest Divisor of an Integer
Consider that the integer is: 75
Divisor of that integer is: 3, 5, 15, ..., 75
The smallest divisor is: 3
Steps
- Take an integer from the user.
- Initialize a variable (res) with that number.
- Use a for loop where the value of i ranges from 2 to the integer.
- If the number is divisible by i, compare with res. If res > i, then update res with i.
- Exit from the loop and print res.
Example
package main import "fmt" func main(){ var n int fmt.Print("Enter the number: ") fmt.Scanf("%d", &n) res := n for i:=2; i<=n; i++{ if n%i == 0{ if i<=res{ res=i } } } fmt.Printf("The smallest divisor of the number is: %d", res) }
Output
Enter the number: 75 The smallest divisor of the number is: 3
- Related Articles
- Python Program to Find the Smallest Divisor of an Integer
- Golang Program to Generate all the Divisors of an Integer
- Golang Program To Get The Successor Of An Integer Number
- Find the Smallest Divisor Given a Threshold in C++
- Golang Program to count the set bits in an integer.
- Golang Program to convert an integer into binary representation
- Golang Program To Get The Predecessor Of An Integer Number Using Library Function
- Find the k-th smallest divisor of a natural number N in C++
- Python program to find better divisor of a number
- Find an integer X which is divisor of all except exactly one element in an array in Python
- Find an integer X which is divisor of all except exactly one element in an array in C++
- How to Print an Integer in Golang?
- Golang program to create an integer array that takes inputs from users.
- Java program to find the smallest number in an array
- C# Program to find the smallest element from an array

Advertisements