We appreciate your visit to 2 Write a Java program to swap two variables 3 Write a Java program to print a face 4 Write a Java program to compare. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!

2. Write a Java program to swap two variables.

3. Write a Java program to print a face.

4. Write a Java program to compare three numbers. Note: Accept all numbers from the keyboard.

5. Write a Java program to check if a character is an alphabet using if-else.

6. Write a Java program to check if a number is even or odd.

7. Write a Java program to find the factorial of a number.

8. What will be the output for the following code snippet?

```java
public class PrintPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
```

9. Write a Java program to check if a character is a vowel or consonant using if and switch statements.

10. Describe what happens if, in `helloWorld.java`, you omit:
a. `main`
b. `String`
c. `HelloWorld`
d. `System.out`

11. Write a Java program to calculate compound interest and simple interest:

- Compound Interest: [tex]\text{compoundInterest} = \text{principal} \times (\text{Math.pow}((1 + \text{rate} / 100), \text{time})) - \text{principal}[/tex]
- Simple Interest: [tex]\text{simpleInterest} = (\text{principal} \times \text{rate} \times \text{time}) / 100[/tex]

12. Write a Java program to calculate the power of a number by accepting the exponent and base from the user.

13. Write a Java program to count vowels and consonants in a string.

14. Write a Java program to reverse an array element (from the last element to the first element).

15. Write a Java program to print out the following pattern:

[tex]\[
\begin{array}{|r|}
\hline
12345 \\
1234 \\
123 \\
12 \\
1 \\
\hline
\end{array}
\][/tex]

16. Write the output and show the reason behind each result for the following code:

```java
public class PrintPattern {
public static void main(String[] args) {
int x = 14, y = 17;
System.out.println(x | y);
System.out.println(x & y);
System.out.println(x >> 2);
System.out.println(y >> 1);
System.out.println(x << 2);
}
}
```

17. Write a Java program to sum the values of an array and calculate the average value of the array. The values of the array are [tex]\{1, 2, 3, 4, 5, 6, 78, 9, 10\}[/tex].

18. Write a Java program to print odd and even numbers from an array element [tex]\{1, 2, 3, 4, 5, 6, 7, 8, 9, 11\}[/tex].

19. Write a Java program to find all roots of a quadratic equation:

- [tex]x = \frac{{-b \pm \sqrt{b^2 - 4ac}}}{{2a}}[/tex]

20. Write a Java program to generate a multiplication table.

Answer :

To solve the problem of finding the sum and average of the given array values in Java, we need to follow these steps:

1. Create an Array: First, we'll make an array to hold the elements for which we want to calculate the sum and average. In this case, the array is `{1, 2, 3, 4, 5, 6, 78, 9, 10}`.

2. Initialize Variables: We'll need a variable to store the sum of the elements.

3. Calculate the Sum: Loop through each element of the array, and add each element's value to the sum variable.

4. Calculate the Average: After the loop, the sum contains the total of all the elements. The average can be calculated by dividing this sum by the number of elements in the array.

5. Display Results: Finally, print out the sum and average to see the results.

Here's how you can write this program in Java:

```java
public class ArraySumAndAverage {
public static void main(String[] args) {
// Step 1: Create an array with the given values
int[] arrayValues = {1, 2, 3, 4, 5, 6, 78, 9, 10};

// Step 2: Initialize a variable to store the sum
int sum = 0;

// Step 3: Calculate the sum of all elements in the array
for (int value : arrayValues) {
sum += value;
}

// Step 4: Calculate the average
double average = (double) sum / arrayValues.length;

// Step 5: Print out the results
System.out.println("Sum of array elements: " + sum);
System.out.println("Average of array elements: " + average);
}
}
```

Explanation of Results:
- Sum: The sum of the numbers in the array is 118.
- Average: The average, which is the sum (118) divided by the number of elements (9), comes out to be approximately 13.1111.

This process effectively computes both the sum and average using simple loops and arithmetic operations in Java.

Thanks for taking the time to read 2 Write a Java program to swap two variables 3 Write a Java program to print a face 4 Write a Java program to compare. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!

Rewritten by : Barada