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.

advertisement
advertisement
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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.