Ques:-13) Two numbers are entered through the keyboard. Write a program to find the value of one number raised to power of another.

				
					#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int num1,num2,result=1;
printf("enter the two number\n");
scanf("%d\n%d",&num1,&num2);
for(int i=1;i<=num2;i++)
{
result=result*num1;
}
printf("result=%d",result);
getch();
}
				
			

Out Put

				
					Enter the two number
2
4
resul=16
				
			

Another Example

Below is a simple C program that takes two numbers as input and computes the first number raised to the power of the second number.

				
					#include <stdio.h>
#include <math.h>

int main() {
    double base, exponent, result;

    // Taking input from the user
    printf("Enter the base number: ");
    scanf("%lf", &base);
    printf("Enter the exponent: ");
    scanf("%lf", &exponent);

    // Calculating the power
    result = pow(base, exponent);

    // Displaying the result
    printf("%.2lf raised to the power of %.2lf is %.2lf\n", base, exponent, result);

    return 0;
}

				
			

Explanation:

  1. Include necessary headers:

    • #include <stdio.h> for standard input/output functions.
    • #include <math.h> for the pow function which is used to calculate the power.
  2. Variable declaration:

    • double base, exponent, result; – These variables store the base number, the exponent, and the result of the calculation respectively.
  3. Taking user input:

    • scanf function is used to take two double inputs from the user for the base and exponent.
  4. Calculating the power:

    • The pow function from the math.h library is used to calculate base raised to the power of exponent.
  5. Displaying the result:

    • printf function is used to display the result with two decimal places.

Sample Output:

				
					Enter the base number: 2
Enter the exponent: 3
2.00 raised to the power of 3.00 is 8.00

				
			

This program will correctly compute and display the result of raising one number to the power of another using the pow function from the standard math library in C.

Scroll to Top