Write C program to check whether a number is positive, negative or zero using Macros

This is a C++ Program to Check if a Number is Positive or Negative.

The program takes a number and checks if it is a positive number or negative number.

1. The program takes a number. 2. It is checked if it is positive or negative. 3. The result is printed.

4. Exit.

Here is the source code of C++ Program to Check if a Number is Positive or Negative. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5. int num;
  6. cout << "Enter the number to be checked : ";
  7. cin >> num;
  8. if (num >= 0)
  9. cout << num << " is a positive number.";
  10. else
  11. cout << num << " is a negative number.";
  12. return 0;
  13. }

1. The user is asked to enter a number and it is stored in the variable ‘num’. 2. If num is greater than or equal to 0, it is positive. 3. Else it is a negative number.

4. The result is then printed.

Subscribe Now: C++ Programs Newsletter | Important Subjects Newsletters

Case 1 : Enter the number to be checked : -8 -8 is a negative number.   Case 2 : Enter the number to be checked : 0 0 is a positive number.   Case 2 : Enter the number to be checked : 125 125 is a positive number.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Participate in C++ Programming Certification Contest of the Month Now!

  • Get Free Certificate of Merit in C++ Programming
  • Participate in C++ Programming Certification Contest
  • Become a Top Ranker in C++ Programming
  • Take C++ Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Write C program to check whether a number is positive, negative or zero using Macros

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 & technical discussions at Telegram SanfoundryClasses.

This program takes a number from the user and checks whether that number is either positive or negative or zero.

Check Positive or Negative Using Nested if...else

#include <stdio.h> int main() { double num; printf("Enter a number: "); scanf("%lf", &num); if (num <= 0.0) { if (num == 0.0) printf("You entered 0."); else printf("You entered a negative number."); } else printf("You entered a positive number."); return 0; }

You can also solve this problem using nested if...else statement.

Check Positive or Negative Using if...else Ladder

#include <stdio.h> int main() { double num; printf("Enter a number: "); scanf("%lf", &num); if (num < 0.0) printf("You entered a negative number."); else if (num > 0.0) printf("You entered a positive number."); else printf("You entered 0."); return 0; }

Output 1

Enter a number: 12.3 You entered a positive number.

Output 2

Enter a number: 0 You entered 0.

Given a number A. The task is to check whether A is positive, negative or zero.

Write C program to check whether a number is positive, negative or zero using Macros
Examples:

Input: A = 2 Output: 2 is positive Input: A = -554 Output: -554 is negative

In the below program, to find whether A is positive, negative or zero; first the number is taken as input from the user using scanf in 

Write C program to check whether a number is positive, negative or zero using Macros
, and then A is checked for positive using 
Write C program to check whether a number is positive, negative or zero using Macros
statement and 
Write C program to check whether a number is positive, negative or zero using Macros
Write C program to check whether a number is positive, negative or zero using Macros
and 
Write C program to check whether a number is positive, negative or zero using Macros
operators. Below is the C program to find whether a number is positive, negative or zero. 

    printf("Enter the number A: ");

        printf("%d is positive.", A);

        printf("%d is negative.", A);

        printf("%d is zero.", A);

Output:

Enter the number A =: -54 -54 is negative.

Time Complexity: O(1)

Auxiliary Space: O(1)

Article Tags :

Last update on August 19 2022 21:50:43 (UTC/GMT +8 hours)

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

Pictorial Presentation:

Write C program to check whether a number is positive, negative or zero using Macros

Sample Solution:

C Code:

#include <stdio.h> void main() { int num; printf("Input a number :"); scanf("%d", &num); if (num >= 0) printf("%d is a positive number \n", num); else printf("%d is a negative number \n", num); }

Sample Output:

Input a number :15 15 is a positive number

Flowchart:

Write C program to check whether a number is positive, negative or zero using Macros

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a C program to check whether a given number is even or odd.
Next: Write a C program to find whether a given year is a leap year or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

How to get the current directory in a C program?

#include <unistd.h> #include <stdio.h> #include <limits.h> int main() { char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd)) != NULL) { printf("Current working dir: %s\n", cwd); } else { perror("getcwd() error"); return 1; } return 0; }

Ref : https://bit.ly/3jSypjr

  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation