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:

  1. Variable Declaration:

    c
    double num;

    Declares a variable num of type double to store the user-entered number.

  2. User Input:

    c
    printf("Enter a number: "); scanf("%lf", &num);

    Prompts the user to enter a number and reads the input into the variable num using scanf.

  3. Conditional Check:

    c
    if (num <= 0.0) {

    Checks if the entered number is less than or equal to 0.

  4. Nested Conditional Check:

    c
    if (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."

  5. Else Block:

    c
    else printf("You entered a positive number.");

    If the entered number is greater than 0, it prints "You entered a positive number."

  6. Program End:

    c
    return 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

x
+
cmd
Enter a number: 12.3
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

x
+
cmd
Enter a number: 0
You entered 0.
Advertisements

Demonstration


C Programing Example to Check Whether a Number is Positive or Negative-DevsEnv

Next
Appending into a File in C Programming