Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
22. Write a program that inputs two arrays and saves the sum of corresponding elements of these arrays in a third array and prints them.
/*
File: Prgrm22.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to input two array and save the sum of corresponding elements into
the third array.
the third array.
*/
#include <stdio.h>
int main (void)
{
int first_array [5], second_array [5], sum_array [5];
return 0;
int i;
//Input elements of first array
printf ("***Input elements of first array***\n");
for (i = 0; i < 5; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &first_array [i]);
}
//Input elements of second array
printf ("\n***Input elements of second array***\n");
for (i = 0; i < 5; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &second_array [i]);
}
//Finding the sum of corresponding elements and save into third array
for (i = 0; i < 5; i++)
{
sum_array [i] = first_array [i] + second_array [i];
}
//Output the result
printf ("\n***Array having the sum of corresponding elements***\n");
for (i = 0; i < 5; i++)
{
printf ("%d ", sum_array [i]);
}
//Input elements of first array
printf ("***Input elements of first array***\n");
for (i = 0; i < 5; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &first_array [i]);
}
//Input elements of second array
printf ("\n***Input elements of second array***\n");
for (i = 0; i < 5; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &second_array [i]);
}
for (i = 0; i < 5; i++)
{
sum_array [i] = first_array [i] + second_array [i];
}
//Output the result
printf ("\n***Array having the sum of corresponding elements***\n");
for (i = 0; i < 5; i++)
{
printf ("%d ", sum_array [i]);
}
};
Output
***Input elements of first array***
Input 1 element: 23
Input 2 element: 64
Input 3 element: 39
Input 4 element: 56
Input 5 element: 38
***Input elements of second array***
Input 1 element: 36
Input 2 element: 96
Input 3 element: 43
Input 4 element: 62
Input 5 element: 47
Input 1 element: 23
Input 2 element: 64
Input 3 element: 39
Input 4 element: 56
Input 5 element: 38
***Input elements of second array***
Input 1 element: 36
Input 2 element: 96
Input 3 element: 43
Input 4 element: 62
Input 5 element: 47
***Array having the sum of corresponding elements***
59 160 82 118 85
No comments:
Post a Comment
Please do not post spam links.