Mar 19, 2007

C program to add two given numbers

how-to-add-two-numbers-in-c-languageIn C language how to add two given integer numbers I will show you this by a simple example.It will add two integer values. Every C program must have a main() function. then your program will be in starting curly bracket { and } ending curly bracket. Here we want to add two numbers a and b and result of addition will be stored in c. So here we declare three integer variables a,b & c. clrscr() function is a clear screen function that will clears the screen. printf() function will print a message "enter any number" on the screen to the user to input a number from the keyboard. scanf() function is used to input a value from the keyboard. When this statement is executed then user has to input a value from the keyboard. Value input by the keyboard will be stored in variable a. Same way value of b is to be stored into variable b. In scanf() function, we have used %d format string. %d is for integer means we have to input a integer from the keyboard. other format string are as follows that can be used in the program.
Format Strings:

%d - integer

%f - float

%c - character

Then we come to the statement c=a+b. This statement will add the value of a and b and store into variable c. then printf() statement will print the value of c variable on the screen. Here + is an arithmetic operator. More arithmetic operators can be used such as - (subtract), * (Multiply), / (Divide), % (Mod or Remainder). getch() statement is a get character statement. It pause the output until user presses a key from the keyboard.

#include<stdio.h> // standard input output header file
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("enter any number");
scanf("%d",&a); // or cin >> a
printf("enter any number");
scanf("%d",&b);
c=a+b;
printf("%d",c);
getch();
}

Other C & C++ Programs:

No comments:

Post a Comment

Infolinks In Text Ads

Total Pageviews

Powered by Blogger.

Dont Forget To Follow Us

Blog Archive