In: Computer Science
Implement if else loop to find out if the given number is positive or negative or zero. You would be hardcoding the number in the below example I used 8. But you can use a different number.
ans)
Explained using C program :
The code for above program is :
#include <stdio.h> ---------------
header files used in C ( to call built in function )
void main() {
double num;
printf("Enter the number ");
scanf("%lf", &num); ----------------take input from user
if (num <= 0.0) {
if (num == 0.0)
printf("you entered zero !");
else
printf("you entered a negative number !! ");
} else
printf("you entered a positive number!");
}
output :
Enter the number -2.34 ------------ displays on screen after RUN
you entered a negative number !!
EXPLANATION OF PROGRAM :
basic terms :
if-else statement in C : if the given condition in if statement evaluates as TRUE then inside the if-block get executed , otherwise the else block get executed.
double : - a data type used in C to accept the decimal valued numbers, the double is having double precision ( 64 bit ) floating-point representation ( more accuracy like : 0.01230393... )
- the variable num is declared using data type double, next the scanf() funtion is called ( to take input from user side ). in the scanf syntax, there must be %lf to accept the double value . after reading value and store it in num, the next statement,if- else get executed, if the num is less than or equal to zero the control goes to inside block and there is another if-else statement to print either zero or negative.
if the first condition is wrong then control will jumb to else part and the statement "entered a positive number get executed .