0% found this document useful (0 votes)
11 views

Java Questions Codes Outputs

The document contains a series of Java programming exercises that cover various topics such as calculating the area of a circle, generating random numbers, performing arithmetic operations, and user input handling. Each exercise includes the code implementation along with sample outputs demonstrating the expected results. The exercises range from basic concepts like loops and conditionals to more complex tasks like generating patterns and checking divisibility.

Uploaded by

elanAli1
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)
11 views

Java Questions Codes Outputs

The document contains a series of Java programming exercises that cover various topics such as calculating the area of a circle, generating random numbers, performing arithmetic operations, and user input handling. Each exercise includes the code implementation along with sample outputs demonstrating the expected results. The exercises range from basic concepts like loops and conditionals to more complex tasks like generating patterns and checking divisibility.

Uploaded by

elanAli1
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/ 9

Q1: Write a Java method to calculate the area of a circle.

Hint: The name of the method is: circlemethod. The program accepts only integer values

from the user.

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:

Enter radius (integer): 5

Area: 78.5

Q2: Write a program to generate 10 random elements in a double array.

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,

division, and modulo of the two numbers 130 and 24.


Code:
public class Arithmetic {
public static void main(String[] args) {
int a = 130, b = 24;
System.out.println("Sum: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Modulo: " + (a % b));
}
}

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

series up to the given number using a while statement:

Example Output: 1, 8, 27, 64, ...

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:

Enter a positive number: 4

1 8 27 64
Q5: Write a Java program that asks the user to input the temperature and prints one of the

following using if statement:

- Print "hot" if the weather is greater than 30.

- Print "cold" if the weather is less than 15.

- Print "warm" otherwise.

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:

Enter the temperature: 32

hot

Q6: Write a Java program that asks the user to input a number n and prints the sum and

average of the given number using a while statement.

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

Q7: Write a program to check if the entered number is odd or not.

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

The number is odd.

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

...

100 (all multiples of 5 up to 100)

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:

Enter Student Name: Ali

Enter Student ID: 123

Student-Name: Ali

Student-ID: 123

(repeated twice)

Q15: Write a Java program that allows the user to:

A. Enter a number

B. Check if the given integer is even and divisible by 8

C. Display the multiplication table of that integer using a do-while statement.

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:

**

***

****

*****

You might also like