 # C Programming Code Examples

## C > For Loops and While Loops Code Examples

### C program to print all even numbers from 1 to n

``` C program to print all even numbers from 1 to n Write a C program to print all even numbers from 1 to n using for loop. C program to generate all even numbers between given range. Logic to print even numbers using if else and for loop in given range in C programming. Logic to print even numbers using if condition First let us talk about the easiest way to print even numbers. If I ask you to list all even numbers from 1 to 100 what will be your immediate step? You will probably start from 1 check if its even number then add it to the even list otherwise not. Input upper limit to the even numbers from user. Store it in some variable say N. Run a loop from 1, that runs till N, increment the loop counter by 1 in each iteration. The loop structure should look like for(j=1; j<=N; j++). Inside the loop body check even/odd condition. If the current number j is divisible by 2 then j is even. Means if(j % 2 == 0), then print the value of j. #include <stdio.h> int main() { int j, n; /* Input upper limit of even number from user */ printf("Print all even numbers till: "); scanf("%d", &n); printf("Even numbers from 1 to %d are: \n", n); /* Start loop counter from 1, increment it by 1, will iterate till n */ for(j=1; j<=n; j++) { /* Check even condition before printing */ if(j%2 == 0) { printf("%d\n", j); } } return 0; } ```

C++ Finds Convex Hull of A Set of Points - Program to implement "Jarvis March" to find convex hull. The idea of Jarvis's Algorithm is simple, we start from the 'leftmost point' (or point with minimum x coordinate value) and

C++ Program Calculate GCD of 2 Integers - Examples on different ways to calculate GCD of two integers (for both Positive & Negative integers) using "Loops and Decision" making statements. The "Largest integer" which can

For Statement is Typically used to Count a - The "First section" specifies the starting point for the count. The "Second section" sets the counting limit. The "Last section" determines the "counting frequency". The formula of the

C Segregate 0s on left side 1s on right side - C Program to Segregate 0s on left side and 1s on right side of the Array (Traverse Array only once). Function to segregate all 0s on left and all 1s on right. Increment left index while we

C Program code Simple Account software - Enter the Name of the Custumer. Enter the Acc.no of the Custumer. Enter the Balance of the Custumer. Enter the name of Customer whose rec. is to be modified. Enter the new

C Program to Print Sum of Array Elements - In C, we are setting up the pointer to the base address of array and then we're incrementing pointer and using * operator to get & sum-up the values of all the array elements. Enter the

C++ Programing Implement String in STL - 'Insert substring' in a string. 'Erase substring' from a string. 'Append substring' to a string. Replace the string with a substrng. Size of a string. Find substring in a string. Display the

C Display Fibonacci Series Using Recursion - Program code display 'Fibonacci series' using recursion. Using "fibonacci_series()" that calls itself recursively, in order to display series for the entered number of terms. Program enter