c-program to print the all even and odd number form 1 to n natural numbers
In this program, we will discuss the C program to display all even and odd number from 1 to n natural number.
In this program, we are going to learn about how to find odd or even numbers from 1 to given number using the while loop in the C language.
What is Even or Odd
When the number is divided by 2 and the the remainder becomes zero then the number is called as the even number on the other sides when it is divided by 2 and the remainder becomes 1 they are called odd numbers .
program to print even and odd number from 1 to n natural number using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("enter the number\n");
scanf("%d",&n);
printf("\n even number \n");
i=2;
while(i<=n){
printf("%d ",i);
i=i+2;
}
printf("\nodd number\n");
i=1;
while(i<=n){
printf("%d ",i);
i=i+2;
}
getch();
}
output:-
Another way to solve this program :-
/*program to print even number from 1 to n by using while loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
if(i%2==0){
printf("%d\n",i);
i++;
}
else{
printf("%d ",i);
i++;
}
}
getch();
}
output :- even number 2,4,6,........
odd number 1,3,5,7,9.........
For more c-programming :-
1.c-program to check which number is divisible by 2,3,9
2.c-program to check which number is divisible by 4,6,9.
3.c-program to check weather a number is prime or not
4.c-program to print all prime number form 1 to n natural number