Home » C solved programs » C one dimensional array programs

C program to add and subtract of Two One Dimensional Array elements

This program will read two One Dimensional Array and create third One Dimensional Array by adding and subtracting elements of inputted two One Dimensional Array elements.

Add and Subtract elements of Two One Dimensional Array using C program



/*program to add and subtract elements of two arrays.*/
 
#include<stdio.h>
#define MAX 20 
 
/*  function    :   readArray() 
    to read array elements. 
*/
 
void    readArray(int a[],int size) 
{ 
    int i; 
    for(i=0;i< size;i++) 
    { 
        printf("Enter %d element :",i+1); 
        scanf("%d",&a[i]); 
    } 
} 
 
/*  function    : printArray() 
    to print array elements. 
*/
void printArray(int a[],int size) 
{ 
    int i; 
    for(i=0;i < size; i++) 
        printf("%5d",a[i]); 
} 
 
/*  function    : addArray(), 
    to add elements of two arrays. 
*/
void addArray(int a[],int b[],int c[],int size) 
{ 
    int i; 
    for(i=0; i< size;i++) 
        c[i]=a[i]+b[i]; 
} 
  
/*  function    : subArray(), 
    to subtract elements of two arrays. 
*/
void subArray(int a[],int b[],int c[],int size) 
{ 
        int i; 
        for(i=0; i< size;i++) 
                c[i]=a[i]-b[i]; 
} 
 
int main() 
{ 
    int A[MAX],B[MAX],ADD[MAX],SUB[MAX]; 
    int i,n; 
 
 
    printf("\nEnter size of an Array :"); 
    scanf("%d",&n); 
 
    printf("\nEnter elements of Array 1:\n"); 
    readArray(A,n); 
    printf("\nEnter elements of Array 2:\n"); 
    readArray(B,n); 
 
    /* add Arrays*/
    addArray(A,B,ADD,n); 
    /* subtract two Arrays*/
    subArray(A,B,SUB,n); 
 
    printf("\nArray elements after adding :\n"); 
    printArray(ADD,n); 
 
    printf("\nArray elements after subtracting :\n"); 
    printArray(SUB,n); 
 
    printf("\n\n"); 
    return 0; 
}

Output

    Enter size of an Array :5 

    Enter elements of Array 1: 
    Enter 1 element :12 
    Enter 2 element :23 
    Enter 3 element :34 
    Enter 4 element :45 
    Enter 5 element :56 

    Enter elements of Array 2: 
    Enter 1 element :11 
    Enter 2 element :22 
    Enter 3 element :33 
    Enter 4 element :44 
    Enter 5 element :55 

    Array elements after adding : 
       23   45   67   89  111 
    Array elements after subtracting : 
        1    1    1    1    1 





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.