0% found this document useful (0 votes)
186 views2 pages

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

This document contains code snippets for two Java programs. The first program uses recursion to calculate the sum of all numbers from 1 to a given number. The second program checks if a number is a "perfect number" by calculating the sum of its proper divisors and comparing it to the original number. Both programs print the results.

Uploaded by

Ajay YA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views2 pages

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

This document contains code snippets for two Java programs. The first program uses recursion to calculate the sum of all numbers from 1 to a given number. The second program checks if a number is a "perfect number" by calculating the sum of its proper divisors and comparing it to the original number. Both programs print the results.

Uploaded by

Ajay YA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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