Computer Science, asked by saniaseraj, 1 year ago

write a program in java to check whether a number is a perfect number or not.

Answers

Answered by Anonymous
11

import java.util.Scanner;

public class Perfect

{

   public static void main(String[] args)  

   {

       int n, sum = 0;

       Scanner s = new Scanner(System.in);

       System.out.print("Enter any integer you want to check:");

       n = s.nextInt();

       for(int i = 1; i < n; i++)

       {

           if(n % i == 0)

           {

               sum = sum + i;

           }

       }

       if(sum == n)

       {

           System.out.println("Given number is Perfect");

       }

       else

       {

           System.out.println("Given number is not Perfect");

       }    

   }

   int divisor(int x)

   {

      return x;

   }

}

Answered by brainusers
3
Please mark it as the brainliest answer.

// Write Perfect Number program in Java using For Loop */ package FrequentPrograms; import java.util.Scanner; public class PerfectNumberUsingFor { private static Scanner sc; public static void main(String[] args) { int i, Number, Sum = 0 ; sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); Number = sc.nextInt(); for(i = 1 ; i < Number ; i++) { if(Number % i == 0) { Sum = Sum + i; } } if (Sum == Number) { System.out.format("\n% d is a Perfect Number", Number); } else { System.out.format("\n% d is NOT a Perfect Number", Number); } } }


Similar questions