write a program to check whether given number is even or odd
Answers
Hello, I am writing a C++ program for the given question. Hope it helps you.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int a;
char y,n,choice;
A:
cout<<"Welcome to my coding world"<<endl;
cout<<"Let's calculate whether the number entered by you is odd or even"<<endl;
cout<<"Enter a number ( whole digit)"<<endl;
cin>>a;
if(a%2==0)
{
cout<<a<<"is an even number"<<endl;
}
else
{
cout<<a<<"is an odd number"<<endl;
}
cout<<" Want to calculate for some other number ??"<<endl;
cin>>choice;
if(choice=='y')
{
goto A;
}
if(choice=='n')
{
exit(0);
}
getch();
}
Thank You ^_^
Here is your answer.
I know JAVA and so I am writing a JAVA program for the given question.
Here is the code
__________________________________________________________________________
import java.util.*;
class Odd_or_even
{
public void main()
{
Scanner sc = new Scanner ( System.in);
System.out.println("Enter a number to check whether it is even or odd");
int n=sc.nextInt();
if(n%2==0)
{
System.out.println("Even number");
}
else
{
System.out.println("Odd number");
}
}
}
______________________________________________________________________
Explanation
The first line of the program:
import java.util.*;
It imports the package .util
This is helpful for doing easy and simple input output programs.
_________________________________
class Odd_or_even
It gives a class name.The class is a user defined datatype which helps us in creating and executing a program through various methods.
_____________________________________
public void main()
It is a method of a class.Methods or functions are identified by {} . They open by the curly bracket { and close by the curly bracket too } The void says that main() does not return any value to any other function.
_______________________________________
Scanner sc=new Scanner(System.in);
This statement creates a new Scanner class.A scanner class helps in taking keyboard inputs from the user.
__________________________________________
int n=sc.nextInt();
This is the statement that helps in taking the input. This tells the compiler that the next character is an integer type value.
Integers are numbers that maybe negative or positive but cannot be decimals
____________________________________________
if(n%2==0)
This checks whether n modulus 2 is 0 or not.
For example 2%2==0 it is true since 2%2=0
if the condition is true then the system will enter the next statement
Otherwise it will go to the else portion.
_____________________________________________
System.out.println("Even number");
This prints the statement: Even number if and only if the condition of if is satisfied.
_______________________________________________
else
If the condition of if statement is satisfied then the system will enter into execution of the else portion of the program.
else is only executed if n%2 is not equal to 0
for example 5%2 = 1
so it is not 0.
___________________________________________
System.out.println("Odd number");
This prints the statement :"Odd number");
Note that the compiler would ignore the else part if the if part is true.
__________________________________________________________________________________
Important things about the program
⇒ Note that the class begins with a curly bracket and ends with one too.
Syntax of a class:
class
{
............................
}
⇒The if condition and main has the same syntax too.
public void main()
{
......................
}
⇒The if condition and else condition are also the same .
if
{
................
}
else
{
...............
}
⇒The if conditions actually checks whether a number is divisible by 2 or not.
⇒The else condition does not check anything.It just prints the number is odd if the if is not satisfied. The logic is: If the number is not even,then it must be odd, hence we don't need to check anything else.
⇒Every JAVA statement has a ; sign at the end.
This marks the end of the statement.
⇒The loops and if else conditions and the methods do not have ; sign at the end of them.
Examples
Input
3
Output
Odd number
_______________________________________________
Input
6
Output
Even number
_________________________________________________
Hope it helps you