Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Mathematical Library Methods

Chapter 7

Mathematical Library Methods

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Print Powers of a Number


Write a program that accepts a number x and then prints:

x0, x1, x2, x3, x4, x5

import java.util.Scanner;

 

public class PowersOfX {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Input value

        System.out.print("Enter the value of x: ");

        int x = sc.nextInt();

 

        // Print powers from x^0 to x^5

        System.out.println("Powers of x are:");

        for (int i = 0; i <= 5; i++) {

            System.out.println("x^" + i + " = " + Math.pow(x, i));

        }

 

        sc.close();

    }

}

Output


Sample Input 
Enter the value of x: 2

Sample Output 
Powers of x are:
x^0 = 1.0
x^1 = 2.0
x^2 = 4.0
x^3 = 8.0
x^4 = 16.0
x^5 = 32.0

Explanation

  • Math.pow(x, i) computes x raised to the power i.
  • A for loop runs from 0 to 5 to generate all required powers.
  • x⁰ is always 1 for any non-zero x.