AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: find nearest factorial

Write a program to find the nearest factorial. The program takes an integer as an input and has to find a number which is nearest to it which is a factorial of another number
Explanation:
The factorial of each positive numbers are as 1,2,6,24,120,720,..etc
The input is 115 which is closest to 120
So the nearest factorial is 120
Sample
Input:
115

Output:
120

corejava x 352
programming x 168
Posted On : 2016-12-21 23:27:31.0
profile Aarti Kumar - anyforum.in Aarti Kumar
5900
up-rate
5
down-rate

Answers


import java.util.Scanner;

public class NearestFactorial{
public static void main(String s1[]){
Scanner input = new Scanner(System.in);
int num=input.nextInt();
int i=1;
int result=1;
int temp=1;
while(!(result*i>num)){
result=result*i;
if(result==num){
System.out.println(num);
break;
}
i++;
temp=result*i;
}
if(result!=num) {
int diff=num-result;
int diff1=temp-num;
if(diff>=diff1)
System.out.println(temp);
else
System.out.println(result);
}
}
}

Posted On : 2016-12-22 22:59:32
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221961
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in