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

Asked

Viewed 985 times

0

I need to do a show 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; 

That’s all I could do.

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

I thought of creating a variable NUMX but I don’t know if it’ll work.

3 answers

1

With the contribution of Van Ribeiro I was able to do what was 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));

}
  • I’m glad it worked out! ✌

0

Good friend, for this problem I would make a variable accumulator, to add the results of the operation. So then:

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

After reading, 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 passed. In case you want to know which were all the variables that were added, I suggest using a stack for this.

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 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 it helped

0

John, I don’t know if it helps you, but I did it based on the instructions you passed on 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 is updated every time a transaction is made.

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

And every time while is executed, the variable soma is updated as a result of n, being added upon itself, until n be 1.

I hope it helps you! See you later!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.