0% found this document useful (0 votes)
208 views5 pages

Java Conditional Statement Exercises

The document contains 15 programming problems involving conditional statements and logic in Java. The problems cover topics like determining if a number is positive or negative, solving quadratic equations, finding the greatest of three numbers, determining if a year is a leap year, and more. Test data with expected output is provided for each problem.

Uploaded by

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

Java Conditional Statement Exercises

The document contains 15 programming problems involving conditional statements and logic in Java. The problems cover topics like determining if a number is positive or negative, solving quadratic equations, finding the greatest of three numbers, determining if a year is a leap year, and more. Test data with expected output is provided for each problem.

Uploaded by

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

Conditional Statements

1. Write a Java program to get a number from the user and print whether it is
positive or negative.

Test Data
Input number: 35
Expected Output :
Number is positive

2. Write a Java program to solve quadratic equations (use if, else if and else).

Test Data
Input a: 1
Input b: 5
Input c: 1
Expected Output :
The roots are -0.20871215252208009 and -4.7912878474779195

3. Take three numbers from the user and print the greatest number.

Test Data
Input the 1st number: 25
Input the 2nd number: 78
Input the 3rd number: 87
Expected Output :
The greatest: 87

4. Write a Java program to read the value of an integer m and display the value
of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.

Test Data
Input a number: 25
Expected Output :
The value of m = 25.0
The value of n = 1
5. Write a Java program that keeps a number from the user and generates an
integer between 1 and 7 and displays the name of the weekday.

Test Data
Input number: 3
Expected Output :
Wednesday

6. Write a Java program that reads in two floating-point numbers and tests
whether they are the same up to three decimal places.

Test Data
Input floating-point number: 1256
Input floating-point another number: 3254
Expected Output :
They are different

7. Write a Java program to find the number of days in a month.

Test Data
Input a month number: 2
Input a year: 2016
Expected Output :
February 2016 has 29 days

8. Write a Java program that takes the user to provide a single character from the
alphabet. Print Vowel or Consonant, depending on the user input. If the user
input is not a letter (between a and z or A and Z), or is a string of length > 1, print
an error message.

Test Data
Input an alphabet: p
Expected Output :
Input letter is Consonant

9. Write a Java program that takes a year from user and print whether that year
is a leap year or not.

Test Data
Input the year: 2016
Expected Output :
2016 is a leap year

10. Write a program in Java to display the first 10 natural numbers.

Expected Output :
The first 10 natural numbers are:

1
2
3
4
5
6
7
8
9
10

11. Write a program in Java to display n terms of natural numbers and their sum.

Test Data
Input the number: 2
Expected Output :
Input number:
2
The first n natural numbers are :
2
1
2
The Sum of Natural Number upto n terms :
23

12. Write a program in Java to input 5 numbers from keyboard and find their sum
and average.

Test Data
Input the 5 numbers : 1 2 3 4 5
Expected Output :
Input the 5 numbers :
1
2
3
4
5
The sum of 5 no is : 15
The Average is : 3.0
13. Write a program in Java to display the cube of the number upto given an
integer.

Test Data
Input number of terms : 4
Expected Output :
Number is : 1 and cube of 1 is : 1
Number is : 2 and cube of 2 is : 8
Number is : 3 and cube of 3 is : 27
Number is : 4 and cube of 4 is : 64
14. Write a program in Java to display the multiplication table of a given integer.

Test Data
Input the number (Table to be calculated) : Input number of terms : 5
Expected Output :
5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
15. Write a program in Java to display the n terms of odd natural number and
their sum.

Test Data
Input number of terms is: 5
Expected Output :
The odd numbers are :
1
3
5
7
9
The Sum of odd Natural Number upto 5 terms is: 25

You might also like