Pages

Friday, 6 January 2017

Write a program to check whether a number is perfect or not ?

Question : Write a program to check whether a number is perfect or not ?

(A number said to be  perfect if sum of all factor  less than given number then equal to given number)
Input : 6
Output : Perfect Number
Solution : 

import java.util.*;
public class Main {
   
    public static void main(String[] args)
    {
        int n,m,sum=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number>> ");
        n=sc.nextInt();
        for(m=1;m<n;m++)
        {
            if(n%m==0)
                   sum=sum+m;
        }
        if(sum==n)
            System.out.println("Number is perfect");
        else
            System.out.println("Number is Not perfect");
     }
   
}

No comments:

Post a Comment