Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Perform this exercise in Java language:
Create a program in which you enter integers and calculate:
to. the product of all even numbers less than 25
b. The sum of all odd numbers greater than 16
c. The average of all numbers multiples of 4

What I have tried:

Java
Scanner sc= new Scanner (System.in);
System.out.println("Ingrese un número");
numero=sc.nextInt();

for(i=1;i<=numero;i++){
    if(number %2==0 && number<25 ){
      acum=acum*multiplo;
    }
    else if (number>16) { 
     impares=impares+i;
     conti=conti+1;
    }
    else if(number%4==0 && i<=numero){
      multiplos=multiplos+i;
      cont=cont+1;    
    }        
    prom=multiplos/cont;
    System.out.println("El producto de todos los números pares menores a 25 es:"+acum);
    System.out.println("La suma de todos los números impares mayores a 16 es:"+impares);
    System.out.println("El promedio de todos números múltiplos de 4 es:"+prom);
}
Posted
Updated 8-Apr-24 21:40pm
v2

Move the read of the number inside the for loop ...

Two other things:
1) Read the question carefully: the first part requires even number, the second odd, but the third needs odd and even. There is no "third state"!
2) Pick an indentation style and stick to it correctly: the indentation of your curly brackets is important for readability!
 
Share this answer
 
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Java
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese un número");
numero = sc.nextInt();

for (i = 1; i <= numero; i++) {
    if (number % 2 == 0 && number < 25) {
        acum = acum * multiplo;

    } else if (number > 16) {
        impares = impares + i;
        conti = conti + 1;

    } else if (number % 4 == 0 && i <= numero) {
        multiplos = multiplos + i;
        cont = cont + 1;
    }

    prom = multiplos / cont;
    System.out.println("El producto de todos los números pares menores a 25 es:" + acum);
    System.out.println("La suma de todos los números impares mayores a 16 es:" + impares);
    System.out.println("El promedio de todos números múltiplos de 4 es:" + prom);
}
}    // this indicate that there is a missing '{' in this piece of code

Indentation style - Wikipedia[^]
https://codebeautify.org/javaviewer[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
v2
Comments
Richard MacCutchan 9-Apr-24 3:37am    
It's Java not Javascript.
Patrice T 9-Apr-24 4:01am    
Ooops

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900