In this exercise, we will see, how to write a C Program to Print Even Numbers from 1 to N using a while and for a loop. We will also see how we can print even numbers from 1 to N without using a branching statement ( if-else statement).
C Program to Print Even Numbers from 1 to 100 using While Loop:
Below mentioned program is used to print even numbers from 1 to N using the while loop. The value of N is asked by users with the help of a scanf (input) function.
#include<stdio.h>
int main()
{
int i = 2, number;
printf("\n Please Enter the Maximum Limit Value : ");
scanf("%d", &number);
printf("\n Even Numbers between 1 and %d are : \n", number);
while(i <= number)
{
printf(" %d\t", i);
i = i+2;
}
return 0;
}
Output:
Please Enter the Maximum Limit Value: 10
Even Numbers between 1 and 10 are : 2 4 6 8 10
C Program to Print Even Numbers from 1 to 100 using for Loop:
Below mentioned program is used to print even numbers from 1 to N using the for a loop. The value of N is asked by users with the help of a scanf (input) function.
#include<stdio.h>
int main()
{
int i, number;
printf("Please Enter the Maximum Limit Value : ");
scanf("%d", &number);
printf("Even Numbers between 1 and %d are : \n", number);
for(i = 1; i <= number; i++)
{
if ( i % 2 == 0 )
{
printf(" %d\t", i);
}
}
return 0;
}
C Program to Print Even Numbers from 1 to 100 using While Loop:
Below mentioned program is used to print even numbers from 1 to N using the while loop. The value of N is asked by users with the help of a scanf (input) function.
Output:
Please Enter the Maximum Limit Value: 10
Even Numbers between 1 and 10 are :
2 4 6 8 10
C Program to Print Even Numbers from 1 to 100 using for Loop:
Below mentioned program is used to print even numbers from 1 to N using the for a loop. The value of N is asked by users with the help of a scanf (input) function.
Output:
Please Enter the Maximum Limit Value: 10
Even Numbers between 1 and 10 are :
need an explanation for this answer? contact us directly to get an explanation for this answer2 4 6 8 10