Friday

C Program — Display Even and Odd Numbers


Displaying even and odd numbers between a range of numbers.

Even numbers are numbers divisible by 2, while odd numbers are numbers whose remainder equals 1 if divided by two.

This program displays even and odd numbers between 1 and a specified limit. The limit can be changed by changing the value of the last number variable. The initial even or initial odd number value can also be changed to specify the start number.


#include<stdio.h>
void main()
{
 int i,end_number;
 clrscr();
 /* Accept command-line input */
 printf("\n Enter last number : ");
 scanf("%d", &end_number);
 
 //Display Even Numbers
 printf("\n Even Numbers:\n ");
 i = 2;//initial even number
 while(i<=end_number)
 {
  printf(" %d",i);
  i=i+2;//increment i by 2
 }
 //Display Odd Numbers
 printf("\n\n Odd Numbers:\n ");
 i=1;//initial odd number
 while(i<=end_number)
 {
  printf(" %d",i);
  i=i+2;//increment i by 2
 }
 getchar();
}
Download program here.

See another example that displays even and odd number between 1 and 30

#include<stdio.h>

void main()
{ int i,end_number; end_number = 30;//specific end number

//Display Even Numbers
printf("\n Even Numbers:\n ");
i = 2;//initial even number
while(i<=end_number)
{
printf(" %d",i);
i = i + 2;//increment i by 2
} //Display Odd Numbers
printf("\n\n Odd Numbers:\n ");
i=1;//initial odd number
while(i <= end_number)
{
printf(" %d",i);
i = i + 2;//increment i by 2
}
getchar();
}
Download program here.

No comments:

Post a Comment