Store multiple numbers in a variable and then print and show the sum

0

I need to do a program that does the following:

• Start with an integer N

• If N is even, divide by 2;

• If N is odd, multiply by 3 and add 1;

• Repeat this new process with the new value of N, if N ≠ 1;

• Ends the process when N = 1.

Scanner NUM = new Scanner(System.in);
double numerox; 
System.out.println("Digite um numero inteiro: ");
double NUM1 = NUM.nextDouble();


    while ((NUM1 % 2 == 0) && (NUM1 != 1))
    {
     numerox = NUM1 * 2;
        System.out.println(+numerox);
        break; 

I just did it.

What can I do to create a variable that stores these numbers and then print them and show their sum?

I thought about creating a variable NUMX but I do not know if it will work.

    
asked by anonymous 30.08.2018 / 03:08

3 answers

1

With the contribution of Van Ribeiro I was able to do what I asked, I completely discarded the way I was trying to do using array and FOR.

    Scanner leitor = new Scanner(System.in);

    System.out.println("Digite um número:");
    int n = leitor.nextInt();

    final int n2 = n;
    int soma = 0;

    System.out.println();
    System.out.print("Sequência: "+n+" ");

    while(n != 1){

        if(n%2==0){
            n = n/2;
        }else{
            n = (n * 3) + 1;
        }

        System.out.print(n+" ");

        soma += n;

    }
    System.out.println();
    System.out.println("Soma: "+(n2+soma));

}
    
31.08.2018 / 16:24
0

John, I do not know if it helps, but I did based on the instructions that came up in the question:

Scanner lendo = new Scanner(System.in);
int n = lendo.nextInt();
int soma = 0;

while(n != 1){
    if(n%2==0){
        n = n/2;
    } else{
        n = (n * 3) + 1;
    }
    soma += n;
}

System.out.println(soma);

From what I understand from your instructions, you need a n to be updated every time the operation is performed.

In this case, I made every time a mathematical operation was calculated, after obeying the condition of the number being even or odd, the variable n itself received the result of these operations.

And, every time the while is executed, the variable soma is updated with result of n , being increased on itself, until n is 1.

Hope you can help! See you later!

    
31.08.2018 / 06:38
0

Good friend, for this problem I would make a cumulative variable, to sum the results of the operation. So being like this:

int acum = 0; //variavel acumulador
int N; //variavel a ser lida

After being read, the logic for the problem would be:

while(N!=1){ //Enquanto N for diferente de 1
    if(N%2==0){
        acum += N; //Soma o valor de N ao acumulador
        N /= 2; //Divisao de N/2, seria o mesmo de N = N/2
    }else{
        acum += N;
        N = N*3 + 1;
    }
}

With this result, you would have the sum of all the variables that have passed. If you want to know what all the variables were added to, I would suggest using a stack for that.

Stack<Integer> pilha = new Stack<>();//variavel acumulador
int N; //variavel a ser lida

while(N!=1){ //Enquanto N for diferente de 1
    if(N%2==0){
        pilha.push(N); //Inserção na Pilha
        N /= 2; //Divisao de N/2, seria o mesmo de N = N/2
    }else{
        pilha.push(N);
        N = N*3 + 1;
    }
}

To display the stack sum however:

int acum = 0;
while(!pilha.empty()){ //enquanto a pilha não for vazia
    acum += pilha.pop();

}

System.out.println("Valor do Acumulador: " + acum);
System.out.println("Valores da Pilha");
for(Integer val : pilha){
    System.out.println("Valor: " + val);

}

I hope you have helped

    
31.08.2018 / 18:10