0% found this document useful (0 votes)
2 views

Java_Assignment_1_(1132220874)

The document contains Java code for two assignments: the first program calculates the factorial of a number both using recursion and without recursion, while the second program checks if a number is a perfect number and finds the nearest perfect number. The code includes classes and methods for each task, demonstrating the implementation of the algorithms. The examples use specific numbers for testing the functionality of the programs.

Uploaded by

phoniexstar2304
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_Assignment_1_(1132220874)

The document contains Java code for two assignments: the first program calculates the factorial of a number both using recursion and without recursion, while the second program checks if a number is a perfect number and finds the nearest perfect number. The code includes classes and methods for each task, demonstrating the implementation of the algorithms. The examples use specific numbers for testing the functionality of the programs.

Uploaded by

phoniexstar2304
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Neeraj Vaze

PRN: 1132220874

Java Assignment 1

Q1. Program to find factorial of given number using recursion and


without recursion.

 Using Recursion:-

public class FactorialRecursion {

public static void main(String[] args) {

int number = 5;

int factorial = findFactorial(number);

System.out.println("Factorial of " + number + " using recursion is: " +


factorial);

public static int findFactorial(int n) {

if (n <= 1) {

return 1;

return n * findFactorial(n - 1);

 Without recursion

public class FactorialNonRecursion {

public static void main(String[] args) {

int number = 7;
int factorial = findFactorial(number);

System.out.println("Factorial of " + number + " without recursion is: " +


factorial);

public static int findFactorial(int n) {

int result = 1;

for (int i = 2; i <= n; i++) {

result *= i;

return result;

Q2. Write a program to check entered number is perfect number or


not. Print the Perfect Number in given range.

public class PerfectNumber {

public static void main(String[] args) {

int number = 31;

if (isPerfectNumber(number)){

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

}else{

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

int nearestPerfect = findNearestPerfectNumber(number);

System.out.println("The nearest perfect number to " + number + " is: "


+ nearestPerfect);

}
}

public static int findNearestPerfectNumber(int n) {

int lower = n - 1;

int upper = n + 1;

while (true) {

if (lower > 0 && isPerfectNumber(lower)) {

return lower;

if (isPerfectNumber(upper)) {

return upper;

lower--;

upper++;

public static boolean isPerfectNumber(int n) {

if (n < 2) {

return false;

int sum = 1;

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

sum += i;
}

return sum == n;

You might also like