2
Can anyone tell me how to get the last "+" that appears on the console from this code:
import java.util.Scanner;
public class Power2 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Valor [1..2^31] ?"); 
        int valor = input.nextInt();
        for(int i=0;valor<0;i++){
            System.out.println("Valor [1..2^31] ?"); 
            valor=input.nextInt();
        }
        int[] array=new int[31];
        int[] arrayvalor=new int[31];
        int resto;
        int ndivisoes=0;
        for(int i=0;valor>0;i++){    
            resto=(valor%2);
            System.out.println("resto= "+resto);
            valor=valor/2;
            System.out.println(valor);
            array[i]=resto;
            ndivisoes=i;
            arrayvalor[i]=valor;
            //System.out.println(arrayvalor[i]);
            //System.out.println("i= "+i);
        }
        int count=ndivisoes;
        for(int j=ndivisoes;j>=0;j--){
            String soma=" ";
            if(array[j]==1){
                if(j==0){
                    soma=" ";
                    System.out.print(2+"^"+j+soma);    
                }else{
                    soma="+";
                    System.out.print(2+"^"+j+soma);
                }                      
            }
        }
    }
}
For 6 the result was
2^2+2^1+with the+at the end. Nothing is missing from the code?– Lucas Lima
@Lucasnunes with this code that I posted, it is impossible a "+" at the end. Make sure to copy and paste it in its entirety to test.
– Bacco