Addition of Two Arrays


Explanation

Two arrays can be added by adding the corresponding elements of each array. In this program, the user enters 5 elements into the first array r[] and another 5 elements into the second array p[]. A for loop is then used to add the corresponding elements and display the result.

How It Works?

  • Step 1: Declare two integer arrays r[5] and p[5], and the variables k, m, and s.
  • Step 2: Use the first for loop to accept 5 numbers for the first array.
  • Step 3: Use the second for loop to accept 5 numbers for the second array.
  • Step 4: Use the third for loop to add the corresponding elements of both arrays.
  • Step 5: Store the result in the variable s.
  • Step 6: Display the addition of each corresponding pair of elements.

Example Program


#include <stdio.h>
#include <conio.h>

main()
{

    int r[5], p[5], s;
    int k, m;

    clrscr();

    s = 0;

    for(k = 0; k <= 4; k++)
    {

        printf("Enter the Number : ");

        scanf("%d", &r[k]);

    }

    for(m = 0; m <= 4; m++)
    {

        printf("\nEnter the Number : ");

        scanf("%d", &p[m]);

    }

    for(m = 0; m <= 4; m++)
    {

        s = r[m] + p[m];

        printf("\nTotal is %d", s);

    }

    getch();

}

Output

Enter the Number : 10
Enter the Number : 20
Enter the Number : 30
Enter the Number : 40
Enter the Number : 50

Enter the Number : 1
Enter the Number : 2
Enter the Number : 3
Enter the Number : 4
Enter the Number : 5

Total is 11
Total is 22
Total is 33
Total is 44
Total is 55

Practice Exercises

Task 1: Write a C program to accept 6 elements into two arrays and display the sum of the corresponding elements using a for loop.

Goal: Learn how to store values in two arrays and perform element-by-element addition using a for loop.

💡 Hint: Declare two arrays of size 6. Use two for loops to accept the elements and a third for loop to add the corresponding elements and display the result.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{

    int r[6], p[6], s;
    int k;

    clrscr();

    for(k = 0; k <= 5; k++)
    {

        printf("Enter First Array Number : ");

        scanf("%d", &r[k]);

    }

    for(k = 0; k <= 5; k++)
    {

        printf("Enter Second Array Number : ");

        scanf("%d", &p[k]);

    }

    for(k = 0; k <= 5; k++)
    {

        s = r[k] + p[k];

        printf("\nTotal is %d", s);

    }

    getch();

}

Output
Enter First Array Number : 10
Enter First Array Number : 20
Enter First Array Number : 30
Enter First Array Number : 40
Enter First Array Number : 50
Enter First Array Number : 60

Enter Second Array Number : 1
Enter Second Array Number : 2
Enter Second Array Number : 3
Enter Second Array Number : 4
Enter Second Array Number : 5
Enter Second Array Number : 6

Total is 11
Total is 22
Total is 33
Total is 44
Total is 55
Total is 66
Explanation: The program declares two integer arrays of size 6. The first two for loops accept six elements for each array. The third for loop adds the corresponding elements of both arrays and displays the result for each pair of elements.