Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

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.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe