Monday, September 23, 2013

C PROGRAM TO CHECK WHETHER A NUMBER IS POSITIVE OR NEGATIVE

C PROGRAM TO CHECK WHETHER A NUMBER IS POSITIVE OR NEGATIVE

This program takes a number from user and checks whether that number is either positive or negative
The output of program should be as follows:

Output 1:
Enter a number: 12.3 
12.30 is positive.


OUTPUT2:
Enter a number: -12.3
-12.30 is negative.

Output 1:
Enter a number: 0 
You entered zero.


SOURCE CODE

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
void main()
{
 float num;
 printf("Enter a number: ");
 scanf("%f",&num);
 if (num<=0)
 {
 if (num==0)
printf("You entered zero.");
 else
 printf("%f is negative.",num);
 }
 else
 printf("%f is positive.",num);
getch();

}


This program can also be solved using else if statement

/* C programming code to check whether a number is negative or positive or zero using nested if...else statement. */

 #include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
void main()
{
 float num;
 printf("Enter a number: ");
 scanf("%f",&num);
 if (num<0) /* Checking whether num is less than 0*/
 printf("%f is negative.",num);
 else if (num>0) /* Checking whether num is greater than zero*/
 printf("%f is positive.",num);
else
printf("You entered zero.");
getch();
}



No comments:

Post a Comment