C Program to Sum Corresponding Elements of Two Arrays. In The world of Computer Science, Manipulating Arrays is a fundamental concept that finds Application in various scenarios. One such scenario is adding corresponding elements of two arrays and storing the result in a third array. In this article, we will write a C program that adds corresponding elements of two arrays.
Table of Contents
Problem Statement: WAP inputs two arrays and saves the sum of corresponding elements of these arrays in a third array and prints them.
Understanding the Task
The task is to write a C program that perform the following steps.
- Takes input for two arrays from the user.
- Add corresponding elements of these two arrays.
- Stores the result in a third array.
- Prints the elements of the resultant array.
C Program to Sum Corresponding Elements of Two Arrays
Let’s write a C Program to Sum Corresponding Elements of Two Arrays. Here we are assuming that both input array contains the same number of elements.
#include <stdio.h>
void main()
{
int n;
printf("Enter the number of elements in array ");
scanf("%d", &n);
int array1[n], array2[n], result[n];
// Taking input for the first array
printf("Enter the elements of the first array \n");
for (int i = 0; i < n; i++)
{
scanf("%d", &array1[i]);
}
// Taking input for the second array
printf("Enter the elements of the second array \n");
for (int i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}
// Adding corresponding elements
for (int i = 0; i < n; i++)
{
result[i] = array1[i] + array2[i];
}
// Printing the sum of corresponding array
printf("Sum of corresponding elements\n");
for (int i = 0; i < n; i++)
{
printf("%d ", result[i]);
}
}
Output
In the above C program, we are taking two arrays of input from the user. After that we calculate the sum of corresponding elements by iterating through the array and at the end we print the resultant array. This problem serves as a solid foundation for more complex operations involving arrays and user input in C programming.
Happy Coding & Learning
2 thoughts on “C Program to Sum Corresponding Elements of Two Arrays”