In this exercise, we will see, how to write a C Program to Print Odd Numbers from 1 to N using a while and for a loop. We will also see how we can print odd numbers from 1 to N without using a branching statement ( if-else statement).
C Program to Print Odd Numbers from 1 to 100 using While Loop:
Below mentioned program is used to print odd 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 = 1, 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;
}
C Program to Print Even Numbers from 1 to 100 using for Loop:
Below mentioned program is used to print odd 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)
{
printf(" %d\t", i);
}
}
return 0;
}
Output:
Please Enter the Maximum Limit Value: 5 Even Numbers between 1 and 5 are : 1 3 5
C Program to Print Odd Numbers from 1 to 100 using While Loop:
Below mentioned program is used to print odd 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: 5
Even Numbers between 1 and 5are :
1 3 5
-----------------------------------------------------------------------------------------------------------------
C Program to Print Even Numbers from 1 to 100 using for Loop:
Below mentioned program is used to print odd 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: 5
need an explanation for this answer? contact us directly to get an explanation for this answerEven Numbers between 1 and 5 are :
1 3 5