Q.) WAP to print even numbers within a range using while loop in C program.
#include< stdio.h>
int main(){
int m=0, n=0;
printf("Enter the range: ");
scanf("%d", &m);
scanf("%d", &n);
while(m<=n){
if(m%2 == 0){
printf("%d ", m);
}
m++;
}
getch();
}
Output
Enter the range : 10 30
10 12 14 16 18 20 22 24 26 28 30
Program Explanation
Step 1 : Include header files (#include< stdio.h> and #include< conio.h>).
Step 2 : Start with main function with return type.
Step 3 : parenthesis to start and end the program { }.
Step 4 : declare variables with data type i.e, 'n=0 , m=0' is an integer type so we use "int" data type.
Step 5 : Use output function printf() to print the output on the screen.
Step 6 : Use input function scanf() to get input from the user.
Step 7 : here, we have even numbers within a range using while loop, enter a the range : , we can enter 10 30 (m=10 , n=30), then check the condition (m <= n), and condition was true then , calculate this condition if(m%2 == 0) m modulus 2 the value is equal to 0, then print the value of m and then loop was go to m++ , and the value of m is increment the original value ,(m=11), this value was go to loop , and check condition .the condition was true loop was goes inside the body,and condition was false loop was goes outside the body.
Step 8 : using getch() function to hold the screen.