Create a program asks a user for an odd positive integer. If the user gives and even number or a number that is negative it prompts the user to enter a number again. Once the user enters a number it sums all the odd numbers between 1 and the number entered.

Respuesta :

Debel

Answer & Explanation:

//written in java

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       // variable for number imput and for the output to

       //to be calculated

       int number, output = 0;

       //a do while loop to first ask for user input

       //and check if its a odd number and a positive integer

       do {

           System.out.println("Please enter a positive odd integer");

           //scanner class to accept user input

           Scanner input = new Scanner(System.in);

           number = input.nextInt();

           //the next line make sure input is a

           // positive integer and also odd

           //number

       } while (number % 2 == 0 || number < 1);

       //for to sum all value of odd numbers between

       //1 and the inputted value

       for (int i = 3; i < number; i = i + 2) {

           output = output + i;

       }

       //print out output

       System.out.println("sums of all the odd numbers between 1 and the " + number + " is " + output);

   }

}

...............................................................................................................................................

Sample of the program output

> Please enter a positive odd integer

< 12

> Please enter a positive odd integer

< 13

> sums of all the odd numbers between 1 and the 13 is 35

ACCESS MORE