Java Loop Ssss
Java Loop Ssss
Explanation:
Sample Output:
less
Source Code
Enter a number: 5
Multiplication table for 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Explanation:
Sample Output:
*****
****
***
**
*
This program will print the desired pattern. Simply run the
code, and it will display the pattern as expected.
3. Write a Java program to print the area and perimeter of a
circle.
java
Source Code
import java.util.Scanner;
Explanation:
Sample Output:
arduino
Source Code
Enter the radius of the circle: 5
Area of the circle: 78.53981633974483
Perimeter (Circumference) of the circle: 31.41592653589793
This program will compute and display the area and perimeter
of the circle based on the radius provided by the user.
Practical 2
This program adds two binary numbers that the user inputs and
prints the result in binary format.
import java.util.Scanner;
Sample Outputs:
scanner.close();
}
}
Explanation for (b):
This program takes a string input from the user and reverses it.
java
Source Code
import java.util.Scanner;
Implement a Java function that calculates the sum of digits for a given
char array consisting of the digits '0' to '9'. The function should return the
digit sum as a long value.
java
SOURCE code
public class DigitSumCalculator {
return sum;
}
Explanation:
1. Function Definition:
o The function calculateDigitSum() takes a char[] (char array) as
input.
o It uses a loop to iterate through each character in the array.
o For each character, it checks if the character is between '0'
and '9' using simple character comparison.
o If the character is a valid digit, it converts it to its numeric
value by subtracting '0' from the character (which gives the
corresponding numeric value of the digit).
o The result is accumulated in the sum variable, which is of type
long to handle large sums.
2. In the main() method:
o An example char[] array is created with the digits '1' to '9' and
'0'.
o The calculateDigitSum() function is called with the array, and the
result is printed.
Sample Output:
python
Copy code
The sum of the digits is: 45