Read : How to use Scanf function in c program
C Program To Add Two Numbers Using Addition Operator
#include<stdio.h> #include<conio.h> void main() { int a,b,add; clrscr(); printf("\n Enter Any Two Numbers To Add : "); scanf("%d%d",&a,&b); add=a+b; printf("\n Addition = %d",add); getch(); }
Output :
Watch the video below For Better Understanding of this program.
C Program For Dev C++ to Add Two Numbers
#include<stdio.h> #include<conio.h> main() { int a,b,add; printf("\n Enter Any Two Numbers To Add : "); scanf("%d%d",&a,&b); // get the values by user add=a+b; // addtion is performed and stored in "add" printf("\n Addition = %d",add); // display output getch(); return 0; }
Output :
We can Also Perform the addition of two numbers using functions in C for that we will create a function named as 'add'(it is not necessary to name it as add you can call it whatever you like) define the function appropriately and call it from the main function as shown below :
C program to add two numbers using function:
#include<stdio.h> #include<conio.h> int add(int a,int b); //function declaration void main() { int sum,x,y; clrscr(); printf("\n Enter Any Two Numbers To Add : "); scanf("%d%d",&x,&y); sum=add(x,y); //calling the function printf("\n Addition = %d",sum); getch(); } int add(int a, int b) //definition of function { int c; c=a+b; return(c); }
We can also use pointers to perform this addition in C like below :
Add two numbers using pointers in C
#include<stdio.h> #include<conio.h> void main() { int *p1,*p2,a,b,add; clrscr(); printf("\n Enter Any Two Numbers To Add : "); scanf("%d%d",&a,&b); p1=&a; p2=&b; add= *p1 + *p2; printf("\n Addition = %d",add); getch(); }
In this way, we can add two numbers in C using functions and pointers.