C Program To Find The Addition Of Two Integer Numbers

In This C Tutorial, we will learn C Program to add two Integer numbers.

Program 1: Simple C Program To Find The Addition of Two Integer Numbers.


/*
C Program To Find The Addition Of Two Integer Numbers.
Date: Sunday, 27-September-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 18.04 Lts/x64/gcc 7.5.0/Visual Studio Code
*/

/*
Algorithm:


Step 1: Start
Step 2: Define two header files "stdio.h" and "stdlib.h".
Step 3: Define main method / main function.
Step 4: Initialize two integer type variables as "num1 = 10" and "num2 = 20".
Step 5: Declare a integer type variable as "sum".
Step 6: Print the value of "num1".
Step 7: Print the value of "num2".
Step 8: Calculate the sum of "num1" and "num2" and store the result in the "sum"
variable.
Step 9: Print the result.
Step 10: End
*/


#include<stdio.h> //Header files
#include<stdlib.h>


//Main method or function
int main(){
int num1 = 10; //Variable initialization
int num2 = 20;
int sum; //Declare variable


system("clear");


/* 'clear' command is used for clear the console screen.
This command is present inside the "stdlib.h" header file.
In Windows machine, use "cls" command to clear the console.
*/


printf("First number : %d\n",num1);
printf("Second number : %d\n",num2);


//calculate the sum
sum = num1 + num2;


//print the result
printf("\a\n\nAddition of %d and %d is :: %d\n",num1,num2,sum);


//returning 0
return 0;
}


Save this file as "addition3.c"


Compile: $ gcc addition3.c -o addition3 [press the Enter key]


Execute:

$ ./addition3 <press the Enter key> [for Linux]
OR
$ addition3 <press the Enter key> [for Windows]


Output:


First number : 10
Second number : 20


Addition of 10 and 20 is :: 30

Program 2: Using Input / Output Function

/*
C Program To Find The Addition Of Two Integer Numbers Using Input / Output Function.
Date: Sunday, 27-September-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 18.04 Lts/x64/gcc 7.5.0/Visual Studio Code
*/

/*
Algorithm:


Step 1: Start
Step 2: Define two header files "stdio.h" and "stdlib.h".
Step 3: Define main method or function.
Step 4: Inside the main method, declare three integer type variables as
"num1", "num2" and "sum".
Step 5: Get the value of "num1" from the user.
Step 6: Get the value of "num2" from the user.
Step 7: Calculate the addition of num1 and num2 and store the result in
"sum" variable.
Step 8: Display the result on the screen.
Step 9: Print "Thanks" message.
Step 10: Returning 0.
Step 11: Stop
*/


#include<stdio.h> //header file
#include<stdlib.h>


//main method or function
int main(){


int num1, num2, sum; //declare integer type variables


system("clear");


//get the value of num1 from the user
printf("Please enter the first number : \n");
scanf("%d", &num1);


//get the value of num2 from the user
printf("Please enter the second number : \n");
scanf("%d", &num2);


//calculate sum
sum = num1 + num2;


//display the result
printf("\a\n\nThe sum of %d and %d is :: %d\n",num1,num2,sum);


printf("\n\nThanks for using my application...\n");


return 0;
}


Save this file as "addition4.c"


Compile: $ gcc addition4.c -o addition4 [press the Enter key]


Execute:

$ ./addition4 <press the Enter key> [for Linux]
OR
$ addition4 <press the Enter key> [for Windows]


Output:


Please enter the first number :
40
Please enter the second number :
65


The sum of 40 and 65 is :: 105

Program 3: Using C Functions

/*
C Program To Find The Addition Of Two Integer Numbers Using Functions.
Date: Sunday, 27-September-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 18.04 Lts/x64/gcc 7.5.0/Visual Studio Code
*/

/*
Algorithm:


Step 1: Start
Step 2: Define two header files "stdio.h" and "stdlib.h".
Step 3: Next, declare a function or prototype as "sum".
Step 4: Now, define the "sum" function.
Step 5: Inside the function, declare a integer type variable "result".
Step 6: Calculate the sum of a and b and store the result in the "result"
variable.
Step 7: Return the "result".
Step 8: Terminate the function.
Step 9: Define the main method or function.
Step 10: Inside the main method, declare three integer type variables as
"num1", "num2" and "addition".
Step 11: Get the value of "num1" and "num2" from the user.
Step 12: Call the function inside the main method.
Step 13: Display the result on the screen.
Step 14: Print "Thanks" message.
Step 15: Return 0
Step 16: Stop
*/


#include<stdio.h> //header file
#include<stdlib.h>


int sum (int, int); //function declatation or prototype


//defining function
int sum (int a, int b){


int result; //declare variable


//calculate sum of two numbers
result = a + b;


//returning result
return result;
}


//defining main method or main function
int main(){


int num1, num2, addition; //declare variable


system("clear");


printf("Please enter any two numbers :\n");


//getting two integer numbers as input from the user
scanf("%d %d", &num1, &num2);


//calling the function
addition = sum (num1, num2);


//print the result on the screen
printf("\a\n\nThe sum of %d and %d is :: %d\n", num1, num2, addition);


printf("\n\nThanks for using my appllication...\n");


//returning 0
return 0;
}


/*
Save this file as "addition5.c"


Compile: $ gcc addition5.c -o addition5 [press the Enter key]


Execute:

$ ./addition5 <press the Enter key> [for Linux]
OR
$ addition5 <press the Enter key> [for Windows]


Output:


Please enter any two numbers :
390
70


The sum of 390 and 70 is :: 460


Thanks for using my appllication...