← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 3 of 6

Java के Math Operations

Math operations वे calculations हैं जो Java कर सकता है, adding से लेकर square roots और random numbers तक, एक scientific calculator के buttons जैसा। यह topic सबसे उपयोगी वालों को group करता है।

Basic Arithmetic Operations

Java के basic arithmetic operators (+, -, *, /, %) numeric primitives पर सीधे काम करते हैं, % negative operands के लिए एक true modulo के बजाय remainder देते हुए।

उसी expression में integer और floating-point operands mix करना अपने आप result को एक floating-point type में promote करता है।

उदाहरण: Basic Arithmetic Operations

java
public class Main {
	public static void main(String[] args) {
		System.out.println(10 + 3);
		System.out.println(10 % 3); // remainder
		System.out.println(-10 % 3); // not a true modulo for negatives
	}
}

Absolute and Power Functions

Math.abs() किसी value का sign हटाकर इसकी absolute magnitude पाता है, और Math.pow(base, exp) एक number को एक दी power तक उठाता है, दोनों अपने input types के लिए appropriate results return करते हुए।

उदाहरण: Absolute and Power Functions

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Math.abs(-7));
		// Print a line to the console
		System.out.println(Math.pow(2, 4));
	}
}

Square Root and Rounding

Math.sqrt() एक square root को double के रूप में compute करता है, और इसे Math.round() या casting के साथ combine करना आपको एक clean integer result पाने देता है जब exact square root ज़रूरी न हो।

Math.sqrt() के double result को वापस एक int में cast करना round करने के बजाय किसी भी decimal हिस्से को truncate करता है, जो off-by-one bugs का एक आम source है।

उदाहरण: Square Root and Rounding

java
public class Main {
	public static void main(String[] args) {
		double root = Math.sqrt(20);
		System.out.println(root);
		System.out.println(Math.round(root)); // clean integer result
	}
}

Min and Max Helpers

Math.max() और Math.min() क्रमशः दो values में से बड़ी या छोटी return करते हैं, इस बेहद आम operation के लिए एक manual if-else comparison से बचते हुए।

दोनों methods int, long, float, और double arguments accept करने के लिए overloaded हैं, इसलिए वे Java के numeric types में seamlessly काम करते हैं।

उदाहरण: Min and Max Helpers

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Math.max(4, 9));
		// Print a line to the console
		System.out.println(Math.min(4, 9));
	}
}

Generating Random Numbers

एक random number generate करना आमतौर पर Math.random() (जो 0.0 और 1.0 के बीच एक double return करता है) को scaling और casting के साथ जोड़ता है आपको चाहिए किसी भी integer range के अंदर land करने के लिए।

उदाहरण: Generating Random Numbers

java
public class Main {
	public static void main(String[] args) {
		int random = (int) (Math.random() * 10); // scaled and cast to an int range
		System.out.println(random >= 0 && random < 10);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Integer division उपयोग करना और decimal हिस्सा खोना।
  2. Math.abs(Integer.MIN_VALUE) उपयोग करना, जो अब भी negative है।
  3. यह उम्मीद करना कि Math.random() एक integer return करेगा, जब यह 0.0 से लेकर लेकिन 1.0 शामिल न करते हुए एक double return करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.