Kpit Solutions 1. Recursion of No.: @placement - Fellas On Telegram

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

FOR MORE MATERIALS AND OFF CAMPUS DATES

JOIN PLACEMENT FELLAS ON TELEGRAM (CLICK HERE)


Kpit solutions

1. Recursion of no.

public class AddNumbers {

public static void main(String[] args) {


int number = 20;
int sum = addNumbers(number);
System.out.println("Sum = " + sum);
}

public static int addNumbers(int num) {


if (num != 0)
return num + addNumbers(num - 1);
else
return num;
}
}

2.PERFECT NUMBER
Class number{
{
@PLACEMENT_FELLAS ON TELEGRAM
FOR MORE MATERIALS AND OFF CAMPUS DATES
JOIN PLACEMENT FELLAS ON TELEGRAM (CLICK HERE)

static boolean isPerfect(int n)


{
int sum = 1;
for (int i = 2; i * i <= n; i++)
{
if (n % i==0)
{
if(i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1)
return true;
return false;
}
public static void main (String[] args)
{
System.out.println("Below are all perfect" +
"numbers till 10000");
for (int n = 2; n < 10000; n++)
if (isPerfect(n))
System.out.println( n +
" is a perfect number");
}
}

@PLACEMENT_FELLAS ON TELEGRAM

You might also like