write a program in java to accept a number and check whether it is a perfect or not perfect no
Answers
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;
}
}:
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");
}
}
}