#Need to check if this is well done

37 messages · Page 1 of 1 (latest)

silver sparrow
#

I wanna know if this exercise i did is like this or if i did wrong; the exercise is
"A person purchased a product to pay in 20 months. The first month he paid €10, the second €20, the third €40 and so on. Make a program to determine how much you must pay each month and the total amount you will pay after 20 months."

My code:

public class Ejercicio3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a=5;
        int i=1;
        int suma=0;
        
        while (i<=20) {
            a=a*2;
            System.out.println("Mes "+i+": "+a+" euros");
            suma+=a;
            i++;
        }
        System.out.println("El total es: "+suma+" euros");
    }
}
little otterBOT
#

Hey, @silver sparrow!
Please remember to /close this post once your question has been answered!

desert mesa
#

hllo

silver sparrow
#

hi

desert mesa
#

arrent there input and output

silver sparrow
#

input and output? what that means? 😅

desert mesa
#

like output is System.out.println("hello");

silver sparrow
#

oh

#

so i think i need to output

desert mesa
#

is that the full quetion

#

what is the exercise quetion full

silver sparrow
#

yah the exercise question full is the one i give on the top

#

"A person purchased a product to pay in 20 months. The first month he paid €10, the second €20, the third €40 and so on. Make a program to determine how much you must pay each month and the total amount you will pay after 20 months."

desert mesa
#

there is actually a way

#

u can do

#

arthermitic progression

#

or u can just use loops and stuff too

silver sparrow
#

My code:

public class Ejercicio3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a=5;
        int i=1;
        int suma=0;
        
        while (i<=20) {
            a=a*2;
            System.out.println("Mes "+i+": "+a+" euros");
            suma+=a;
            i++;
        }
        System.out.println("El total es: "+suma+" euros");
    }
}```
#

i gave this on the top 😅 just needed to know if this is right

desert mesa
#

its not many things wrong

#

the while loop will stop when its equal to 20

#

u need 20 also right ??

#

i<=20 this is true when i == 20 so it wills top

#

stoop

silver sparrow
#

20 months ya

#

but the 20th month is paid too no?

#

for that i put i<=20; you have to pay the month 1, the month 2, month 3...

desert mesa
#

i<20;

#

use like this then

#

sorry

#

ur is correct

silver sparrow
#

yah if i put i=0; then while i<20. I did i=1 so i can show the month too

desert mesa
#

its correct

silver sparrow
#

wow ty, and nice!:D

desert mesa
#

i didnt did anythign lol

silver sparrow
#

the confirmation is what i needed so ya, u did 😛 haha ; tyy, so now i close this and give ur heart 🙂