In this tutorial you will learn about the C Program for Positive or Negative Number and its application with practical example.
C Program for Positive or Negative Number
In this tutorial, we will learn to create a C program that will check the number is Positive or Negative Numbers in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Conditional Statements in c programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Taking the input numbers. 3. Passing that number to the conditions for the checking. 4. Checking the input number. 5. Print the number is positive or negative. 6. End the program. |
Program for Positive or Negative Number.
In this program, we will first take the input number from the user to check. After taking the input, we will use that number in conditional statements to check. Then we will print the output of the program using the “printf() ” function. Whether it’s a positive, negative, or zero value.
1 2 3 4 5 6 |
<strong>FOR EXAMPLE:- INPUT NUMBER BE 123456. THEN THE PROGRAM WILL RETURN THE OUTPUT AS A POSITIVE NUMBER. IF A INPUT NUMBER IS -142. THEN THE PROGRAM WILL RETURN THE OUTPUT AS A NEGATIVE NUMBER.</strong> |
With the help of this program, we can check Positive or Negative Numbers.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int main() { //Declaring the required variable for the program int n; //n = it will hold an integer value for the input number. //Taking the input number from the user for the program. printf("Enter any number:\n"); //Scanning the input number from the user. scanf("%d",&n); //Checking the positive and negative number from the user. if (n >= 0) { if (n > 0) //Printing the number is positive printf("%d is Positive", n); else //Printing the number is zero not positive not negative. printf("You have entered Value zero."); } else //Printing the number is negative printf("%d is Negative", n); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- n = it will hold the integer input value of the number.
Taking the input number from the user in the integer format.
In this section of code for the program to check, a number is a positive number or a negative number, or a Zero.
Printing the output for the program after checking the conditions.