lucproblem007.c (970B)
1 /* Two numbers are entered through the keyboard. Write a program to 2 find the value of one number raised to the power of another */ 3 /* Let Us C, Chap - 5, Page - 84, Problem 5.3 */ 4 5 #include <stdio.h> 6 int main() 7 { 8 double num, result; 9 int power, i = 1; 10 printf("Enter the numbers in 'num^power' format : "); 11 // checking if the input is valid or not 12 if (scanf("%lf^%d", &num, &power) != 2) 13 { 14 printf("\nPlease enter numbers."); 15 return 1; 16 } 17 // result for the negetive input 18 if (power < 0) 19 { 20 printf("\nPlease use a positive number as power."); 21 return 1; 22 } 23 // Hard codded result for input '0' (zero) 24 if (power == 0) 25 { 26 printf("\n%g to the power of %d is : 1", num, power); 27 return 0; 28 } 29 result = num; 30 while (i <= power - 1) 31 { 32 result = result * num; 33 i++; 34 } 35 printf("\n%g to the power of %d is : %g", num, power, result); 36 return 0; 37 }