# C Programming Code Examples

## C > Functions Code Examples

### program to find LCM of two numbers using recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/* program to find LCM of two numbers using recursion Write a C program to find LCM of two numbers using recursion. How to find LCM of two numbers in C programming using recursion. Finding LCM using iterative method involves three basic steps: Initialize multiple variable with the maximum value among two given numbers. Check whether multiple clearly divides both number or not. If it does, then end the process and return multiple as LCM. If multiple doesn't divides both given numbers then increment multiple by the max values among both given numbers. Repeat steps 2 to 3 till you find LCM. To convert the above iterative approach of finding LCM into recursive we will use step 2 as our base condition. */ #include <stdio.h> /* Function declaration */ int lcm(int a, int b); int main() { int number1, number2, LCM; /* Input two numbers from user */ printf("Enter any two numbers to find lcm: "); scanf("%d%d", &number1, &number2); /* * Ensures that first parameter of LCM function * is always less than second */ if(number1 > number2) LCM = lcm(number2, number1); else LCM = lcm(number1, number2); printf("LCM of %d and %d = %d", number1, number2, LCM); return 0; } /** * Recursive function to find lcm of two numbers 'a' and 'b'. * Here 'a' needs to be always less than 'b'. */ int lcm(int a, int b) { static int multiple = 0; /* Increments multiple by adding max value to it */ multiple += b; /* * Base condition of recursion * If found a common multiple then return the multiple. */ if((multiple % a == 0) && (multiple % b == 0)) { return multiple; } else { return lcm(a, b); } }

#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:

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.

Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

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,

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

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.

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

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.

C program will let you understand that how to print an array in C. Declare and define one array and then loop upto the length of array. At each iteration we shall print one index...

C Define the union named sample. Declare three variables m, n and ch of different data types. Use the keyword sizeof() to find the size of a union and print the same. Initialize

C Program take a decimal number as input. Divide the input number by 8 and obtain its remainder and quotient. Store the remainder in the array. Repeat the step 2 with quotient

This C Program to Calculates the value of nCr. The algorithm used in this program is nCr = n! /((n-r)!r!) and We need to Find all the Possible combination of the value n&r. A combination

Program sample to copy its input to its output replacing string of one or more blanks by a single blank. String will be terminated if you press ENTER. Press Ctrl-Z & then ENTER

C program code ask the user for operation like insert, delete, display and exit. According to the option entered, access its respective function using switch statement and use the

Program calculates the the sum of A.P series. This C program is used to find the sum of the "Arithmetic Progression Series" and Here A.P stands for Arithmetic Progression. Sequence