Here's a simple C program with output and proper explanation to find odd 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 odd numbers are :\n\n") ; for(i = 1 ; 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 :
1 3 5 7 9 11 13 15 17 19
Explanation of above program -
The program is very simple to understand. First it asks the user to enter a limit "n" up to which odd are needed. Then using a for loop and an IF-Else condition the program calculates and prints the odd 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 odd numbers separately up to 20.
The for loop does the calculation of odd numbers. Notice the syntax of this loop. The starting value of "i" is set to 1 (first odd number)
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 divisible by 2 will evaluate to true only when i is even. 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 even 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 odd 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 divisible by 2 will evaluate to true only when i is even. 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 even 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 odd and then the program prints the value of i up to the specified limit n.
Glad to help you. Follow us for more such post.
Very nice one.
Thank You.
Hi There,
Grazie! Grazie! Grazie! Your blog is indeed quite interesting around C Program to Find Odd Numbers up to a Limit using IF-Else! I agree with you on lot of points!
Getting memory violation error when i use string function "strlwr", what wrong in step 3, please help, I am new to c language
char *a="SAM";
char *c;
c=strlwr(a);
printf("%s",c);
Very useful article, if I run into challenges along the way, I will share them here.
Kind Regards,
Preethi