Java Exercises: 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 32bit 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.

Java: Tips of the Day

Java: ConvertInputStreamToString

Converts InputStream to a String.

public static String convertInputStreamToString(final InputStream in) throws IOException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = in.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    return result.toString(StandardCharsets.UTF_8.name());

Ref: https://bit.ly/2N1GDss