Write a program to find x to the power n (i.E. X^n). Take x and n from the user. Y
Answers
import java.util.*;
class program
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter x and n");
int x=sc.nextInt();
int n=sc.nextInt();
System.out.println("Here we gooooo : "+Math.pow(x,n));
}
}
Here is the C program's source code for computing the value of X N. The compilation of the C programme is successful.
/*
C programme that takes X and N as inputs will calculate the value of X N.
*/
#include <stdio.h>
#include <math.h>
long int power(int x, int n);
void main()
{
long int x, n, xpown;
printf("Enter the values of X and N \n");
scanf("%ld %ld", &x, &n);
xpown = power(x, n);
printf("X to the power N = %ld\n", xpown);
}
/* Recursive function to computer the X to power N */
long int power(int x, int n)
{
if (n == 1)
return(x);
else if (n % 2 == 0)
/* if n is even */
return (pow(power(x, n/2), 2));
else
/* if n is odd */
return (x * power(x, n - 1));
}
#SPJ2