Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- System.out.printf("Ingrese límite superior: ");
- Scanner sc = new Scanner(System.in);
- String lim_sup = sc.nextLine();
- System.out.printf("Ingrese límite inferior: ");
- String lim_inf = sc.nextLine();
- sc.close();
- int lim_sup_int = Integer.parseInt(lim_sup);
- int lim_inf_int = Integer.parseInt(lim_inf);
- if (lim_sup_int % 2 == 0) {
- lim_sup_int--;
- }
- if (lim_inf_int % 2 == 0) {
- lim_inf_int++;
- }
- ArrayList<Integer> sum_elements = new ArrayList<Integer>();
- double sum = 0;
- int sum_elements_i= 0;
- for(int i = 0; i*2+1 <= lim_sup_int; i++) {
- if(i*2+1 < lim_inf_int) {
- continue;
- }
- else {
- int sum_element = i*2+1;
- sum_elements.add(sum_element);
- sum += sum_elements.get(sum_elements_i);
- sum_elements_i++;
- }
- }
- DecimalFormat df = new DecimalFormat("#.##");
- System.out.println("La suma de todos los números impares desde " + lim_inf + " hasta " + lim_sup + " es " + df.format(sum));
- System.out.println("Se sumaron los siguientes números: \n" + sum_elements);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement