The given Java program checks whether a given number is a perfect number or not.

- The program starts by importing the Scanner class from the java.util package to get user input.
- Then, the program prompts the user to enter a number and reads the input using the nextInt() method of the Scanner class.
- Next, the program initializes a variable sum to 0, which will be used to store the sum of factors of the number.
- The program uses a for loop to iterate through all the numbers from 1 to n - 1, inclusive. For each number, the program checks whether it is a factor of n. If it is, then the program adds it to the sum variable.
- After the loop ends, the program checks whether the value of sum is equal to n. If it is, then n is a perfect number, and the program prints a message to the console indicating that n is a perfect number. Otherwise, the program prints a message indicating that n is not a perfect number.

import java.util.Scanner; public class perfect { //Write a program to check the given number is perfect number or not. public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter The Number : "); int n = in.nextInt();//6 int sum=0; for (int i = 1; i <n; i++) { if(n%i==0){ sum+=i;//1+2+3 } } if(sum==n){ System.out.println(n + " is a Perfect Number"); }else { System.out.println(n + " is not a Perfect Number"); } } }

Enter The Number : 6 6 is a Perfect Number

Enter The Number : 13 13 is not a Perfect NumberTo download raw file Click Here

Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions