Here is a Java Program to Calculate the Power and Square Root of a Given Number
Output of Above Java Program
Enter the value of exponent
5
Enter the value for base
8
the value for power fun:-> 32768.0
the value for square this is sqr of base:-> 64.0
import java.util.Scanner; class power_sqrt { public static void main(String a[]){ int power; float base; Scanner sc=new Scanner(System.in); System.out.println("Enter the value of exponent"); power=sc.nextInt(); System.out.println("Enter the value for base"); base=sc.nextFloat(); number pow=new number(); number sqr=new number(); System.out.println("the value for power fun:-> "+pow.power(base, power)); System.out.println("the value for square this is sqr of base:-> "+sqr.squre(base)); } } class number{ float base; int expo; float power(float a,int b){ if(b==1) return a; if(b==0) return 1; else return(a*power(a,b-1)); } float squre(float base){ return base*base; } }
Output of Above Java Program
Enter the value of exponent
5
Enter the value for base
8
the value for power fun:-> 32768.0
the value for square this is sqr of base:-> 64.0