CAN WE PUT ANS VARIABLE AT LAST

import java.util.Scanner;

class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
Scanner scn = new Scanner(System.in);
int snum = scn.nextInt();
int sb = scn.nextInt();

    int res = anyBaseToDecimal(snum , sb);
    System.out.println(res);
}

public static int anyBaseToDecimal(int snum , int sb){
    int multiplier = 1;
    int ans = 0;
    while (snum!= 0){
        int rem = snum % 10;

        snum = snum/10; 
                    
        multiplier = multiplier*sb;
        ans = ans + (rem*multiplier);
    }
    return ans;
}

}

HERE WHY CAN NOT WE WRITE
ans = ans + (rem*multiplier);
THIS LINE AT LAST

@varenatray_316d1f71b9922ccb We cannot write it last bcoz you’re changing your multiplier in the while loop which means the multiplier used for the first iteration will be 10 but it should be 1 for the first iteration thats why you cannot use multiplier after updating it in the loop.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.