Seu SlideShare está sendo baixado. ×

# Develop a C program that computes X raised to the power n- X^n- recurs.docx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Functions Practice Sheet.docx
Carregando em…3
×

1 de 3 Anúncio

# Develop a C program that computes X raised to the power n- X^n- recurs.docx

Develop a C program that computes X raised to the power n, X^n, recursively. In a nut shell, input: X is a real number and n is an integer.
Program output: X is raised to the power n. Should alert the user if a negative exponent is entered, to enter positive ONLY
Solution
/* C Code to determine computes X raised to the power n using recursive function */
#include <stdio.h>
// recursive power function
double power(double base,int exponent)
{
if(exponent == 0) return 1;
if ( exponent!=1 )
// multiply base exponenet times recursively
return (base*power(base,exponent-1));
}
int main()
{
double X;
int n;

// taking input base and exponent
printf(\"Enter base number(real number): \");
scanf(\"%lf\",&X);
printf(\"Enter exponent(positive integer): \");
scanf(\"%d\",&n);
// checking if exponenet is negative
// the take input again
while(n < 0)
{
printf(\"\ Invalid input\ \");
printf(\"Enter positive exponent ONLY\ \ \");
printf(\"Enter exponent: \");
scanf(\"%d\",&n);
}
// calling the power function
printf(\"%lf^%d = %lf\ \", X, n, power(X, n));
return 0;
}
/*
Output:
Enter base number(real number): 3
Enter exponent(positive integer): 3
3.000000^3 = 27.000000
Enter base number(real number): 10
Enter exponent(positive integer): 2
10.000000^2 = 100.000000
Enter base number(real number): 10.1
Enter exponent(positive integer): 0
10.100000^0 = 1.000000
Enter base number(real number): 101.2
Enter exponent(positive integer): 2
101.200000^2 = 10241.440000
Enter base number(real number): 10.5
Enter exponent(positive integer): -2
Invalid input
Enter positive exponent ONLY
Enter exponent: -2
Invalid input
Enter positive exponent ONLY
Enter exponent: 0
10.500000^0 = 1.000000
*/
.

Develop a C program that computes X raised to the power n, X^n, recursively. In a nut shell, input: X is a real number and n is an integer.
Program output: X is raised to the power n. Should alert the user if a negative exponent is entered, to enter positive ONLY
Solution
/* C Code to determine computes X raised to the power n using recursive function */
#include <stdio.h>
// recursive power function
double power(double base,int exponent)
{
if(exponent == 0) return 1;
if ( exponent!=1 )
// multiply base exponenet times recursively
return (base*power(base,exponent-1));
}
int main()
{
double X;
int n;

// taking input base and exponent
printf(\"Enter base number(real number): \");
scanf(\"%lf\",&X);
printf(\"Enter exponent(positive integer): \");
scanf(\"%d\",&n);
// checking if exponenet is negative
// the take input again
while(n < 0)
{
printf(\"\ Invalid input\ \");
printf(\"Enter positive exponent ONLY\ \ \");
printf(\"Enter exponent: \");
scanf(\"%d\",&n);
}
// calling the power function
printf(\"%lf^%d = %lf\ \", X, n, power(X, n));
return 0;
}
/*
Output:
Enter base number(real number): 3
Enter exponent(positive integer): 3
3.000000^3 = 27.000000
Enter base number(real number): 10
Enter exponent(positive integer): 2
10.000000^2 = 100.000000
Enter base number(real number): 10.1
Enter exponent(positive integer): 0
10.100000^0 = 1.000000
Enter base number(real number): 101.2
Enter exponent(positive integer): 2
101.200000^2 = 10241.440000
Enter base number(real number): 10.5
Enter exponent(positive integer): -2
Invalid input
Enter positive exponent ONLY
Enter exponent: -2
Invalid input
Enter positive exponent ONLY
Enter exponent: 0
10.500000^0 = 1.000000
*/
.

Anúncio
Anúncio

Anúncio

### Develop a C program that computes X raised to the power n- X^n- recurs.docx

1. 1. Develop a C program that computes X raised to the power n, X^n, recursively. In a nut shell, input: X is a real number and n is an integer. Program output: X is raised to the power n. Should alert the user if a negative exponent is entered, to enter positive ONLY Solution /* C Code to determine computes X raised to the power n using recursive function */ #include <stdio.h> // recursive power function double power(double base,int exponent) { if(exponent == 0) return 1; if ( exponent!=1 ) // multiply base exponenet times recursively return (base*power(base,exponent-1)); } int main() { double X; int n; // taking input base and exponent printf("Enter base number(real number): "); scanf("%lf",&X); printf("Enter exponent(positive integer): "); scanf("%d",&n); // checking if exponenet is negative // the take input again while(n < 0) {
2. 2. printf(" Invalid input "); printf("Enter positive exponent ONLY "); printf("Enter exponent: "); scanf("%d",&n); } // calling the power function printf("%lf^%d = %lf ", X, n, power(X, n)); return 0; } /* Output: Enter base number(real number): 3 Enter exponent(positive integer): 3 3.000000^3 = 27.000000 Enter base number(real number): 10 Enter exponent(positive integer): 2 10.000000^2 = 100.000000 Enter base number(real number): 10.1 Enter exponent(positive integer): 0 10.100000^0 = 1.000000 Enter base number(real number): 101.2 Enter exponent(positive integer): 2 101.200000^2 = 10241.440000 Enter base number(real number): 10.5 Enter exponent(positive integer): -2 Invalid input Enter positive exponent ONLY Enter exponent: -2 Invalid input Enter positive exponent ONLY Enter exponent: 0 10.500000^0 = 1.000000
3. 3. */