Write a Java program to find the sum and difference between 25 and 16 using variables in different lines.


Calculating Sum and Difference of Two Numbers

Welcome to the world of Java programming! Today, we'll walk through a simple yet fundamental example: Writing a Java program to calculate the sum and difference between two numbers. This is a great starting point for beginners, and we'll break down each step so it's easy to understand.

What We'll Cover:

  • The Algorithm: Our Plan of Action
  • The Java Code: Step-by-Step Explanation
  • Running the Program and Seeing the Output
  • Variable Descriptions: Understanding the Building Blocks
The Algorithm: Our Plan of Action

1. Declare two integer variables, num1 and num2, and initialize them with 25 and 16 respectively.
2. Declare an integer variable sum to store the sum of num1 and num2.
3. Calculate the sum: sum = num1 + num2.
4. Declare an integer variable difference to store the difference between num1 and num2.
5. Calculate the difference: difference = num1 - num2.
6. Print the value of sum.
7. Print the value of difference.

The Java Code: Step-by-Step Explanation

public class BrightXLearn {
    public static void main(String[] args) {
        // Declare and initialize the first number
        int num1 = 25;
        // Declare and initialize the second number
        int num2 = 16;
        // Declare a variable to store the sum
        int sum;
        // Calculate the sum
        sum = num1 + num2;
        // Declare a variable to store the difference
        int difference;
        // Calculate the difference
        difference = num1 - num2;
        // Print the sum
        System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
        // Print the difference
        System.out.println("The difference between " + num1 + " and " + num2 + " is: " + difference);
    }
}

Write a program to find the sum and difference between 25 and 16 using variables in different lines.
Let's break down the code:

  • public class BrightXLearn: This line declares a class named BrightXLearn. In Java, everything is organized into classes.
  • public static void main(String[] args): This is the main method, the starting point of our program. The code inside this method will be executed when we run the program.
  • int num1 = 25;: This line declares an integer variable named num1 and assigns it the value 25. int tells Java we're working with whole numbers.
  • int num2 = 16;: Similarly, this declares an integer variable num2 and assigns it the value 16.
  • int sum;: This declares an integer variable named sum, but we don't assign a value to it yet.
  • sum = num1 + num2;: This line calculates the sum of num1 and num2 and stores the result in the sum variable.
  • int difference;: Declares an integer variable named difference.
  • difference = num1 - num2;: Calculates the difference between num1 and num2 and stores the result in the difference variable.
  • System.out.println(...): These lines print the results to the console. System.out.println() is a built-in Java command for displaying output.

Running the Program and Seeing the Output

To run this program, you'll need a Java Development Kit (JDK) installed on your computer. You can then save the code in a file named BrightXLearn.java and compile it using the command javac BrightXLearn.java. After successful compilation, you can run the program using the command java BrightXLearn.


You should see the following output on your console:
The sum of 25 and 16 is: 41
The difference between 25 and 16 is: 9

Variable Descriptions: Understanding the Building Blocks

  • num1: An integer variable that stores the first number (25).
  • num2: An integer variable that stores the second number (16).
  • sum: An integer variable that stores the sum of num1 and num2.
  • difference: An integer variable that stores the difference between num1 and num2.

Conclusion

Congratulations! You've just written your Java program. This simple example demonstrates fundamental concepts like variables, operators, and output. As you continue learning Java, you'll build upon these basics to create more complex and powerful programs. Keep practicing, and you'll be a Java programmer in no time!

Support Me


Comments