C

Print Even Numbers in C Programming

Google+ Pinterest LinkedIn Tumblr +

In our previous program we learnt How to Calculate Profit or Loss in C. In this program, we will make use of the while loop which will print even numbers in C. This is a problem from Chapter 1: Getting Started of the book, “Let us C” by “Yashwant Kanetkar”. Here, we will print all the even numbers starting from 1 to 20. In this program, we will use while loop which will print even number as it gets.

Question

Write a C program to print even numbers from 1 to 20.

Solution to Print Even Numbers in C

First initialise a counter variable I as i=1, then use while loop with condition as i<=20. Within the loop use if block which prints even number as it occurs. Similarly, if you need to print even numbers upto a certain number, let’s say 80, then you can just replace the while loop with the desired number. Example: i<=80. The ‘(i%2==0)’ uses a modulus operator – which prints the remainder of two variables. As we know, any number that gives a remainder of 0 when divided by 2 is an even number. Hence, the if block in the code checks if the number is divisible by 2 and if true, it prints the number.

Download the solution:

Download Program

Source Code

 //Program to print even numbers till 20 while loop demo
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int i=1;
 clrscr();
 printf(“Even numbers are: ”);
 while(i<=20)
 {
 if(i%2==0)
 {
 printf("%d ",i);
 }
 i++;
 }
 getch();
 }

 

Output

Even numbers are: 2 4 6 8 10 12 14 16 18 20

Download the solution:

Download Program

Easy, huh? Let us know if you are stuck somewhere and need help. You are also free to request some other problem using our comments section or contact form and we will try our best to provide you a solution to your problem. Alternatively, you can also contact us on our social media pages.

Read Also:

Introduction to C Programming

How to Calculate Simple Interest in C Programming

Calculate gross salary in C

Multiplying two numbers in C

Share.

About Author

Geek, Tech Crazy, Wannabe Entrepreneur, Blogger, Freelancer, Noob

Leave A Reply

Shares