Value Change to some random value in the middle of the iteration

52 Views Asked by At
import java.util.*;

public class Main
{
    public static int plusOne(int[] digits) {
        int no =0;
        for(int i=0; i< digits.length; i++){
            if(digits[i]==0){
                no = no*10;
            }
            else{
                no = no*10+ digits[i];
            }
            
        }
        no = no+1;
        return no;
    }
    public static void main(String[] args) {
        System.out.println("Hello World");
        int arr[] = {9,8,7,6,5,0,4,3,2,1,0};
        
        System.out.println(plusOne(arr));;
        
    }
}

EXPECTED OUTPUT:- 98765043211

OUTPUT:- -19204598

ISSUE:- In the last run when i ==9 , no change to some random value

I am trying to simply create a no by taking all the values from the array and adding one to it *Also try with double but it does not resolve the problem

0

There are 0 best solutions below