Algorithm
The provided C code is a simple program that takes a user input (a double-precision floating-point number) and then checks whether the entered number is positive, negative, or zero. Here's a breakdown of the algorithm:
-
Variable Declaration:
cdouble num;
Declares a variable
num
of type double to store the user-entered number. -
User Input:
cprintf("Enter a number: "); scanf("%lf", &num);
Prompts the user to enter a number and reads the input into the variable
num
usingscanf
. -
Conditional Check:
cif (num <= 0.0) {
Checks if the entered number is less than or equal to 0.
-
Nested Conditional Check:
cif (num == 0.0) printf("You entered 0."); else printf("You entered a negative number.");
If the entered number is 0, it prints "You entered 0." Otherwise, if the number is negative, it prints "You entered a negative number."
-
Else Block:
celse printf("You entered a positive number.");
If the entered number is greater than 0, it prints "You entered a positive number."
-
Program End:
creturn 0;
Indicates the successful execution of the program.
Code Examples
#1 Code Example- Check Positive or Negative Using Nested if...else
Code -
C Programming
#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;
}
Copy The Code &
Try With Live Editor
Output
You entered a positive number.
#2 CodeExample-Check Positive or Negative Using if...else Ladder
Code -
C Programming
#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;
}
Copy The Code &
Try With Live Editor
Output
You entered 0.
Demonstration
C Programing Example to Check Whether a Number is Positive or Negative-DevsEnv