Slide 1
Slide 2
Slide 3

Float data type in java programming (V13)

No Comments

 

float Data Types

        The float data type represents a 32-bit IEEE 754 floating-point number. It is particularly useful for saving memory in large arrays of floating-point numbers.
The default value of a float variable is 0.0f.
However, it is important to note that the float data type is not suitable for precise values, such as currency, because of rounding errors that can occur in floating-point arithmetic.

Syntax:

            

Source Code:

Copied!
public class ScoreCalculation {
    public static void main(String[] args) {
        // Declaring float variables for scores in different subjects
        float mathScore = 85.5f;
        float scienceScore = 90.0f;
        float englishScore = 78.5f;
        float historyScore = 88.0f;

        // Calculating the total score
        float totalScore = mathScore + scienceScore + englishScore + historyScore;

        // Calculating the average score
        float averageScore = totalScore / 4; // Dividing by the number of subjects (4)

        // Displaying the total and average scores
        System.out.println("Total Score: " + totalScore);
        System.out.println("Average Score: " + averageScore);
    }
}

Result:

Watch the video:

Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top