Java: Compute xn % y where x, y and n are all 32bit integers

Java Basic: Exercise-204 with Solution

Write a Java program to compute xn % y where x, y and n are all 32-bit integers.

Sample Solution:

Java Code:

import java.util.*;
 public class Main 
 public static void main(String[] args)
       Scanner in = new Scanner(System.in);
          System.out.print("Input x : ");
          int x = in.nextInt();  
		      System.out.print("Input n : ");
		      int n = in.nextInt(); 
	      	System.out.print("Input y : ");
	      	int y = in.nextInt(); 
	      	    double result = Math.pow(x, n);
		     double result1 = result % y;
		   System.out.println("x^n % y = " + result1); 

Sample Output:

Input x :  25
Input n :  35
Input y :  45
x^n % y = 5.0


Flowchart: Java exercises: Compute  x<sup>n</sup> % y where x, y and n are all 32bit integers.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to find the contiguous subarray of given length k which has the maximum average value of a given array of integers. Display the maximum average value.
Next: Write a Java program to check whether an given integer is power of 2 or not using O(1) time.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.