 # C Programming Code Examples

## C > Functions Code Examples

### C program to print even or odd numbers in given range using recursion

``` C program to print even or odd numbers in given range using recursion Write a recursive function in C programming to print all even or odd numbers between 1 to n. How to print all even numbers in given range using recursion in C programming. First give a meaningful name to the recursive function to print even odd numbers. Let's say printEvenOdd(). This function can print both even as well as odd numbers in given range. Next the function must accept two inputs i.e. the current number to print and the upper limit. Hence, update the function declaration to printEvenOdd(int cur, int limit);. Finally, the function prints all even or odd numbers in given range and returns void. So, the final function declaration to print even or odd numbers is - void printEvenOdd(int cur, int limit);. Printing either even or odd numbers have same logic. Starting from a seed value increment the current number by 2 to get next value. When the current number exceeds the upper limit to print then terminate from function. Which is our required base condition to exit control from function. If the current number is less than upper limit than print the current number and recursively call the printEvenOdd() with a new value of cur i.e. printEvenOdd(cur + 2, limit);. #include <stdio.h> /* Function declaration */ void printEvenOdd(int cur, int limit); int main() { int lowerLimit, upperLimit; // Input lower and upper limit from user printf("Enter lower limit: "); scanf("%d", &lowerLimit); printf("Enter upper limit: "); scanf("%d", &upperLimit); printf("Even/odd Numbers from %d to %d are: ", lowerLimit, upperLimit); printEvenOdd(lowerLimit, upperLimit); return 0; } void printEvenOdd(int cur, int limit) { if(cur > limit) return; printf("%d, ", cur); // Recursively call to printEvenOdd to get next value printEvenOdd(cur + 2, limit); } ```

C Add Two Numbers using Command Line - Write a C Program. Open 'command prompt' inside Borland C/C++ and Click on DOS Shell. Inside command prompt type this command. Type "exit" command to return to C++ Screen

C++ Program Displays Factors of Number - Find all 'factors' of an integer using for loop & if statement. Takes a positive integer from an user & displays all the factors of that number. In this program, an integer entered by user is

C Program Compare Strings Using StrCmp - C program to compare two strings using loop character by character. Logic to compare two strings. Input two strings from user and Store it in some variable say str1 and str2. Compare

C Programming Codes Displaying a Table - C Language code how to display a table in C programming language is more or less similar to that of counting. We use only one iteration and increment it with value of which table is

Program to Pick and Print a random value - Picks a random value withing range low-high. Value returned seeds the rand() function. Print version information and exit. First seed the random function. Add line, to the array...

C Code Reads a Number of Objects by Size - Reads a number of objects by size and stores them in buf. C programming code returns the number of items actually read. Get open a file and read. If there is an error give a message.

Program Find the Length of the Linked list - C Program to Find the length of the linked list using Recursion. This program uses Recursive Function and calculates the length of a string. The user enters a string to find it's length. List

Code Find Sum of lower Triangular matrix - C program to read elements in a matrix and find sum of lower triangular matrix. How to find sum of lower triangular matrix in C. Find sum of lower triangular matrix, we need to