4.
11 (Extremes) Write an application that finds the minimum and
maximum among several integers and then computes the sum of the two
extremes. The user will be prompted for how many values the application
should ask the user to input.
package If_Else_Java;
import [Link];
public class Extremes{
static void main() {
Scanner input = new Scanner([Link]);
[Link]("How many integers will you enter? ");
int count = [Link]();
[Link]("Enter integer 1: ");
int firstValue = [Link]();
int min = firstValue;
int max = firstValue;
for (int i = 2; i <= count; i++)
{
[Link]("Enter integer %d: ", i);
int current = [Link]();
if (current < min) {
min = current;
}
if (current > max) {
max = current;
}
}
int sum = min + max;
[Link]("%nMinimum: %nMaximum: %d%n", min, max);
[Link]("Sum of extremes: %d%n", sum);
}
}
4.12 (Integers Divisible by 3) Write an application that calculates the sum
of those integers between 1 and 30 that are divisible by 3.
package If_Else_Java;
public class IntegersDivisibleby3 {
public static void main(String[] args) {
int totalSum = 0;
for (int i = 1; i <= 30; i++) {
// The modulo operator (%) checks the remainder
if (i % 3 == 0) {
totalSum += i;
}
}
[Link]("The sum of integers between 1 and 30 divisible by
3 is: %d%n", totalSum);
}
}
Exercise 4.10:
package If_Else_Java;
public class output {
static void main() {
for (int i = 1; i < 3; i++) // Outer loop: runs 2 times (i = 1, 2)
{
for (int j = 1; j < 5; j++) // Inner loop: runs 4 times for each 'i'
[Link]('*');
[Link]("\n#####"); // Prints a newline and five hashtags
}
}
}
4.13 (The Sum of a Series) Find the summation of the sequence of
numbers 1, 2, 3 ... n, where n ranges from 1 to 100. Use type long. Display
the results in a tabular format that shows n and the corresponding sum. If
this were a product instead of a sum, what difficulty might you encounter
with the variable that accumulates the product?
package If_Else_Java;
import [Link];
public class sumofsereis {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
long sum = 0;
[Link]("Enter the maximum value for n: ");
int maxN = [Link]();
[Link]("%n%-5s | %-15s%n", "n", "Sum (1 to n)");
[Link]("-------------------------");
for (int n = 1; n <= maxN; n++) {
sum += n;
[Link]("%-5d | %-15d%n", n, sum);
}
[Link]();
}
}
4.16 (Bar-Chart Printing Program) One interesting application of
computers is to display graphs and bar charts. Write an application that
reads five numbers between 1 and 30. For each number that’s read, your
program should display the same number of adjacent asterisks. For example,
if your program reads the number 7, it should display *******. Display the
bars of asterisk
package If_Else_Java;
import [Link];
public class BarChart {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n1, n2, n3, n4, n5;
[Link]("Enter 1st number (1-30): ");
n1 = [Link]();
[Link]("Enter 2nd number (1-30): ");
n2 = [Link]();
[Link]("Enter 3rd number (1-30): ");
n3 = [Link]();
[Link]("Enter 4th number (1-30): ");
n4 = [Link]();
[Link]("Enter 5th number (1-30): ");
n5 = [Link]()
for (int i = 0; i < 5; i++) {
[Link]("Enter number %d (1-30): ", i + 1);
int val = [Link]();
if (val >= 1 && val <= 30) {
numbers[i] = val;
} else {
[Link]("Invalid input. Please enter a number
between 1 and 30.");
i--; // Decrement i to repeat this specific input
}
}
[Link]("\n--- Bar Chart ---");
// 2. Display the bars after reading all five numbers
for (int i = 0; i < 5; i++) {
// For each number, print that many asterisks
for (int j = 0; j < numbers[i]; j++) {
[Link]('*');
}
[Link](); // Move to the next line for the next bar
}
[Link]();
}
}
s after you read all five numbers.