C Program to Check Whether a Number is Positive or Negative

devquora
devquora

Posted On: Feb 29, 2024

C Program to Check Whether a Number is Positive or Negative

 

Overview:

In this article, we will discuss how to check whether a number is positive or negative in C programming language. Now, First of all, I would like to explain the number, Integers, types of integers, positive integers, and negative Integers. After all, I will demonstrate the logic of the C Program to Check Whether a Number is Positive or Negative and explain the working of its internal execution with output.

Table of contents:

  1. What is a Number?
  2. What is Whole Number?
  3. What is Integer?
  4. What are the types of Integers?
  5. What is positive Integer?
  6. What is negative Integer?
  7. Logic to Check Whether a Number is Positive or Negative
  8. Demonstration to Check Whether a Number is Positive or Negative
  9. C Program to Check Whether a Number is Positive or Negative
  10. Conclusion

What is a Number?

The Number is a combination of digits and decimal points. The repetition of digits does not matter but the decimal point does not repeat. For instance, a Number 1123.171 uses 4 digits i.e. 1,2,3,7, and one decimal point.

Check positive or negative

What is Whole Number?

Whole numbers mean numbers without fractions or decimals. It is a collection of positive integers and zero. It is represented by the symbol “W” and the set of numbers is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,……………}. All the natural numbers are considered whole numbers, but all the whole numbers are not natural numbers. Thus, the negative numbers are not considered whole numbers.

What is Integer?

An integer is a number that can be written without a fractional component. Integers are generally denoted as Z and can be represented as follows: Z = {∞……-8,-7,-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,……∞}

What are the types of Integers?

Mainly Integers are 3 types as Positive, and negative counting numbers, as well as zero. Z = {∞..., −3, −2, −1, 0, 1, 2, 3, ...∞}. Rational numbers can be expressed as a ratio of an integer to a non-zero integer. All integers are rational, but the converse is not true.

types of integers

What is positive Integer?

Positive integers are all whole numbers greater than zero. All the numbers we use for counting are positive integers. Positive integers are actually part of a larger group of numbers called integers. Positive Integers are generally known as Natural numbers. It is denoted by N and represented as N = {1, 2, 3, 4, ....∞} For instance, 100001 is a positive integer. The square root of 2 is not since it is not a whole number. 0.01 is not since it is not a whole number. -2 is not since it is not greater than zero. zero (0) is not a positive integer since zero is not a positive number. In other words, A positive integer is a whole number from zero onwards with no fractions or decimals right of the number line.

What is negative Integer?

A negative integer is one of the integers ..., -4, -3, -2, -1 obtained by negating the positive integers. A number that is less than zero but not a fraction or a decimal is called a Negative Integer. The negative integers are commonly denoted as Z(-). It is represented by putting a '-' sign before the number. It is shown to the left of zero on a number line.

Logic to Check Whether a Number is Positive or Negative

  • Step 1: The user gives an input
  • Step 2: Input is stored in an int type variable say num, num is first checked for being 0.
  • Step 3: if(num == 0)
  • Step 4: prep is then checked for being greater than 0.
  • Step 5: if(num > 0)
  • Step 6: If num is greater than 0 then the input is a positive number. Otherwise, the number is a negative number.

Demonstration to Check Whether a Number is Positive or Negative

A number is positive if it is greater than 0 N > 0, the number is positive. Otherwise, the number is negative N< 0, the number is negative.

Note: 0 is neither positive nor negative.

Check positive or negative

C Program to Check Whether a Number is Positive or Negative

#include <stdio.h>

int main()
{
 int number;

 printf("Enter any number: ");
 scanf("%d",&number);

 if (number > 0)
   printf("%d is positive number", number);

 else if (number < 0)
   printf("%d is a negative number.", number);

 else
   printf(" You entered value zero.");

 return 0;
}

The output of the Program with code:

Enter a number: 6

You entered a positive number.

positive or negative output

Explanation of the C Program to Count Number of Digits in an Integer with output.

The above C program to find Positive or Negative Numbers is structured as if the first condition fails, it will traverse to the second condition. If both the conditions fail, the else statement will execute. Let’s see the working principle of both conditions below:

First Condition(if (number > 0)): Checks whether the given number is greater than 0. If this condition is true, the given value is a positive number.

The second condition(else if (number < 0)): Checks whether the given number is less than 0. If this condition is true, it is a negative number If both the above conditions fail, the number is equal to 0.

In this program, the user entered 6 which is a positive integer displayed on the screen.

Conclusion:

The internal execution of the program depends upon the entered Integer.

After the successful compilation of the program, a message is displayed on the screen as entering a number: if the user entered 6 then the compiler checks that 6 is greater than 0, therefore, it is a positive number not negative or 0 therefore, again a message displayed on the screen as You entered a positive number.

    Please Login or Register to leave a response.

    Related Articles