Digits In Factorial

PHOTO EMBED

Sun Feb 06 2022 01:44:16 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #mathematics #gfg #geeksforgeeks #digitsinfactorial

import java.io.*;
import java.util.*;

class Solution{
    public int digitsInFactorial(int N){
        
        if (N < 0)
            return 0;
  
        // base case
        if (N <= 1)
            return 1;
  
        // else iterate through n and calculate the value
        double digits = 0;
        for (int i=2; i<=N; i++)
            digits += Math.log10(i);
  
        return (int)(Math.floor(digits)) + 1;
    }
}

public class Main {
	public static void main (String[] args) {
		Scanner sc=new Scanner(System.in);
		
		//taking total testcases
		int T=sc.nextInt();
		
		while(T-->0)
		{
		    Solution obj=new Solution();
		    int N;
		    
		    //taking N
		    N=sc.nextInt();
		    
		   //calling method digitsInFactorial()
		   System.out.println(obj.digitsInFactorial(N));
		    
		}
		
	}
}
content_copyCOPY

5. Digits In Factorial Given an integer N. Find the number of digits that appear in its factorial. Factorial is defined as, factorial(n) = 1*2*3*4……..*N and factorial(0) = 1. Example 1: Input: N = 5 Output: 3 Explanation: Factorial of 5 is 120. Number of digits in 120 is 3 (1, 2, and 0) Example 2: Input: N = 120 Output: 199 Explanation: The number of digits in 120! is 199 Your Task: You don't need to read input or print anything. Your task is to complete the function digitsInFactorial() that takes N as input parameter and returns number of digits in factorial of N. Expected Time Complexity: O(N) Expected Auxilliary Space: O(1) Constraints: 1 ≤ N ≤ 10^5

https://practice.geeksforgeeks.org/problems/digits-in-factorial/1/?track=DSASP-Mathematics&batchId=190