StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #8 (U1): Sign of a Number — positive/negative/zero (nested if-else)
Download
  1. C Programming
  2. C Programming Practice
Program #7 (U1): Even/Odd Checker : Program #9 (U1): Largest of Three Numbers — nested if-else
C Programming Practice

Task: Read a number and classify it as positive, negative, or zero using nestedif-else statements. Print only the category.

 
/* 

  Program: Sign of a Number (nested if-else) — U1

  What it does:

    - Reads a real number x.

    - Uses nested if-else to print "positive", "negative", or "zero".

  Notes:

    - Using double allows inputs like 3.14 or -2.5 as well as integers.

    - In C, -0.0 compares equal to 0.0, so it will be reported as "zero".

*/

 
#include <stdio.h>

 
int main(void) {

    double x;

 
    printf("Enter a number: ");

    if (scanf("%lf", &x) != 1) {

        printf("Invalid input. Please enter a numeric value.\n");

        return 0;

    }

 
    /* Nested if-else structure for classification */

    if (x >= 0.0) {

        if (x == 0.0) {

            printf("zero\n");

        } else {

            printf("positive\n");

        }

    } else {

        printf("negative\n");

    }

 
    /* Examples:

       Input:  7     -> positive

       Input: -3.25  -> negative

       Input:  0     -> zero

    */

 
    return 0;

}

 

 

Program #7 (U1): Even/Odd Checker Program #9 (U1): Largest of Three Numbers — nested if-else
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website