0% found this document useful (0 votes)
24 views4 pages

Java 8 Assignment Solutions

The document contains a series of Java programming exercises with solutions, including programs to find the product of two numbers, count digits, perform basic calculations, compute factorials, print prime numbers, list odd and even numbers, check for palindromes, and merge two arrays. Each program includes code snippets and explanations of their functionality. The solutions are designed for educational purposes to help users learn Java programming.

Uploaded by

piyushinarayan05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Java 8 Assignment Solutions

The document contains a series of Java programming exercises with solutions, including programs to find the product of two numbers, count digits, perform basic calculations, compute factorials, print prime numbers, list odd and even numbers, check for palindromes, and merge two arrays. Each program includes code snippets and explanations of their functionality. The solutions are designed for educational purposes to help users learn Java programming.

Uploaded by

piyushinarayan05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programs with Solutions

Q1. Write a Java program to find the product of two numbers entered by the user.

import [Link];
public class Product {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int product = a * b;
[Link]("Product = " + product);
[Link]();
}
}

Q2. Write a Java program to count the number of digits in a given number.

import [Link];
public class DigitCount {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int count = 0;
if (num == 0) count = 1;
while (num != 0) {
num /= 10;
count++;
}
[Link]("Number of digits: " + count);
[Link]();
}
}

Q3. Write a Java program to perform basic calculations on two numbers.

import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double a = [Link]();
[Link]("Enter second number: ");
double b = [Link]();
[Link]("Addition = " + (a + b));
[Link]("Subtraction = " + (a - b));
[Link]("Multiplication = " + (a * b));
if (b != 0)
[Link]("Division = " + (a / b));
Java Programs with Solutions

else
[Link]("Division by zero is not allowed.");
[Link]();
}
}

Q4. Write a Java program to compute the factorial of a given number.

import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
[Link]("Factorial = " + fact);
[Link]();
}
}

Q5. Write a Java program to print prime numbers from 1 to 10.

public class PrimeNumbers {


public static void main(String[] args) {
[Link]("Prime numbers from 1 to 10:");
for (int i = 2; i <= 10; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
[Link](i);
}
}
}

Q6. Write a Java program to list odd and even numbers from 1 to 10.

public class OddEven {


public static void main(String[] args) {
[Link]("Even numbers:");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) [Link](i);
}
Java Programs with Solutions

[Link]("Odd numbers:");
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) [Link](i);
}
}
}

Q7. Write a Java program to check if a given string is a palindrome.

import [Link];
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
}
if ([Link](reversed))
[Link]("Palindrome");
else
[Link]("Not a palindrome");
[Link]();
}
}

Q8. Write a Java program to merge two arrays into one combined array.

import [Link];
public class MergeArrays {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of first array: ");
int n1 = [Link]();
int[] a = new int[n1];
[Link]("Enter elements:");
for (int i = 0; i < n1; i++) a[i] = [Link]();

[Link]("Enter size of second array: ");


int n2 = [Link]();
int[] b = new int[n2];
[Link]("Enter elements:");
for (int i = 0; i < n2; i++) b[i] = [Link]();

int[] merged = new int[n1 + n2];


for (int i = 0; i < n1; i++) merged[i] = a[i];
for (int i = 0; i < n2; i++) merged[n1 + i] = b[i];

[Link]("Merged array:");
Java Programs with Solutions

for (int value : merged) [Link](value + " ");


[Link]();
}
}

Common questions

Powered by AI

To check if a string is a palindrome in Java, the program reads the string and then generates its reverse. This involves looping backwards through the string to build the reversed version. The integral string operation is character extraction using charAt at each index from the end to the start, appending each character to a new string. Then, the original string is compared with the reversed string for equality to determine palindrome status .

A Java program facilitates basic arithmetic operations such as addition, subtraction, multiplication, and division by taking user inputs for two numbers and applying these operations sequentially. The potential outcomes for division operations include a valid result when the second number is non-zero and an error message indicating division by zero if the divisor is zero. The program conditionally checks for divisibility before attempting division to ensure correct and safe operations .

In Java, checking if a single-character or empty string is a palindrome is straightforward. For a single-character string, the reverse will be identical to the original string due to its length, inherently making it a palindrome. An empty string is also considered a palindrome by definition, as reversing it results in another empty string. The program's logic, by nature of its string reversing and comparison, accommodates these cases without special handling. It checks string equality regardless of length, encompassing single-length and null scenarios inherently .

A Java program identifies prime numbers within a range by iterating through each number and checking for divisibility. For each candidate number, it assumes primality and tests divisibility by all preceding numbers starting from 2. If a divisor is found, the number is marked as non-prime. The logic employs a nested loop, where the outer loop progresses through the range and the inner loop checks divisibility for each candidate, stopping early if a divisor is found to optimize performance .

The iterative approach for computing factorials in Java involves initializing a variable at 1 and then using a for loop that multiplies this variable by incremental integers up to the given number. The potential inefficiencies arise from a lack of tail call optimization and the linear increase in operations for higher numbers, which can accumulate to significant computational load and potential overflow if the result exceeds the storage capacity of integer data types .

In a Java program performing division, one must consider the potential for division by zero, which can result in an arithmetic exception. To handle this, the program checks if the divisor is zero before performing the division. If the divisor is zero, an appropriate message such as 'Division by zero is not allowed.' is printed instead of attempting the operation . This prevents runtime errors and ensures the program handles exceptional cases gracefully.

Java calculates the product of two numbers by using the Scanner class to read user input, storing the numbers in integer variables, and multiplying these variables. Key considerations for reading user input include ensuring correct data types and handling potential input errors. The program employs nextInt to read integers and ensures scanner closure to prevent resource leaks .

A Java program can count the number of digits in a given integer by continuously dividing the number by 10 until the number becomes 0, incrementing a counter each time a division occurs. The primary algorithm involves using a loop to repeatedly reduce the number's magnitude. Initially, if the number is 0, count is set to 1; otherwise, a while loop checks if the number isn't 0, divides it by 10, and increments the count variable .

Merging two arrays in Java involves creating a new array whose size is the sum of the sizes of the two input arrays. The algorithm then copies elements from the first array into the new array, followed by copying elements from the second array. This is achieved using two for loops: the first loop populates the new array with elements of the first array, and the second loop continues from where the first loop left off, inserting elements from the second array .

A Java program lists odd and even numbers within a range by iterating through each integer in the range and applying a modulus operation to determine parity. If a number modulo 2 equals zero, it is even; if not, it is odd. This logic drives the distinction and is implemented within separate loops or conditional branches that append odd and even numbers onto corresponding outputs .

You might also like