Sum of Two Numbers in C using Function | Pointers | Array | Recursion
Sum of two numbers in C using function, pointers, array, and recursion.
In this article, you will learn how to find sum of two numbers in c using function, pointers, array, and recursion with and without minimum variables.
Example
Enter two integer values::
5
7
Result:: 5 + 7 = 12
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C Data Types
- C Functions
- C Pointers
- C Array
- C For loop
In this article, we solved this problem in six methods:
- Using the normal calculation
- With minimum variable
- Using the Function
- Using the Pointers
- Using the Array
- Using the Recursion
Sum of Two Numbers in C Program
// Sum of Two Numbers in C Program
#include <stdio.h>
int main() {
int p, q, r;
printf("Enter two integer values::\n\n");
scanf("%d %d", &p, &q);
// calculating sum
r = p + q;
printf("Result:: %d + %d = %d\n", p, q, r);
return 0;
}
Output
Enter two integer values::
5
7
Result:: 5 + 7 = 12
Sum of Two Numbers in C with Minimum Variable
// Sum of Two Numbers in C with Minimum Variable
#include <stdio.h>
int main() {
int p, q;
printf("Enter two integer values::\n\n");
scanf("%d %d", &p, &q);
printf("Result:: %d + %d = ", p, q);
// calculating sum
p = p + q;
printf("%d\n", p);
return 0;
}
Output
Enter two integer values::
12
22
Result:: 12 + 22 = 34
Explanation
In this given program, we have taken inputs 12
and 22
from the user then we applied arithmetic to make a sum on these values.
Then this will return the 34
the final output of the above calculation.
Sum of Two Numbers in C using Function
// Sum of Two Numbers in C using Function
#include <stdio.h>
// This function will return sum of two integer numbers
int Sum(int a, int b) {
return a + b;
}
// It's the driver function
int main() {
int p, q;
printf("Enter two integer values::\n");
scanf("%d %d", &p, &q);
printf("Result:: %d + %d = %d\n", p, q, Sum(p, q));
return 0;
}
You should also read these things
C Program to Find Sum of N Numbers using For loop
Sum of Digits of a Number in C using While loop
C Program to Find Sum of Natural Numbers using Recursion
Sum of Two Numbers in C using Pointers
// Sum of Two Numbers in C using Pointers
#include <stdio.h>
int main() {
int p, q, r;
int *a, *b;
printf("Enter two integer values::\n");
scanf("%d %d", &p, &q);
// To get the address of `p` and `q` variable
a = &p;
b = &q;
// calculating sum
r = *a + *b;
printf("Result:: %d + %d = %d\n", *a, *b, r);
return 0;
}
Enter two integer values::
34
54
Result:: 34 + 54 = 88
Sum of Two Numbers in C using Array
// Sum of Two Numbers in C using Array
#include <stdio.h>
int main() {
int sum = 0, s = 0;
printf("Enter the number of inputs: ");
scanf("%d", &s);
// To store the numbers
int numbers[s];
// To store the input values into the Array
printf("\nEnter the %d integer values:\n", s);
for (int i = 0; i < s; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}
// Final output
printf("\nResult:: ");
for (int i = 0; i < s; i++) {
printf("%d", numbers[i]);
if (i < s - 1)
printf(" + ");
}
printf(" = %d\n", sum);
return 0;
}
Enter the number of inputs: 5
Enter the 5 integer values:
23
34
2
54
98
Result:: 23 + 34 + 2 + 54 + 98 = 211
Sum of Two Numbers in C using Recursion
// Sum of Two Numbers in C using Recursion
#include <stdio.h>
int sum;
// This function will return sum of two integer numbers
int Sum(int a, int b) {
if (b == 0)
return a;
// Recursion: adding 1, N times and, then at the end adding m to it
sum = Sum(a, b - 1) + 1;
return sum;
}
// It's the driver function
int main() {
int p, q;
printf("Enter two integer values::\n");
scanf("%d %d", &p, &q);
printf("Result:: %d + %d = %d\n", p, q, Sum(p, q));
return 0;
}
Output
Enter two integer values::
10
99
Result:: 10 + 99 = 109
Sum of Two Float Numbers in C Program
// Sum of Two Float Numbers in C Program
#include <stdio.h>
int main() {
float p, q;
printf("Enter two float values::\n\n");
scanf("%f %f", &p, &q);
printf("Result:: %f + %f = ", p, q);
// calculating sum
p = p + q;
printf("%f\n", p);
return 0;
}
Output
Enter two float values::
5.23
7.29
Result:: 5.230000 + 7.290000 = 12.520000
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student