Java Questions Codes Outputs
Java Questions Codes Outputs
Hint: The name of the method is: circlemethod. The program accepts only integer values
Code:
public class CircleArea {
public static void circlemethod(int radius) {
double area = Math.PI * radius * radius;
System.out.println("Area: " + area);
}
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter radius (integer): ");
int r = sc.nextInt();
circlemethod(r);
}
}
Output:
Area: 78.5
Code:
public class RandomArray {
public static void main(String[] args) {
double[] arr = new double[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = Math.random();
System.out.println(arr[i]);
}
}
}
Output:
Random 10 double values between 0.0 and 1.0 (output will vary).
Q3: Write a Java program to print the summation (addition), subtraction, multiplication,
Output:
Sum: 154
Subtraction: 106
Multiplication: 3120
Division: 5
Modulo: 10
Q4: Write a Java program that asks the user to input a positive number n and prints the cubic
Code:
public class CubicSeries {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = sc.nextInt();
int i = 1;
while (i <= n) {
System.out.print((i * i * i) + " ");
i++;
}
}
}
Output:
1 8 27 64
Q5: Write a Java program that asks the user to input the temperature and prints one of the
Code:
public class WeatherCheck {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter the temperature: ");
int temp = sc.nextInt();
if (temp > 30)
System.out.println("hot");
else if (temp < 15)
System.out.println("cold");
else
System.out.println("warm");
}
}
Output:
hot
Q6: Write a Java program that asks the user to input a number n and prints the sum and
Code:
public class SumAverage {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt(), sum = 0, i = 1;
while (i <= n) {
sum += i;
i++;
}
double avg = (double) sum / n;
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
}
}
Output:
Enter a number: 5
Sum: 15
Average: 3.0
Code:
public class OddCheck {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n % 2 != 0)
System.out.println("The number is odd.");
else
System.out.println("The number is even.");
}
}
Output:
Enter a number: 3
Q8: Write a program to print all the numbers from 10 to 20 using a for statement.
Code:
public class PrintRange {
public static void main(String[] args) {
for (int i = 10; i <= 20; i++) {
System.out.println(i);
}
}
}
Output:
10
11
12
13
14
15
16
17
18
19
20
Q9: Write a program in Java to print "Computer Science" five times using a do-while
statement.
Code:
public class PrintCS {
public static void main(String[] args) {
int i = 0;
do {
System.out.println("Computer Science");
i++;
} while (i < 5);
}
}
Output:
Computer Science
(printed 5 times)
Q10: Write a program in Java to sum the first 10 natural numbers using a for statement.
Code:
public class SumNatural {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 55
Q11: Write a Java program that uses a for loop to print the multiples of 5.
Code:
public class MultiplesOf5 {
public static void main(String[] args) {
for (int i = 5; i <= 100; i += 5) {
System.out.println(i);
}
}
}
Output:
10
15
...
Q12: Write a Java program to get a number from the user and print whether it is divisible by 5
or not.
Code:
public class DivisibleBy5 {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n % 5 == 0)
System.out.println("Divisible by 5");
else
System.out.println("Not divisible by 5");
}
}
Output:
Enter a number: 15
Divisible by 5
Q13: Write a program to create two variables X and Y as positive integers and check if X is
divisible by Y or not.
Code:
public class DivisibleCheck {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter X: ");
int x = sc.nextInt();
System.out.print("Enter Y: ");
int y = sc.nextInt();
if (x % y == 0)
System.out.println("X is divisible by Y");
else
System.out.println("X is not divisible by Y");
}
}
Output:
Enter X: 10
Enter Y: 2
X is divisible by Y
Q14: Write a program to demonstrate input statement and print the following student details
two times:
- Student Name
- Student ID
Code:
public class StudentDetails {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.print("Enter Student Name: ");
String name = sc.next();
System.out.print("Enter Student ID: ");
int id = sc.nextInt();
System.out.println("Student-Name: " + name);
System.out.println("Student-ID: " + id);
}
}
}
Output:
Student-Name: Ali
Student-ID: 123
(repeated twice)
A. Enter a number
Code:
public class DivAndTable {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n % 2 == 0 && n % 8 == 0)
System.out.println("Even and divisible by 8");
else
System.out.println("Not divisible by 8 or not even");
int i = 1;
do {
System.out.println(n + " x " + i + " = " + (n * i));
i++;
} while (i <= 10);
}
}
Output:
Enter a number: 16
Even and divisible by 8
16 x 1 = 16
...
16 x 10 = 160
Q16: Write a program in Java to print a pattern like a right-angle triangle using stars * and a
for statement.
Example Pattern:
**
***
****
*****
Code:
public class StarPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output:
**
***
****
*****