Best Cell Phones

Positive or Negative

Positive or Negative Number - C Programming Example

This C program code takes a number as an input and as an output this code says whether the number is positive, negative or neutral. 
C Programming Example
Positive and Negative number

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    float a;
    printf("Input the number \n");
    scanf("%f",&a);
    if(a>0)
    {
        printf("The number is positive \n");
    }
    if (a<0)
    {
        printf("The number is negative \n");
    }
    if (a==0)
    {
        printf("It is a neutral number \n");
    }
    getch();
}


Output


C Programming Example
Negetive output
C Programming Example
Neutral Output
C Programming Example
Positive Output


EXPLANATION

·        #include<stdio.h>
#include<stdio.h>

Ø  This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand  <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.


·         main()
Ø  In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·            float a;
Ø  float is a variable or data type. A variable declared to be of type float can be used for storing numbers which have decimal points. Here we declare “a” variable in int type.
·         printf() function
Ø  C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·         scanf() function
Ø  This function is used to get input from the user of the program. scanf(“%f”,&a”) is used here. So, this program will read in a float value for “a” variable that the user enters on the keyboard (%f is for float values. As there is “&a”, the number user enters will be the value of “a” variable).
 if(a>0)
Here if loop is used and the condition a>0 is set within ( ) as it is customary to set the condition within the brackets. A>0 means that if the float number is greater than 0 then it will be a positive number.
    {
        printf("The number is positive \n");
    }
Is written like this in another second bracket cause after every loop a code within a second bracket is required. And if the conditions meet then the program will show that it is a positive number like written in the brackets after printf.
If the condition doesn’t mean then 2 more if loops are set where a<0 is a negative number and a==0 is a neutral number, the codes are written in the same way as explained above.
Then finally getch(); is written to end the code.







No comments

Powered by Blogger.