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

Java 8 Questions With 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)
58 views4 pages

Java 8 Questions With 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
You are on page 1/ 4

Java Programs with Solutions

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

import java.util.Scanner;
public class Product {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int product = a * b;
System.out.println("Product = " + product);
sc.close();
}
}

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

import java.util.Scanner;
public class DigitCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int count = 0;
if (num == 0) count = 1;
while (num != 0) {
num /= 10;
count++;
}
System.out.println("Number of digits: " + count);
sc.close();
}
}

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

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

else
System.out.println("Division by zero is not allowed.");
sc.close();
}
}

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

import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
sc.close();
}
}

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

public class PrimeNumbers {


public static void main(String[] args) {
System.out.println("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)
System.out.println(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) {
System.out.println("Even numbers:");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) System.out.println(i);
}
Java Programs with Solutions

System.out.println("Odd numbers:");
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) System.out.println(i);
}
}
}

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

import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
if (str.equals(reversed))
System.out.println("Palindrome");
else
System.out.println("Not a palindrome");
sc.close();
}
}

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

import java.util.Scanner;
public class MergeArrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of first array: ");
int n1 = sc.nextInt();
int[] a = new int[n1];
System.out.println("Enter elements:");
for (int i = 0; i < n1; i++) a[i] = sc.nextInt();

System.out.print("Enter size of second array: ");


int n2 = sc.nextInt();
int[] b = new int[n2];
System.out.println("Enter elements:");
for (int i = 0; i < n2; i++) b[i] = sc.nextInt();

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];

System.out.println("Merged array:");
Java Programs with Solutions

for (int value : merged) System.out.print(value + " ");


sc.close();
}
}

You might also like