Write a Java application that involves the following functio

Write a Java application that involves the following functions: A non-recursive method intPower1(int n, int pow). The first argument is a number that is to be raised to a (non-negative) power, and the second argument is the power. Do not use any methods of class Math. A recursive method intPower2(int n, int pow) with the same functionality as above. Put both methods into one class. The program should ask the user to input the numbers n and pow and output the results. Name the project Hw4_2

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

import java.util.Scanner;

public class Power {
  
int power1(int n,int pow)
{
int res = 1;
  
for(int i=1;i<=pow;i++)
res = res*n;
  
return res;
}

int power2(int n,int pow)
{
  
if(pow==0)
return 1;
  
if(pow==1)
return n;
  
return n*power2(n,pow-1);
}
  
public static void main(String[] args)
{
Power p = new Power();

int n,pow;

Scanner input = new Scanner(System.in);
System.out.println(\"Input the value of n : \");
n = input.nextInt();

System.out.println(\"Input the value of pow : \");
pow = input.nextInt();

System.out.println(\"Result of iterative function is : \" + p.power1(n, pow));
System.out.println(\"Result of recursive function is : \" + p.power2(n, pow));
}
  
}

 Write a Java application that involves the following functions: A non-recursive method intPower1(int n, int pow). The first argument is a number that is to be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site