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. 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! Next Steps:
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. Examples:Input: A = 2 Output: 2 is positive Input: A = -554 Output: -554 is negativeIn 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 , and then A is checked for positive using statement and , and operators. Below is the C program to find whether a number is positive, negative or zero.
Output: Enter the number A =: -54 -54 is negative.Time Complexity: O(1) Auxiliary Space: O(1) Article Tags :
Write a C program to check whether a given number is positive or negative. Pictorial Presentation: Sample Solution: C Code: Sample Output: Flowchart: 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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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
|