Using if-else statement in C: Checking for Positive and Negative Numbers


Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. The if...else ladder allows you to check between multiple test expressions and execute different statements

 Syntax :
   if ( condition 1 )
   {
       // block of statement to be executed if condition is true ;
   }
   else if ( condition 2 )
   {
       // block of statement to be executed if the condition1 is false condition2 is true ;
   }
   else
   {
       block of statement to be executed if the condition1 is false condition2 is False ;
   }

This program is a basic example of using the "if-else" statement in C. The program first prompts the user to input an integer value (stored as the variable "a") using the "scanf" function. The program then uses an "if-else" statement to check the value of "a". If the value of "a" is less than 0, the program will print "a is Negative Value". If the value of "a" is equal to 0, the program will print "The Given Value is 0". And if the value of "a" is greater than 0, the program will print "a is Positive Value". Finally, the program returns 0.


Source Code

//else if statement
 
#include<stdio.h>
int main()
{
   int a;
   printf("\nEnter The Value of A : ");
   scanf("%d",&a);
   if(a<0)
   {
       printf("%d is Negative Value",a);
   }
   else if(a==0)
   {
       printf("The Given Value is 0");
   }
   else if(a>0)
   {
       printf("%d is Positive Value",a);
   }
    return 0;
}
 
 
To download raw file Click Here

Output

Enter The Value of A : 12
12 is Positive Value
Enter The Value of A : -89 -89 is Negative Value
Enter The Value of A : 0 The Given Value is 0

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C