Home » , » Basic C Programming Practice - Practice 2

Basic C Programming Practice - Practice 2

Written By Unknown on Thursday, October 10, 2013 | 1:17 PM

=> Write a program to check the user inputted number is even or odd number?


Solution :

#include<stdio.h>
#include<conio.h>
main()
{
      int numb;
      printf("Let's Know The Number Is Even Or Odd.  \nPlease Enter Your Number : ");
      scanf("%d",&numb);
      if(numb%2==0)
      printf("The Number Is Even.");
      else

      printf("The Number Is Odd.");
      getch();
}

Program Analysis :

We know if any number get divided by 2 and the reminder (%) become 0 then the number is EVEN so here I use this logic in this line :

   if(numb%2==0)

Here numb is a variable inputted by user and the expression is if the number get divided by 2 and the remainder become 0 than the number is EVEN:

We can also do it like the following :

#include<stdio.h>
#include<conio.h>
main()
{
      int numb;
      printf("Let's Know The Number Is Even Or Odd.  \nPlease Enter Your Number : ");
      scanf("%d",&numb);
      if(numb%2==1)
      printf("The Number Is Odd.");
      else

      printf("The Number Is Even.");
      getch();
}

Program Analysis :

if(numb%2==1)

This expression says that if the given number get divided by 2 then the reminder will be 1 and it will be an odd number otherwise it will be an Even number.

Example Program :                                              

#include<stdio.h>
#include<conio.h>
main()
{
      int numb;
      printf("Let's Know The Number Is Even Or Odd.  \nPlease Enter Your Number : ");
      scanf("%d",&numb);
      if(numb%2==1)
      printf("The Number Is Odd.");
      else
      printf("The Number Is Even.");
      if(numb>5)
      printf("\nThe Number Is Grater Than 5");
      else
      if(numb<5)
      printf("\nThe Number Is Less Than 5");
      else
      if(numb=5)
      printf("\nThe Number Is 5");
      getch();
      }

Hope you get a clear concept on this decision control statement " if "
Share this article :

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Learn Programming - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger| Distributed by Rocking Templates