Monday, August 4, 2014

C Program to Check if a given Integer is Positive or Negative

his C Program checks if a given integer is positive or negative. Here if a number is greater than 0 then that number is a positive number. If a number is less than 0 then the number is negative number.
Here is source code of the C program which checks a given integer is positive or negative. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to check whether a given integer is positive
  3.  * or negative
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     int number;
  10.  
  11.     printf("Enter a number \n");
  12.     scanf("%d", &number);
  13.     if (number >= 0)
  14.         printf("%d is a positive number \n", number);
  15.     else
  16.         printf("%d is a negative number \n", number);
  17. }

$ cc pgm5.c
$ a.out
Enter a number
-10
-10 is a negative number
 
$ a.out
Enter a number
45
45 is a positive number

No comments:

Post a Comment