0% found this document useful (0 votes)
15 views2 pages

Java Programs for Basic Data Types

Uploaded by

Subrata Manna
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)
15 views2 pages

Java Programs for Basic Data Types

Uploaded by

Subrata Manna
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

Data-types

1. Write a java program to print an int, a double and a char on screen.


2. Write a program to print the area of a Triangle of height and base 5 and 19
units respectively.
3. Print the ASCII value of the character 'G'.
4. Write a program to assign a value of 20.235 to a double variable and then
convert it to int.
5. Write a program to add 10 to the ASCII value of the character ’q’ and print
the equivalent character
6. Write a program to add an integer variable having value 16 and a double
variable having value 3.2.
7. Write a program to find the cube of the number 3.4.
Input by user
1. Write a program to enter the values of two variables 'a' and 'b'
from keyboard and then check if both the conditions 'a < 50' and 'a <
b' are true.
2. Write a program to find square of a number.
E.g.-
INPUT : 3 OUTPUT : 9
INPUT : 5 OUTPUT : 25
3. Take two different string input and print them in same line. E.g.-
INPUT : <First name>
<Last name>
OUTPUT : <Firstname><Lastname>

Decide if or else
1. Take values of length and breadth of a rectangle from user and check if it is
square or not.
2. Take two int values from user and print greatest among them.
3. A shop will give discount of 10% if the cost of purchased quantity is more
than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
4. A company decided to give bonus of 5% to employee if his/her year of
service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.
5. A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.

6. Take input of age of 3 people by user and determine oldest and youngest
among them.

7. Write a program to print absolute value of a number entered by user. E.g.-


INPUT: 1 OUTPUT: 1
INPUT: -1 OUTPUT: 1

8. A student will not be allowed to sit in exam if his/her attendance is less than
75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.

9. Modify the above question to allow student to sit if he/she has medical
cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print
accordingly.
10. If
x=2
y=5
z=0
then find values (Yes/NO) of the following expressions:
a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)

[Link] a program to check whether a entered character is lowercase ( a to z )


or uppercase ( A to Z ).

Common questions

Powered by AI

A Java program evaluates exam eligibility by first calculating attendance percentage using `attendedClasses / totalClasses * 100`. If this is below 75%, the student is flagged as ineligible, unless identified medical excuses permit otherwise. Users input medical conditions, with 'Y' allowing eligibility despite poor attendance. This dual-conditional logic requires accurate calculations and robust user input handling, ensuring all logical paths - absolute attendance, and medical excuse scenarios - are effectively covered to make a correct determination .

Checking conditions such as 'a < 50' and 'a < b' in a Java program is crucial for conditional logic and decision-making. These conditions determine program flow by evaluating if a variable satisfies specific criteria before executing certain blocks of code. If both conditions are true, then related conditional statements execute; otherwise, alternate paths might be taken. This allows dynamic handling of input scenarios, ensuring that program responses cater to different user inputs or situations .

To print the ASCII value of a character in Java, you can directly cast the character to an integer. For instance, to find the ASCII value of 'G', use the expression `(int)'G'`. This will output the value 71, since 'G' corresponds to ASCII 71 in the ASCII table. This casting effectively converts the character to its underlying numerical code .

To add 10 to the ASCII value of a character such as 'q' in Java, you would first convert 'q' to its ASCII integer value, add 10, and then cast back to a character. The ASCII of 'q' is 113, adding 10 results in 123, which converts to the character '}'. This code snippet achieves this: `char result = (char) ('q' + 10);` which would print '}' .

A Java program can check if a shape is a square by comparing its length and breadth. If both dimensions are equal, then the shape is a square. The program takes two integer inputs from the user for length and breadth, and uses a conditional statement: `if (length == breadth)`. If the condition is true, it indicates the shape is a square; otherwise, it is not. This logical comparison ensures that only when both dimensions match does the program confirm the shape as a square .

Java programs can calculate bonuses by assessing the employee's service years using conditional statements. If an employee has more than five years of service, a bonus, which could be calculated as `salary * 0.05`, is awarded. The program must correctly calculate the bonus based on accurate integer and float handling, ensuring no truncation or rounding errors. Logic should accommodate inputs at boundary values, such as exactly five years, to securely handle service comparisons and prevent erroneous bonus allocations .

To determine the greatest of two integers in Java, a program can use the `if-else` conditional structure. The program compares the integers using `if (a > b)`, printing 'a' if true, and 'b' otherwise. Edge cases involve both integers being equal, which the program should handle to avoid erroneous output by adding an additional check such as `if (a == b)`, informing the user both numbers are equal. Handling these ensures accurate results and appropriate responses .

The grading system in Java categorizes marks using a sequential conditional logic. Marks are input and checked against predefined ranges, such as: below 25 for 'F', 25 to 45 for 'E', 45 to 50 for 'D', etc., using `if-else if` structures. An incorrect implementation of these ranges could lead to misgraded outputs, confusing users or leading to unexpected processing, especially if boundary checks overlap or are missed. Consequently, it’s vital to ensure exclusive and comprehensive boundary coverage to maintain system fidelity .

The cube of a number can be calculated by multiplying the number by itself twice. For a floating-point number like 3.4, the formula in Java would be `Math.pow(3.4, 3)`, which uses Java's Math library. Alternatively, using `3.4 * 3.4 * 3.4` achieves the same result. The calculation yields approximately 39.304 as the cube of 3.4 .

In Java, when adding an integer to a double, Java performs implicit casting where the integer is converted to a double for the computation. In the case of adding 16 to 3.2, the integer 16 is cast to a double, becoming 16.0, and then the addition is performed. The result is 19.2 as both numbers are now treated as doubles, preserving any decimal precision .

You might also like