Sunday, May 04, 2014

Power Function (X^Y - X Raised to power Y) - C Program

Problem Question


Write a function power ( a, b ), to calculate the value of a raised to b.

Explanation of Problem


It is a simple program wherein the main function shall accept 2 inputs from user, X & Y (for X^Y). The power function should return an int which should be the result of X^Y. Our program should print this result.

Code


#include <stdio.h>

/**@Title: Power Function.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: Code::Blocks 13.12*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 04-05-2014*
*/

int power(int a, int b)
{
 int i, pro = 1;
 for ( i = 0; i < b; i++ )
 {
  pro *= a;
 }
 return pro;
}

int main()
{
 int a, b, ex;
 printf("\n\nEnter 2 numbers: ");
 scanf("%d%d", &a, &b);
 ex = power(a, b);
 printf("\n%d raise to power %d is: %d\n\n", a, b, ex);
 system("pause");
 return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

int power(int a, int b)
{
 int i, pro = 1;
 for ( i = 0; i < b; i++ )
 {
  pro *= a;
 }
 return pro;
}

This is the power function that the problem statement asks us to code. The return type is int since I am returning an integer value, which is the result of the operation a^b where a & b are the arguments to the function 'power'. The for loop calculates the value of a^b. The for loop counter starts from 0 to the value of exponent 'b'. We keep multiplying the number to itself (or the running product) until the counter reaches the value 1 less than b (since we are starting from 0). Now we have our result ready. So we return it to the calling function.

ex = power(a, b); -> Here we have an integer variable 'ex' in which we store the result. I have called the function power with the user input values of a and b and assigned the returned value to variable 'ex'.

We can replace the variable 'ex' in our program. We need 'ex' for storing the result so that we can print it. But we can replace this use by calling the power() function directly in the printf() function, like:
printf("\n%d raise to power %d is: %d\n\n", a, b, power(a, b));

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?