Program Discussion :: Basics
20 / 279
Write a function to accept a number 'n' and
a)if n is negative then return -1.
b)if n is positive and perfect number then return sqrt(n).
c)if n is positive and not a perfect no. then return n(n+1)/2
Answer:
public class CheckNumber {
static int sum=0;
static boolean isPerfact(int n) {
for(int i=1;i
Asked In ::
Language:
DEEPASRI
30 May, 2022 10:42 AM
public class CheckNumber {
static int sum=0;
static boolean isPerfact(int n) {
for(int i=1;i<n;i ) {
if(n%i==0) {
sum=sum i;
}
}if(sum==n) {
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<0) {
System.out.println("Negative");
}else if(n>0