Programas Java: Cálculos y Estructuras
Programas Java: Cálculos y Estructuras
The program identifies if a number is positive or negative by reading user input into an 'int num' variable. It uses a simple 'if' statement to check if 'num' is less than zero, in which case it prints 'el numero es negativo'; otherwise, it outputs 'el numero es positivo', indicating that the number is not negative .
The program iteratively prints odd numbers between 1 and 15 using a 'for' loop starting from 1, incrementing by 2 in each cycle (i.e., 'i += 2'). By this approach, only odd numbers are printed: 1, 3, 5, 7, 9, 11, 13, and 15, fulfilling the requirement to output all odd numbers in that range .
Using a 'switch' statement, the program assigns numbers 1 through 12 to respective month names, outputting month names in Spanish, e.g., 'enero' for 1 and 'diciembre' for 12. If a user input outside this range is detected, it triggers the 'default' case, indicating an invalid input with 'ingrese un numero valido'. This structured approach ensures accurate month display and error handling .
The program checks if a number is prime by decrementing a second variable 'num2' initialized to one less than 'num'. It employs a 'while' loop testing if 'num' mod 'num2' equals zero (a non-prime condition). The loop continues until 'num2' decreases to 1. A number is recognized as prime when 'num2' equals 1 at the loop's termination, implying it has no divisors other than 1 and itself. If 'num2' is greater than 1 after exiting the loop, it indicates a divisor was found and the number is not prime .
After receiving user input for a specific number 'tabla', the program utilizes a 'for' loop running from 1 to 12 to compute and display the multiplication table by multiplying 'tabla' with the loop variable 'num' in each iteration. Each result is printed in the format '{tabla} X {num} = {result}', effectively presenting the multiplication table up to the 12th multiple .
The program calculates the total area and volume of a cylinder by prompting the user to input the cylinder's radius (r) and height (h). It uses the formulas A = 2πrh + 2πr² for the area and V = πr²h for the volume, represented in the program by 'a = (2)* (3.1416)* (r)*(h+r)' and 'v = (3.1416)* (r*r) * (h)'. The final results are displayed as output for the user .
The program applies a sliding scale for price per unit based on quantity ranges: 1-25 units cost 27.7 each, 26-50 cost 25.5 each, 51-75 cost 23.5 each, and more than 75 cost 21.5 each. Additionally, if the purchase exceeds 50 units, a 15% discount is applied; otherwise, a 5% discount is given. The logic is implemented using 'if' and 'else if' statements to determine the applicable discount and compute the final purchase amount .
To compute the average of a series of numbers, the program first asks for the number of values ('num') to be averaged. It then iterates 'num' times, prompting the user to input each value (stored in 'calificacion') and accumulating them in 'suma'. After summing all entered values, the program calculates the average by dividing 'suma' by 'num', outputting the result .
The program calculates the factorial by initializing a variable 'factorial' to 1 and using a 'while' loop. The loop multiplies 'factorial' by 'numero' and decrements 'numero' until it reaches zero. This iterative approach effectively computes the factorial by successively multiplying numbers from 'numero' down to 1, displaying the result as the computed factorial .
The program uses a 'switch' statement to correspond a user's input (1-7) to a weekday. Each 'case' in the 'switch' equates a number to a respective weekday name: 1 for 'lunes', 2 for 'martes', continuing through 7 for 'domingo'. For invalid inputs, the default case prompts 'ingrese un numero valido', ensuring all numbers map logically to weekday names or a validation error .