even and odd number program in c

 

even and odd number program in c


Algorithm 

step 1-start
 
step 2-Declare n 
 
step 3-print "Enter a Number"
 
step 4-Input n 
 
step 5-if (n mod 2==0) print "Entered Number is even number"
 
else print "Entered Number is odd Number"
 
step 7-stop

Flowchart
 

 
  
Code
 #include <stdio.h>
int main() {
    int n;
    printf("Enter a Number:");
    scanf("%d",&n);
    if(n%2==0)
    {
        printf("%d is even Number",n);
    }
    else
    {
        printf("%d is Odd Number",n);
    }
    return 0;
}
 

Sample Input and Output:

Enter a Number:31
31 is Odd Number

 

 Explanation of above Program:

Even No

An even number is an any number that can by divisible by 2 and gives remainder  as 0 is Known as Even Number.To Understand more clearly  Let's Understand we an example 


Formula to Find Even Number = number % 2

Divide the number by 2 and check whether the remainder is 0 or not if it is 0 then the given number is Even else Odd.In the given formula we have used %(modulo) operator instead of /(Divide) operator because divide operator only divide the number while %(modulo) operator the divide the number returns remainder  of the number                            

Example                    

number=6

result= 6%2

result=0

Therefore 6 is Even Number 

Odd no

An odd number is an any number that can by divisible by 2 and gives remainder  as 1 is Known as Odd Number.To Understand more clearly  Let's Understand we an example

Formula to Find Even Number = number %2

Divide the number by 2 and check whether the remainder is 1 or not if it is 1 then the given number is Odd else Even.In the given formula we have used %(modulo) operator instead of /(Divide) operator because divide operator only divide the number while %(modulo) operator  divide the number and  returns remainder  of the number  as well                                          

Example                    

number=5

result= 5%2

result=1

Therefore 5 is Odd Number. 

 In above C Program we have first imported header file(#include<stdio.h>) and in the second line of code we have defined main function with return type int,those who don't known about main function then I would like to tell them that main  function is nothing but a function  from  where a execution of any c program is started click here to known more about main function or function.

After that we have declared one variable i.e n with data type int,that indicates it can hold only integer value,We will be using this variable to take the input and to check whether entered number is  even or odd then in the next of line of code  we are simply printing the message on console  "Enter a Number" using printf() which is defined in stdio.h header file click here to learn about it After that we are taking user input and storing it in  variable n using scanf() function

In the next line of code  we are going to do some operation to find entered number is even or odd i.e we are doing "if (n%2==0) " the meaning of this line is  divide n by 2 and check whether the remainder is 0 if 0 then execute the statement inside if block else execute the statements  of else blocks.That's it Guys We have Created Program to check entered number is Even or Odd😁

 

 

 If you any have doubt,suggestion regarding this post or website then feel free to share with us.

 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎 


Previous Next

Post a Comment

Previous Post Next Post