Comparing two numbers

Comparing two numbers - C Programming Examples

The following description is an example C program code that compares two numbers according to input.

C Programming Code

    int sum, a,b,i;
    printf("Input the first number \n");
    printf("Input the second number \n");
        printf("%d is greater than %d", a,b);
    else if(a<b)
        printf("%d is smaller than %d",a,b);
        printf("they are equal!");

Output of the code

Comparing two numbers - C program example
Comparing two numbers - C program example

Explanation of the code

·         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 files. The 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>. On 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” form a program.
·         In C programming, 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 the function. The function returns control to main when a return statement is executed or when end of function is reached.
int sum,a,b,i;
·         Int is a variable or data type. A variable declared to be of type int (integer) can be used for storing integer numbers. Here we declare “sum”, “a”,”b” and “i” 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(“%d”,&a) and scanf(“%d”,&b)  is used here. So, this program will read in an integer value of “a” valuable that the user enters on the keyboard (%d is for the integer values. As there is “&a”, the number user enters will be the value of “a” valuable.
If loop
·         When if is used in a program, at least 1 condition is added in order to make a loop. It must be noted that the next part which is bound by the “if loop” must be separated by another pair of second bracket. There are two if loops in this program. Only one of these loops and its condition remains true.
Printf("%d is greater than %d", a,b);
Printf("%d is greater than %d", a,b);

·         As one of the conditions remain true, only one of these lines are printed. It depends on the numbers added by the user.

·         Getch() is added in order to stop the program. It is used in order to stop the program while the header file <conio.h> is used.

Final Result

This program will show us the comparison between two numbers which are inputted by the user.

No comments

Powered by Blogger.