Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Program to find absolute value of a given number
Next article icon

C Program to Check Whether a Number is Positive or Negative or Zero

Last Updated : 16 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to check whether a given number is positive, negative, or zero.

Examples

Input: 10
Output: Positive
Explanation: Since 10 is greater than 0, it is positive.

Input: -5
Output: Negative
Explanation: Since -5 is less than 0, it is negative.

Different Ways to Check for Positive Numbers, Negative Numbers, or Zero in C

There are two ways to check whether a given number is positive, negative, or zero:

Table of Content

  • Using Conditional Statements Like if-else
  • Using Bit Operators

1. Using Conditional Statements Like if-else

The idea is to use simple if-else-if ladder to check for the equality manually. First use if statement for zero, then else if statement of less than zero numbers (negative) and finally if nothing matches, then it is positive numbers.

Implementation

C
// C Program to check if a number is positive, negative, // or zero using simple conditional checks #include <stdio.h>  void checkNum(int N) {        // Check if the number is zero     if (N == 0) {         printf("Zeri\n");     }     // Check if the number is less than zero     else if (N < 0) {         printf("Negative\n");     }     // If neither, the number is positive     else {         printf("Positive\n");     } }  int main() {     int N = 10;     checkNum(N);     return 0; } 

Output
Positive 

Time Complexity: O(1)
Auxiliary Space: O(1)

2. Using Bit Operators

The most significant bit (MSB) in the binary representation of an integer is the sign bit. For positive numbers, the sign bit is 0, and for negative numbers, the sign bit is 1. By extracting the MSB from the number, we can determine the sign of the given number. For zero, we have to check it in the same way as previous method.

Implementation

C
// C Program to check if a number is positive, negative, // or zero using bitwise operators #include <stdio.h>  void checkNum(int N) {      // Check if the number is zero     if (N == 0) {         printf("Zero\n");         return;     }      // Extracting msb     int msb = N & (1 << (sizeof(int) * 8 - 1));      if (msb)         printf("Negative\n");     else         printf("Positive\n"); }  int main() {     int N = 10;     checkNum(N);     return 0; } 

Output
Positive 

Time Complexity: O(1)
Auxiliary Space: O(1)

  • Bitwise Operators in C
  • C Program to Find the Largest of Three Numbers
  • C Program to Check for Prime Numbers


Next Article
Program to find absolute value of a given number

R

RishabhPrabhu
Improve
Article Tags :
  • C Language
  • C Programs
  • School Programming
  • C Basic Programs

Similar Reads

  • C program to count Positive and Negative numbers in an Array
    Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array Examples: Input: arr[] = {2, -1, 5, 6, 0, -3} Output: Positive elements = 3 Negative elements = 2 There are 3 positive, 2 negative, and 1 zero. Input: arr[] = {4, 0, -2, -9,
    2 min read
  • Lex program to check whether input number is odd or even
    Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) gcc lex.yy.c -ll ./a.ou
    1 min read
  • C Program to Check for Odd or Even Number
    Write a C program to check whether the given number is an odd number or an even number. A number that is completely divisible by 2 is an even number and a number that is not completely divisible by 2 leaving a non-zero remainder is an odd number. Example Input: N = 4Output: EvenExplanation: 4 is div
    4 min read
  • C Program to find whether a no is power of two
    Given a positive integer, write a function to find if it is a power of two or not.Examples : Input : n = 4 Output : Yes 22 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 25 = 32 1. A simple method for this is to simply take the log of the number on base 2 and if you get an integer then nu
    3 min read
  • Program to find absolute value of a given number
    Given an integer N, The task is to find the absolute value of the given integer. Examples: Input: N = -6 Output: 6Explanation: The absolute value of -6 is 6 which is non negative Input: N = 12 Output: 12 Naive Approach: To solve the problem follow the below idea: The absolute value of any number is
    9 min read
  • C Program For Char to Int Conversion
    Write a C program to convert the given numeric character to integer. Example: Input: '3'Output: 3Explanation: The character '3' is converted to the integer 3. Input: '9'Output: 9Explanation: The character '9' is converted to the integer 9. Different Methods to Convert the char to int in CThere are 3
    3 min read
  • Program to find out the data type of user input
    Take a input from user and find out the data type of input value. Examples : Input : geek Output : The input is a string Input : chetna Output : The input is a string Input : 4 Output : The input is a integer Below is C program to find the datatype of user input : // C program to find data type #inc
    2 min read
  • How to Take Operator as Input in C?
    In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we
    2 min read
  • Lex Program to accept a valid integer and float value
    Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) cc lex.yy.c -efl ./a.ou
    1 min read
  • C Program for Difference between sums of odd and even digits
    Write a C program for a given long integer, the task is to find if the difference between sum of odd digits and sum of even digits is 0 or not. The indexes start from zero (0 index is for leftmost digit). Examples: Input : 1212112Output : YesExplanation:the odd position element is 2+2+1=5the even po
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences