When you start learning Python, one of the first things you need to learn is how to display information on the screen. For example, you might want to display: A welcome message. Your name and age. The result of a mathematical calculation. Student marks and results. Information generated by a Python program. Python provides a built-in function called print() for displaying information. The print() function is one of the simplest and most frequently used functions in Python. It helps programmers display information while testing programs, solving problems, and developing applications. In this article, we will learn how to use print() with practical examples and their outputs. 1. What Is the print() Function in Python? The print() function is a built-in Python function that displays information in the output area, usually the terminal or console. Let's look at a simple example. print("Hello, World!") Output: Hello, World! In this example, Python displays the text Hello,...
🧾 Program 1: Using Math.pow() Function
// Java program to calculate power of a number using Math.pow()
import java.util.Scanner;
public class PowerCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input from user
System.out.print("Enter the base number: ");
double base = sc.nextDouble();
System.out.print("Enter the exponent: ");
double exponent = sc.nextDouble();
// Calculating power using Math.pow()
double result = Math.pow(base, exponent);
// Displaying the result
System.out.println(base + " raised to the power " + exponent + " is: " + result);
}
}
🧮 Example Output
Enter the base number: 2
Enter the exponent: 5
2.0 raised to the power 5.0 is: 32.0
⚙️ Explanation
-
Math.pow(base, exponent)is a built-in Java method that calculates base^exponent. -
It returns a double value.
-
Example:
Math.pow(2, 3)→ 8.0
🧾 Program 2: Using Loop (Without Math.pow())
// Java program to calculate power of a number without using Math.pow()
import java.util.Scanner;
public class PowerCalculatorLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base number: ");
int base = sc.nextInt();
System.out.print("Enter the exponent: ");
int exponent = sc.nextInt();
long result = 1;
// Using for loop to calculate power
for (int i = 1; i <= exponent; i++) {
result *= base;
}
System.out.println(base + " raised to the power " + exponent + " is: " + result);
}
}
🧮 Example Output
Enter the base number: 3
Enter the exponent: 4
3 raised to the power 4 is: 81
🧠 Explanation
-
This program multiplies the base number repeatedly.
-
If exponent = 4 and base = 3 →
3 × 3 × 3 × 3 = 81
🧩 Program 3: Using Recursion
// Java program to calculate power using recursion
import java.util.Scanner;
public class PowerRecursion {
static long power(int base, int exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base number: ");
int base = sc.nextInt();
System.out.print("Enter the exponent: ");
int exponent = sc.nextInt();
long result = power(base, exponent);
System.out.println(base + " raised to the power " + exponent + " is: " + result);
}
}
🧮 Example Output
Enter the base number: 5
Enter the exponent: 3
5 raised to the power 3 is: 125
🏁 Conclusion
-
✅ Use
Math.pow()for quick calculations. -
✅ Use a loop if you want to understand how powers work internally.
-
✅ Use recursion for practicing function calls.
Comments
Post a Comment