Here's a simple C program with output and proper explanation to find even numbers up to a specified limit using For Loop and IF-Else statement
# include <stdio.h> # include <conio.h> void main() { int n, i ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\n\nThe even numbers are :\n\n") ; for(i = 2 ; i <= n ; i++){ if(i % 2 != 0) continue; else printf("%d\t", i) ; } getch() ; }
Output of above program -
Enter the limit :20
The even numbers are :
2 4 6 8 10 12 14 16 18 20
Explanation of above program -
The program is very simple to understand. First it asks the user to enter a limit "n" up to which even are needed. Then using a for loop and an IF-Else condition the program calculates and prints the even numbers up to limit "n". Let's take a look at the working of both loop and IF-Else condition with the help of an example.
Suppose the value of limit n is 20. In this case, the program will calculate and print all even numbers separately up to 20.
The for loop does the calculation of even numbers. Notice the syntax of this loop. The starting value of "i" is set to 2 (first even number not considering 0)
and the loop's terminating condition is "i <= n" and after each
iteration the value of i is incremented by 1.
Inside this for loop there is an IF-Else condition which checks whether the value of i is even or odd. The condition i % 2 != 0 i.e. i is not divisible by 2 will evaluate to true only when i is odd. Now notice carefully, inside IF part there is a keyword continue. The use of continue is to skip the rest of the looping processes and go to the next iteration. In other words, when program encounters a continue keyword inside a loop the program will not execute any statements written below that continue keyword inside the loop and continue the flow of execution from the next iteration.
So to sum up the process of IF-Else condition, the IF part's condition will evaluate to true only when the number is odd and in that case that further execution of that particular iteration will be skipped (to improve the efficiency of the program) using continue keyword and the loop will continue with the next value of i. While the Else part will be executed only when the value of i is even and then the program prints the value of i up to the specified limit n.
Inside this for loop there is an IF-Else condition which checks whether the value of i is even or odd. The condition i % 2 != 0 i.e. i is not divisible by 2 will evaluate to true only when i is odd. Now notice carefully, inside IF part there is a keyword continue. The use of continue is to skip the rest of the looping processes and go to the next iteration. In other words, when program encounters a continue keyword inside a loop the program will not execute any statements written below that continue keyword inside the loop and continue the flow of execution from the next iteration.
So to sum up the process of IF-Else condition, the IF part's condition will evaluate to true only when the number is odd and in that case that further execution of that particular iteration will be skipped (to improve the efficiency of the program) using continue keyword and the loop will continue with the next value of i. While the Else part will be executed only when the value of i is even and then the program prints the value of i up to the specified limit n.
Also look at: C Program to Find Odd Numbers up to a Limit using IF-Else
How can i write a program that prints odd and even numbers separately from minimum limit to maximum limit using goto statement?? please help
Greetings Mate,
Brilliant article, glad I slogged through the C Program to Find Even Numbers up to a Limit using IF-Elseit seems that a whole lot of the details really come back to from my past project.
why is saying c programming is a based on java?
Very useful article, if I run into challenges along the way, I will share them here.
Obrigado,