इस उदाहरण में, हम जावा में सरल ब्याज और चक्रवृद्धि ब्याज की गणना करना सीखेंगे।
इस उदाहरण को समझने के लिए, आपको निम्नलिखित जावा प्रोग्रामिंग विषयों का ज्ञान होना चाहिए:
- जावा स्कैनर क्लास
- जावा ऑपरेटर्स
उदाहरण 1: जावा में सरल ब्याज की गणना करें
import java.util.Scanner; class Main ( public static void main(String() args) ( // create an object of Scanner class Scanner input = new Scanner(System.in); // take input from users System.out.print("Enter the principal: "); double principal = input.nextDouble(); System.out.print("Enter the rate: "); double rate = input.nextDouble(); rate = rate/100; System.out.print("Enter the time: "); double time = input.nextDouble(); double interest = (principal * time * rate) / 100; System.out.println("Principal: " + principal); System.out.println("Interest Rate: " + rate); System.out.println("Time Duration: " + time); System.out.println("Simple Interest: " + interest); input.close(); ) )
आउटपुट
मूलधन प्रविष्ट करें: 1000 दर दर्ज करें: 8 समय दर्ज करें: 2 प्रधान: 1000.0 ब्याज दर: 8.0 समय अवधि: 2.0 सरल ब्याज: 160.0
उपरोक्त उदाहरण में, हमने उपयोगकर्ता से इनपुट के रूप में मूलधन , दर और समयScanner
लेने के लिए कक्षा का उपयोग किया है । हम साधारण ब्याज की गणना के लिए साधारण ब्याज के फार्मूले का उपयोग करते हैं।
Simple Interest = (Principal * Rate * Time) / 100
उदाहरण 2: चक्रवृद्धि ब्याज की गणना
import java.util.Scanner; class Main ( public static void main(String() args) ( // create an object of Scanner class Scanner input = new Scanner(System.in); // take input from users System.out.print("Enter the principal: "); double principal = input.nextDouble(); System.out.print("Enter the rate: "); double rate = input.nextDouble(); System.out.print("Enter the time: "); double time = input.nextDouble(); System.out.print("Enter number of times interest is compounded: "); int number = input.nextInt(); double interest = principal * (Math.pow((1 + rate/100), (time * number))) - principal; System.out.println("Principal: " + principal); System.out.println("Interest Rate: " + rate); System.out.println("Time Duration: " + time); System.out.println("Number of Time interest Compounded: " + number); System.out.println("Compound Interest: " + interest); input.close(); ) )
आउटपुट
मूलधन प्रविष्ट करें: 1000 दर दर्ज करें: 10 समय दर्ज करें: 3 बार ब्याज की संख्या दर्ज की गई है: 1 प्रधान: 1000.0 ब्याज दर: 10.0 समय अवधि: 3.0 समय ब्याज की संख्या: 1 चक्रवृद्धि ब्याज: 331.0000000000045
उपरोक्त उदाहरण में, हमने चक्रवृद्धि ब्याज की गणना करने के लिए चक्रवृद्धि ब्याज के सूत्र का उपयोग किया है।
यहां, हमने संख्या की शक्ति की गणना करने के लिए Math.pow () विधि का उपयोग किया है।