C program to check whether a number is positive or negative
what can we learn from here?
you will learn to check whether a number (entered by the user) is negative or positive.
To understand this program required knowledge,
you should have the knowledge of the following C programming topics:
C Programming Operators
C if…else Statement
work of the program,
This program takes a number from the user and checks whether that number is either positive or negative or zero.
ALGORITHM TO FIND WHETHER NUMBER IS POSITIVE, NEGATIVE OR ZERO (IF ELSE STRUCTURE)
- Num ← 0.
- Read Num.
- Is (Num > 0) Then. Begin. Print “Positive” End. Else if (Num<0) Then. Begin. Print “Negative” End. Else. Begin. Print “Zero” End.
Flowchart
Explanation of the program
First we should give #include<stdio.h>
The #include directive tells the C preprocessor to include the contents of the file specified in the input stream to the compiler and then continue with the rest of the original file. … A header file may contain any valid C program fragment.
The header file stdio. h stands for Standard Input Output. It has the information related to input/output functions. Here is the table that displays some of the functions in stdio.
int main()
int main — ‘int main’ means that our function needs to return some integer at the end of the execution and we do so by returning 0 at the end of the program. 0 is the standard for the “successful execution of the program”. main — In C89, the unspecified return type defaults to int.
double num;
Double is also a datatype which is used to represent the floating point numbers. It is a 64-bit IEEE 754 double precision floating point number for the value. It has 15 decimal digits of precision.
- In the printf statement we are printing the statement “Enter a number”.
- In the scanf statement we are using %lf for double data type.
we are using nested if statement.Nested If in C Programming is placing If Statement inside another If Statement. Nested If in C is helpful if you want to check the condition inside a condtion.
if the entered number is 0.0,it will print the statement “you entered 0”
In these lines else statement is used.Because if we entered less than 0.0 else statement will print the statement as “you entered a negative number”
Then the second else statement is ,if we didn’t entered 0.0 or less than 0.0 it will print the statement as “you entered a positive number”.
return 0 in the main function means that the program executed successfully.