Lets write a C program to find all the even numbers between 2 integer values input by the user, using for loop.
Related Read:
Even or Odd Number: C Program
Modulus or Modulo Division In C Programming Language
C Program to Generate Even Numbers Between Two Integers
Note 1: An even number is an integer that is exactly divisible by 2. That is reminder of division should be zero.
Note 2: Even numbers are of the form 2 * number;
Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.
Video Tutorial: C Program to Find Even Numbers Between Range using For Loop
In above c program, we ask the user to input 2 integer value and store it in variables start and end. For loop counter is initialized to start and for loop executes until loop counter is less than or equal to value of end. For each iteration of the for loop, loop counter value increments by 1.
Inside for loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window.
#include<stdio.h> int main() { int start, end, count, temp; printf("Enter start value and end value to generate Even no's\n"); scanf("%d%d", &start, &end); if(start > end) { temp = start; start = end; end = temp; } printf("Even numbers between %d and %d are:\n", start, end); for(count = start; count <= end; count++) { if(count % 2 == 0) { printf("%d\n", count); } } return 0; }
Output 1
Enter start value and end value to generate Even no’s
10
20
Even numbers between 10 and 20 are:
10
12
14
16
18
20
Output 2
Enter start value and end value to generate Even no’s
50
40
Even numbers between 40 and 50 are:
40
42
44
46
48
50
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert