Previous Page C Programming Home Next Page
Q1. Write a C Program to take input for two numbers calculate and print their sum?
/*
Sum of two numbers
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
clrscr();
printf(“Enter 2 nos “);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(“Sum = %d\n”,c);
getch();
}
Explanation Of above Program
Line :1,2 and 3
/*
Sum of two numbers
*/
These are remark statements which helps to specify the objective of the program in hand. (this is optional but a good habit to write)
Line : 4 and 5
#include<stdio.h>
#include<conio.h>
these are header files. These contain details (prototypes) of function used in the program. if we use any function in the program whose details are not present in the included header files then an error will get generated.
What do header file contain?
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
Line 6:
int main()
Each and every C program is made up of functions. there can be any number of functions in a single program. but there has top be at least one function in each and every C program. and if there is only one function in the program then it will be the function named main(). As this is the starting point of execution of any “C” program. The program is compiled from top to bottom but execution of the program starts from the function named main().
Line:7 and 15
{
}
This
defines the body of the function main. This also defines the logical starting
and ending point of the program. There can be any number of statement in the
body of the function.
Line: 8
int a,b,c;
On
execution of this statement, three memory location named “a”
,”b” and “c” are created in the memory and it is also
specified that these can store numeric value without decimal as the data type
is given as “int”.
Line: 9
clrscr();
this function helps us to clear the screen.
Header file
: conio.h
Line:10
printf(“Enter 2 nos “);
printf():
this function helps us to display any text, value of a variable and/or value of
an expression on standard output device(screen/monitor).
Header file: stdio.h
Line: 11
scanf(“%d %d”,&a,&b);
scanf():
this function helps us to take input for value of a variable through standard
input device(keyboard).
header file : stdio.h
%d: format specifier for “int”
& : address of
So the above statement says, take input for two values of “int” type
and store them in the address of variables “a” and “b”.
Line : 12
c=a+b;
on execution of this statement sum of values stored in variables “a” and “b” is assigned to variable “c”.
Line :13
printf(“Sum = %d\n”,c);
displays the result as
(if values entered are a=10 and b=20)
Sum = 20
_
“Sum =” is displayed as it is and value of
variable “c” is displayed in place of “%d” further the
cursor is taken to next line as ‘\n’ new character is used.
Line :14
getch();
this function helps us to take input for a single character
and it is written at the end (in turboc3) so that after execution of all the
statements the computer should wait for a single character to be pressed and we
can easily see the output.
We can also use Alt+F5 combination keys to view the output of the program after
execution of the program.