Write a C Program to Check Whether a Number is Positive, Negative, or Zero.

Prince Patel
By -
0

Write a C Program to Check Whether a Number is Positive, Negative, or Zero.


In this Article of programming, understanding basic control structures and logic is essential. One fundamental task involves identifying whether a given number is positive, negative, or zero. In this article, we will explore a simple C program that accomplishes this task without utilizing structures or functions. By breaking down the program step by step, we will gain insight into the underlying logic and decision-making process.

The goal of our C program is straightforward: we want to determine whether a given input number is positive, negative, or zero. We will accomplish this by using only the basic building blocks of C programming, such as variables, user input, and conditional statements (if-else).

Here's a C program to Check Whether a Number is Positive, Negative, or Zero:

#include<stdio.h>
#include<conio.h>
void main()
{
   int number;

   printf("Enter a number: ");    scanf("%d", &number);
   if (number > 0)
   {
      printf("%d is a positive number.\n", number);
   }
   else if (number < 0)
   {
      printf("%d is a negative number.\n", number);
   }
   else
   {
      printf("The number is zero.\n");
   }
}

  • we declare an integer variable named "number" to hold the input value
  • we prompt the user to input a number and use the "scanf" function to read and store the input in the "number" variable.
  • Check Positive, Negative, or Zero

       if (number > 0)
       {
          printf("%d is a positive number.\n", number);
       }
       else if (number < 0)
       {
          printf("%d is a negative number.\n", number);
       }
       else
       {
          printf("The number is zero.\n");
       }

    Using the if-else statements, we compare the value of "number" with zero. If the number is greater than zero, we print that it is positive. If the number is less than zero, we print that it is negative. If none of these conditions are met, we conclude that the number is zero.

Through this article, we've walked through the process of creating a simple C program to determine whether a given number is positive, negative, or zero. By employing basic programming concepts like variables, input/output functions, and conditional statements, we were able to construct a concise yet effective solution. This exercise underscores the importance of mastering foundational programming concepts, which serve as the bedrock for more complex applications in the world of software development.
 
Output:




Tags:

Post a Comment

0Comments

Post a Comment (0)