AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
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 353programming x 169 Posted On : 2016-12-21 23:27:31.0 Aarti Kumar 5900 5 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 523188225701Reply This Thread 4 