/* */ Click to Join Live Class with Shankar sir Call 9798158723

check entered integer positive or negative in C using if else ?


Q.) W.A.P to check the leap year or not in C using if else ?
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
     int a;
     printf("enter an integer :\n");
     scanf("%d",&a);
     if (a>=0)
     {
        printf("This is a positive integer");
     }
	 else{
     printf( "This is a negative integer");
	 }
     getch();
 }
Output
enter an integer :
-14      /* say a = -14 */.
This is a negative integer

Program Explanation


Step 1: Include header files (#include<stdio.h> and #include<conio.h>).

Step 2: Start with main function with return type.

Step 3: parenthesis to start and end the program { }.

Step 4: declare variables with data type i.e, 'a' is an integer type so we use "int" data type.

Step 5: Use output function printf() to print the output on the screen.

Step 6: Use input function scanf() to get input from the user.

Step 7: Here, we have to check whether the entered integer is positive or negative by using if-else condition, Here, we use the relational operator ( i.e, >=) to check condition. if condition checks whether 'a' is greater than or equal to zero.

  • if this condition is true, then it will execute if block, which will print the output as this is a positive integer.
  • if the condition is not true, then it will execute else block and print the output as this is a negative integer.
Step 8: using getch() function to hold the screen.