AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
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 ?.
Follow @anyforumin
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 353
programming x 169
Posted On :
2016-12-21 23:27:31.0
Aarti Kumar
59
0
0
5
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
Rishi Kumar
523
1882
41268
Reply This Thread
4
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in