Practical 1
1. Write a Java program that takes a number as input and
prints its multiplication table upto 10.
Source Code
import [Link];
public class MultiplicationTable {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner sc = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int number = [Link]();
// Print the multiplication table for the entered number
[Link]("Multiplication table for " + number + ":");
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number * i));
}
// Close the scanner
[Link]();
}
}
Explanation:
1. The program starts by creating a Scanner object to read
input from the user.
2. The user is asked to input a number, and that number is
stored in the variable number.
3. A for loop is used to print the multiplication table from 1 to
10 for the entered number.
4. After printing the table, the scanner is closed to prevent
resource leakage.
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
2. Write a Java program to display the following pattern.
*****
****
***
**
*
Source Code
public class StarPattern {
public static void main(String[] args) {
// Outer loop to handle the rows
for (int i = 5; i >= 1; i--) {
// Inner loop to print stars in each row
for (int j = 1; j <= i; j++) {
[Link]("*");
}
// Move to the next line after each row
[Link]();
}
}
}
Explanation:
The outer loop runs from 5 down to 1, controlling the
number of rows.
The inner loop prints asterisks (*). The number of asterisks
printed in each row corresponds to the value of i from the
outer loop.
After printing the asterisks for each row, the
[Link]() statement moves the cursor to the next
line.
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 [Link];
public class Circle {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner sc = new Scanner([Link]);
// Prompt the user to enter the radius of the circle
[Link]("Enter the radius of the circle: ");
double radius = [Link]();
// Calculate the area and perimeter (circumference) of the circle
double area = [Link] * radius * radius; // Area formula: πr^2
double perimeter = 2 * [Link] * radius; // Perimeter (Circumference)
formula: 2πr
// Print the results
[Link]("Area of the circle: " + area);
[Link]("Perimeter (Circumference) of the circle: " + perimeter);
// Close the scanner
[Link]();
}
}
Explanation:
1. Scanner Object: We use the Scanner class to take input
from the user, specifically the radius of the circle.
2. [Link]: The value of Pi (π) is accessed using [Link].
3. Formulas:
o Area of the circle = π×radius2\pi \times \
text{radius}^2π×radius2
o Perimeter (Circumference) of the circle =
2×π×radius2 \times \pi \times \
text{radius}2×π×radius
4. The results for both area and perimeter are printed to the
console.
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
a. Java Program to Add Two Binary Numbers:
This program adds two binary numbers that the user inputs and
prints the result in binary format.
import [Link];
public class BinaryAddition {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter two binary numbers
[Link]("Enter first binary number: ");
String binary1 = [Link]();
[Link]("Enter second binary number: ");
String binary2 = [Link]();
// Convert the binary numbers to decimal
int num1 = [Link](binary1, 2);
int num2 = [Link](binary2, 2);
// Add the numbers
int sum = num1 + num2;
// Convert the sum back to binary
String binarySum = [Link](sum);
// Display the result
[Link]("Sum in binary: " + binarySum);
// Close the scanner
[Link]();
}
}
Explanation for (a):
1. We take two binary numbers as input using Scanner.
2. Convert them from binary to decimal using
[Link](binary, 2).
3. Add the two numbers.
4. Convert the result back to binary using
[Link](sum).
5. Print the result in binary format.
Sample Outputs:
a. Binary Addition Program:
Source Code
Enter first binary number: 1011
Enter second binary number: 1101
Sum in binary: 11000
b. Java Program to Convert Decimal Number to Binary
and Vice Versa:
This program converts a decimal number to binary and a binary
number to decimal.
java
Source Code
import [Link];
public class DecimalBinaryConversion {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Convert decimal to binary
[Link]("Enter a decimal number: ");
int decimal = [Link]();
String binary = [Link](decimal);
[Link]("Decimal " + decimal + " in binary is: " + binary);
// Convert binary to decimal
[Link]("Enter a binary number: ");
String binaryInput = [Link]();
int decimalFromBinary = [Link](binaryInput, 2);
[Link]("Binary " + binaryInput + " in decimal is: " +
decimalFromBinary);
[Link]();
}
}
Explanation for (b):
1. Decimal to Binary: The [Link](decimal) method
is used to convert a decimal number to binary.
2. Binary to Decimal: The [Link](binaryInput, 2) method
converts a binary string to a decimal integer.
Decimal 25 in binary is: 11001
Enter a binary number: 11001
Binary 11001 in decimal is: 25
c. Java Program to Reverse a String:
This program takes a string input from the user and reverses it.
java
Source Code
import [Link];
public class StringReversal {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a string
[Link]("Enter a string: ");
String str = [Link]();
// Reverse the string using StringBuilder's reverse() method
String reversedString = new StringBuilder(str).reverse().toString();
// Display the reversed string
[Link]("Reversed string: " + reversedString);
// Close the scanner
[Link]();
}
}
Explanation for (c):
1. We use StringBuilder to reverse the string. The reverse()
method of StringBuilder reverses the string, and we convert
it back to a regular String using toString().
Sample Outputs:
c. String Reversal Program:
c
Source Code
Enter a string: Hello
Reversed string: olleH
Each program performs the respective tasks as per your
request.
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 {
public static long calculateDigitSum(char[] digitArray) {
long sum = 0;
// Loop through each character in the char array
for (char ch : digitArray) {
// Check if the character is a digit (between '0' and '9')
if (ch >= '0' && ch <= '9') {
// Convert the char to its numeric value and add it to the sum
sum += ch - '0';
} else {
[Link]("Invalid character found: " + ch);
}
}
return sum;
}
public static void main(String[] args) {
// Example char array consisting of digits
char[] digits = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
// Calculate and print the sum of digits
long result = calculateDigitSum(digits);
[Link]("The sum of the digits is: " + result);
}
}
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