C

C Program To Print Even Numbers in a Given Range Using For Loop

In this tutorial, we are going to see how to print even numbers in a given range using for loop. Considering we have a range and we need to print all even numbers present in this range using a C program.

An even number is an integer exactly divisible by 2. Example: 0, 4, 8, etc.

An odd number is an integer that is not exactly divisible by 2. Example: 1, 3, 7, 15, etc.

To print even numbers in a given range, we check the remainder of the division by dividing the number by 2.
 

 

C Program To Print Even Numbers in a Given Range Using For Loop
#include<stdio.h>

int main() {

  int r1, r2, remainder, i;

  printf("Start : ");
  scanf("%d", & r1);
  printf("End : ");
  scanf("%d", & r2);
  printf("\nThe Even numbers between %d and %d are: ", r1, r2);

  for (i = r1; i <= r2; ++i) {
    remainder = i % 2;
    if (remainder == 0)
      printf("\n%d", i);
  }
  return 0;
}

Output:

Start : 1
End : 10

The Even numbers between 1 and 10 are: 
2
4
6
8
10

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *