Sunday, April 27, 2025

Java Tutorial - Arithmetic Operations - Simple Calc

This is a Java program that performs arithmetic operations on two float variables x and y.  

public class Main {

	public static void main(String[] args) {
		
		float x = 50;
		float y = 20;
		
		System.out.println("Add: " + (x + y));
		System.out.println("Sub: " + (x - y));
		System.out.println("Mul: " + (x * y));
		System.out.println("Div: " + (x / y));
		
		System.out.println("Mod: " + (x % y));	
		
	}	
}

Here's a line-by-line explanation: 

float x = 50;
float y = 20;

These two lines declare two float variables x and y and initialize them with the values 50 and 20, respectively. 

System.out.println("Add: " + (x + y));
System.out.println("Sub: " + (x - y));
System.out.println("Mul: " + (x * y));
System.out.println("Div: " + (x / y));

These four lines perform addition, subtraction, multiplication, and division on the two variables x and y, and output the results to the console along with a string indicating the operation being performed. 

System.out.println("Mod: " + (x % y));

This line performs the modulo operation on x and y, which gives the remainder of dividing x by y, and outputs the result along with a string indicating the operation being performed.

Modulo operation is a mathematical operation that finds the remainder of the division of one number by another. In Java, the modulo operator is represented by the % symbol.

For example, 11 % 3 would return 2 because 11 divided by 3 is 3 with a remainder of 2.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...