Practical 1
1. Write a Java program that takes a number as input and
prints its multiplication table upto 10.
Source Code
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = sc.nextInt();
// Print the multiplication table for the entered number
System.out.println("Multiplication table for " + number + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
}
// Close the scanner
scanner.close();
}
}
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++) {
System.out.print("*");
}
// Move to the next line after each row
System.out.println();
}
}
}
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
System.out.println() 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 java.util.Scanner;
public class Circle {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner sc = new Scanner(System.in);
// Prompt the user to enter the radius of the circle
System.out.print("Enter the radius of the circle: ");
double radius = sc.nextDouble();
// Calculate the area and perimeter (circumference) of the circle
double area = Math.PI * radius * radius; // Area formula: πr^2
double perimeter = 2 * Math.PI * radius; // Perimeter (Circumference)
formula: 2πr
// Print the results
System.out.println("Area of the circle: " + area);
System.out.println("Perimeter (Circumference) of the circle: " + perimeter);
// Close the scanner
scanner.close();
}
}
Explanation:
1. Scanner Object: We use the Scanner class to take input
from the user, specifically the radius of the circle.
2. Math.PI: The value of Pi (π) is accessed using Math.PI.
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 java.util.Scanner;
public class BinaryAddition {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter two binary numbers
System.out.print("Enter first binary number: ");
String binary1 = scanner.nextLine();
System.out.print("Enter second binary number: ");
String binary2 = scanner.nextLine();
// Convert the binary numbers to decimal
int num1 = Integer.parseInt(binary1, 2);
int num2 = Integer.parseInt(binary2, 2);
// Add the numbers
int sum = num1 + num2;
// Convert the sum back to binary
String binarySum = Integer.toBinaryString(sum);
// Display the result
System.out.println("Sum in binary: " + binarySum);
// Close the scanner
scanner.close();
}
}
Explanation for (a):
1. We take two binary numbers as input using Scanner.
2. Convert them from binary to decimal using
Integer.parseInt(binary, 2).
3. Add the two numbers.
4. Convert the result back to binary using
Integer.toBinaryString(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 java.util.Scanner;
public class DecimalBinaryConversion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Convert decimal to binary
System.out.print("Enter a decimal number: ");
int decimal = scanner.nextInt();
String binary = Integer.toBinaryString(decimal);
System.out.println("Decimal " + decimal + " in binary is: " + binary);
// Convert binary to decimal
System.out.print("Enter a binary number: ");
String binaryInput = scanner.next();
int decimalFromBinary = Integer.parseInt(binaryInput, 2);
System.out.println("Binary " + binaryInput + " in decimal is: " +
decimalFromBinary);
scanner.close();
}
}
Explanation for (b):
1. Decimal to Binary: The Integer.toBinaryString(decimal) method
is used to convert a decimal number to binary.
2. Binary to Decimal: The Integer.parseInt(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 java.util.Scanner;
public class StringReversal {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print("Enter a string: ");
String str = scanner.nextLine();
// Reverse the string using StringBuilder's reverse() method
String reversedString = new StringBuilder(str).reverse().toString();
// Display the reversed string
System.out.println("Reversed string: " + reversedString);
// Close the scanner
scanner.close();
}
}
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 {
System.out.println("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);
System.out.println("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