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?
r[5] and p[5], and the variables k, m, and s.for loop to accept 5 numbers for the first array.for loop to accept 5 numbers for the second array.for loop to add the corresponding elements of both arrays.s.#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(); }
for loop.for loop.6. Use two for loops to accept the elements and a third for loop to add the corresponding elements and display the result.#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(); }
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.