- 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 Check if a Number is a Perfect Number
Steps
- Read an integer and store it in a variable.
- Initialize a variable to count the sum of the proper divisors to 0.
- Use a for loop and an if statement to add the proper divisors of the integer to the sum variable.
- Check if the sum of the proper divisors of the number is equal to the variable.
- Print the final result.
Enter any number: 6 The number is a Perfect number! | Enter any number: 25 The number is not a Perfect number! |
Example
package main import "fmt" func main(){ var n int fmt.Print("Enter any number: ") fmt.Scanf("%d", &n) sum1 := 0 for i:=1; i<n;i++{ if n % i ==0{ sum1 += i } } if sum1 == n { print("The number is a Perfect number!") }else{ print("The number is not a Perfect number!") } }
Output
Enter any number: 6 The number is a Perfect number!
Advertisements