C Programming Code Examples
C > Functions Code Examples
C program to find power of a number using recursion
/* C program to find power of a number using recursion
Write a C program to input a number from user and find power of given number using recursion. How to find power of a number using recursive function in C programming.
First give a meaningful name to our recursive function say pow().
The function must accept two numbers i.e. base and exponent and calculate its power. Hence, take two parameters for base and exponent, say pow(double base, int exponent);.
Finally the function should return base ^ exponent i.e. a double type value.
After declaring pow() function its time to define logic to find power recursively. There can be three cases while calculating power of a number.
If exponent is 0, then power is 1. This is the base condition of our recursive function.
If exponent is negative, then power is 1 / (x ^ -y). Which uses recursive call to pow() function for computing the value of (x ^ -1) i.e. 1 / pow(base, -expo).
If exponent is positive, then calculate power normally using x ^ y. Computing x ^ y also makes a recursive call i.e. base * pow(base, expo - 1) */
#include <stdio.h>
/* Power function declaration */
double pow(double base, int expo);
int main()
{
double base, power;
int expo;
/* Input base and exponent from user */
printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%d", &expo);
// Call pow function
power = pow(base, expo);
printf("%.2lf ^ %d = %f", base, expo, power);
return 0;
}
/**
* Calculate power of any number.
* Returns base ^ expo
*/
double pow(double base, int expo)
{
/* Base condition */
if(expo == 0)
return 1;
else if(expo > 0)
return base * pow(base, expo - 1);
else
return 1 / pow(base, -expo);
}
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions. A function can also be referred as a method or a sub-routine or a procedure, etc.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Raise to power. Returns base raised to the power exponent: baseexponent. The function pow() is used to calculate the power raised to the base value. It takes two arguments. It returns the power raised to the base value. It is declared in "math.h" header file.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Graphic program about Mine Sweeper. Put mines in random blocks. Put mines value in other blocks. Sets view port to the play area only. Puts F tag in Array. Determine current
Program to illustrates reading of data from a file. Program to opens a file which is present. Once the file opens successfully, it uses libc fgetc() library call to read the content. Code
C Program calculate length of string without using strlen() function. You can use standard library function 'strlen()' to find the length of a string, this C program computes the length
Take a string and a substring as input. Store it in the arrays 'str[] & str1[]' respectively. Using For Loop to 'compare' str1[] with the str[]. Do step2 until the end of main string. During the
C Program code finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program accepts an MxN matrix. Then adds main diagonal of matrix as well as the
Take the Number which you have to Reverse as the input. Obtain its quotient & remainder. Multiply the Separate variable with 10 & add the obtained remainder to it. Do step 2 again
Open the file and point it to the file pointer fpoint1. Initialize the variables line_count, n_o_c_l, n_o_n_b_l, n_o_b_l, n_e_c to zero. Using while loop read the next line character