The below code checks whether a number is even or odd using switch structure.
Logic:
If a number is divided by 2 and the remainder is zero then that number is an even number.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int x;
char ch;
printf("Even-Odd checker\nEnter a number:");
scanf("%d",&x);
switch(x%2)
{
case 0:
printf("The number is even");
break;
case 1:
printf("The number is odd");
}
printf("\n\n(*_*) Would you like to run again...(Y/N)");
scanf(" %c",&ch);
if(ch=='Y'||ch=='y')
{
system("cls");
main();
}
printf("\npress any key to continue");
getch();
}
Logic:
If a number is divided by 2 and the remainder is zero then that number is an even number.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int x;
char ch;
printf("Even-Odd checker\nEnter a number:");
scanf("%d",&x);
switch(x%2)
{
case 0:
printf("The number is even");
break;
case 1:
printf("The number is odd");
}
printf("\n\n(*_*) Would you like to run again...(Y/N)");
scanf(" %c",&ch);
if(ch=='Y'||ch=='y')
{
system("cls");
main();
}
printf("\npress any key to continue");
getch();
}
Post a Comment