Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Recursion Code Examples

Program to find Product of 2 Numbers using Recursion

/* Program to find Product of 2 Numbers using Recursion This C program using recursion, finds the product of 2 numbers without using the multiplication operator. */ #include <stdio.h> int product(int, int); int main() { int x, y, result; printf("Enter two numbers to find their product: "); scanf("%d%d", &x, &y); result = product(x, y); printf("Product of %d and %d is %d\n", x, y, result); return 0; } int product(int x, int y) { if (x < y) { return product(y, x); } else if (y != 0) { return (x + product(x, y - 1)); } else { return 0; } }

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.

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

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.

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,

C program search for an element in the linked list using recursion. Uses recursive function & search for an element in a Linked List. Linked List is an Ordered set of Data Elements, each

Define our Recursive Function to print array elements & say printArray(int arr[], int start, int len). The function takes three parameters where first is array to print, second is starting




Program to input a number and calculate its factorial using for loop. Factorial of a number n is product of all positive integers less than or equal to n. It is denoted as n!. ex. 5! = 120

A magic square is a simple mathematical game developed during the 1500. Square is divided into equal number of rows and columns. Start filling each square with the

C program to read elements in a matrix and find transpose of the given matrix. How to find transpose of a given matrix in C. Input elements in matrix A from user. Declare...

Program Input number and nth bit position to clear from user. Store it in some variable say j and n. Left shift 1, n times i.e. 1 << n. Perform bitwise complement with the above result...

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

C programming string function strlen returns the "length of the string". Program code print the Length of the string. It returns the length of the string without including 'end character'

Program code to find equivalent resistance of parallel combination of resistive circuits. Enter the number of resistances. Enter value of each resistance. Printing equivalent series