C program to Check Whether a Number is Positive or Negative

 

C program to Check Whether a Number is Positive or Negative




This is a C program to check whether a given integer is positive or negative.

Problem Description

The program takes the given integer and checks whether the integer is positive or negative.

Problem Solution

1. Take the integer which you want to check as input.
2. Check if it is greater or lesser than zero and print the output accordingly.
3. Exit.

Program/Source Code

Here is source code of the C program which checks a given integer is positive or negative. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5.     int number;
  6.  
  7.     printf("Enter a number \n");
  8.     scanf("%d", &number);
  9.     if (number >= 0)
  10.         printf("%d is a positive number \n", number);
  11.     else
  12.         printf("%d is a negative number \n", number);
  13. }
Program Explanation

1. Take the integer which you want to check as input and store it in a variable number.
2. Using if,else statements check whether the integer is greater or lesser than zero.
3. If it is greater than or equal to zero, then print the ouput as “it is a positive number”.
4. If it is lesser than zero, then print the ouput as “it is a negative number”.
5. Exit.


Runtime Test Cases
Case:1
Enter a number
-10
-10 is a negative number
 
Case:2
Enter a number
45
45 is a positive number

Brand creation, trend analysis & style consulting

Our content is highly curated—in partnership with industry leaders from Google, Amazon, and Microsoft—to advance expertise in data science, ML, cloud computing, cybersecurity, and more. From our individual courses to our highly valued Nanodegree credential, each program is deeply focused on a single concept, eliminating guesswork in selecting the right course to overcome talent gaps.

Contact