A ))) //Program to print even numbers upto a given limit
Method -1
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=2,n,e;
printf("Enter the limit\n");
scanf("%d",&n);
printf("Even nos upto %d\n",n);
for(i=2;i<=n;i=i+2)
{
printf("%d\n",i);
}
getch();
}
------------------------------------
Output
Enter the limit (Enter your choice)
8
Even numbers upto 8
2
4
6
8
Method -2
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,
printf("Enter the limit\n");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(i%2==0)
{
printf("%d\n",i)
}
}
getch();
}
------------------------------------
Output
Enter the limit (Enter your choice)
8
2
4
6
8
===============================================
B ))) //Check whether the given number is odd or even
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
printf("Enter the no\n");
scanf("%d",&i);
if(i%2==0)
{
printf("%d is even\n");
}
else
{
printf("%d is odd\n");
}
getch();
}
------------------------------------------------------------------
Output
Enter the number
5
5 is odd
===============================================
C ))) ////Print the total number of odd or even within a given range(1 to 10)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,o,e;
printf("Enter the number\n");
scanf("%d",&i);
for(i=1;i<=10;i=i+1)
{
if(i%2==0)
{
e=e+1;
}
else
{
o=o+1;
}
}
printf("Total no of even is %d,and odd is %d",e,o);
getch();
}
------------------------------------------------------------------
Output
Enter the number
9
Total number of even is 4,and odd is 5
===============================================
No comments:
Post a Comment