Showing posts with label Perfect Number. Show all posts
Showing posts with label Perfect Number. Show all posts

Perfect Number

A Perfect number is a positive integer that is equal to the sum of its proper divisors except itself. Let's take an easy example, such as 6. 1, 23 and 6 are the divisors of 6. If we add up all the numbers except 6, we end with the sum of 6 itself. 

import java.util.Scanner;

class perfect

{//class perfect opens

    public static void main(String[]args)

    {//main method opens

        

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

       int n=sc.nextInt();//variable n to accept the no

        int s=0;//variable s to store the sum of the factors

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

        {

           if(n%i==0)

            s=s+i;

        }

        if(s==n)

        System.out.println("It is a perfect no");

        else

        System.out.println("It is not a perfect no");

    }//main method closes

}//class closes

          

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...