Computer Science, asked by harsh00876, 1 year ago

write a java program which check whether a number is perfect or not​

Answers

Answered by Arishshahid
2

Hope it helps buddy

perfect number:-

import java.util.*;

class Perfect_Number

{

public static void main()

{

Scanner sc= new Scanner(System.in);

System.out.println("Please enter a number ");

int n=sc.nextInt();

int i= 1,sum=0;

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

{

if(n%i == 0 )

sum=sum+i;

}

if(n == sum)

System.out.println("it is a perfect number ");

else

System.out.println("it is not a perfect number");

}

}

this is the entire code for perfect number.

Answered by atrs7391
0

package com.company;

/* Write a program to accept a number and check whether the number is a perfect square or not.

Sample Input=49

Sample Output= A perfect square.

*/

import java.util.Scanner;

public class Main

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner (System.in);

       System.out.println("Enter a Number:");

       double n = sc.nextDouble();

       double n2 = Math.sqrt(n);

       if (n2%1==0) {

           System.out.println("The entered number is a perfect square");

       }

       else {

           System.out.println("The entered number is not a perfect square");

       }

   }

}

Similar questions