Problem regarding possible lossy conversion from double to int

I have been making a program to find the number of trailing zeroes at the end of the factorial of a number.
The program first of all takes the input ‘a’- the number of the numbers it has to operate on. Then it takes ‘a’ number of inputs and calculates number of zeroes at the end of the factorial of the integers I have inserted. The problem lies in the 6th line , where I have declared a variable of type int. This variable stores the integer quotient of a division process. But the compiler keeps reporting the error "Possible lossy conversion from double to int. Now on declaring the variable of type double, the variable stops ignoring the fractional part of the number produced after division. How can I rectify it?
The code I have written is as follows :

  Scanner p=new Scanner(System.in);

  int a=p.nextInt();       //This shows the numbers of inputs to be taken

        for(int i=1;i<=a;i++)   //This loops makes program takes a number of inputs

  {

      int b=p.nextInt();

      int sum=0;                         //This line is the one creating problem.

      for(int j=1;Math.pow(5,j)<=b;j++)    //Taking out max power of 5 contained in the input.
   
      {
          
         sum=sum+b/Math.pow(5,j);           //This is where it reports the lossy conversion error
                    
      }

      System.out.println(sum);

  }

@karan pow returns float by default and you are storing it in int so there may be some data loss and the language may not allow it like it do in c. This may not be the issue but just try explicit data type conversion or take your sum as float initially and then take a int variable s…

s=(int)sum;

I think i know which Q you are trying. I will ask you 2 things-

  1. Math.pow(5,j) Is this GUARANTEED to fit inside the normal data range and wont overflow?? If the Q deals with factorials, that too, more than 20! , then overflow is bound to happen.
  2. Did you try storing stuff in a double variable, and converting it into int JUST before giving answer. Eg-
    2.8+1.2=4.0 . int (4.0)=4.
1 Like

One Approach to your solution i came is–
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package debugothercode;

import java.util.Scanner;

/**
 *
 * @author Hemant Dhanuka
 */
class TWONMS {

    public static void main(String[] args) {
        Scanner p = new Scanner(System.in);

        int a = p.nextInt();

        for (int i = 1; i <= a; i++) {

            int b = p.nextInt();

            double sum = 0;
            double sum1 = 0;

            for (int j = 1; Math.pow(5, j) <= b; j++) {

                sum = sum + Math.floor(b / Math.pow(5, j));
                sum1 = sum1 + b / Math.pow(5, j);
                System.out.println(sum);
                System.out.println(sum1);
            }

        }
    }

}

u can use Math.floor function to remove fractional part then add it double type sum variable

@vijju123 Thank you sir. It worked as a charm.