Ad Code

Essential Array Programs in C Language: Print Unique Elements, Reverse, Copying, and More.

 Arrays Programs

In this article, we will cover some basic and essential programs for arrays using the C programming language. Arrays are an essential concept in programming, and learning how to work with them efficiently is a crucial skill for any programmer.

The first program we will cover is printing unique elements of an array. This program is useful when you need to identify and remove duplicates from an array. The next program we will cover is reversing an array. This program is useful when you need to display the array in reverse order or perform operations on the array in reverse order.

We will also cover the program for copying array1 to array2. This program is useful when you need to create a backup of an array or copy data from one array to another. Additionally, we will cover a program to find the largest element in an array, which is useful when you need to identify the highest value in an array.

Finally, we will cover a program to calculate the sum of elements in an array. This program is useful when you need to find the total value of all the elements in an array. By the end of this article, you will have a better understanding of how to work with arrays in the C programming language and will be able to apply these concepts to your own programs.



The array is a collection of the same types of data types like integer, float, character, etc.

Programs of Array:

#1 Print An Array.

 #include <stdio.h>
 int main()
 {
    int arr[5];
    arr[0]=1;
    arr[1]=9;
    arr[2]=5;
    arr[3]=2;
    arr[4]=4;
    int i;
    for(i=0;i<5;i++){
        printf("%d\n",arr[i]);
    }
    return 0;
 }

Output:

1
9
5
2
4

#2 Input from User:
 #include <stdio.h>

 int main()
 {
    int i,size;
    int arr[10];    
    printf("enter the size of array:");
    scanf("%d",&size);    
    printf("enter the elements of array:");
    for(i=0;i<size;i++){
        scanf("%d\n",&arr[i]);
    }    
    for(i=0;i<size;i++){
        printf("%d\n",arr[i]);
    }
    return 0;
 }

Output:

enter the size of array:4
enter the elements of array:1
2
4
5

1
2
4
5

#3 Print Unique Elements of an Arrays:
 #include <stdio.h>
 int main()
 {
    int i,j,size;
    int arr[20];
    int unique;    
    printf("enter the size of array:");
    scanf("%d",&size);
    printf("enter the elements of array:");
    for(i=0;i<size;i++){
        scanf("%d\n",&arr[i]);
    }    
    for(i=0;i<size;i++){
        unique = 0;
        for(j=0;j<i;j++){
         if(arr[i]==arr[j]){
             unique=1;
             break;
         }
        }
        if(unique==0){
            printf("%d\n",arr[i]);
        }
    }    
    return 0;
 }

Output:

enter the size of array:6
enter the elements of array:1
2
2
4
5
5

1
2
4
5

#4 Reverse an Array:
 #include <stdio.h>
 int main()
 {
    int i,j,size;
    int arr[20];   
    printf("enter the size of array:");
    scanf("%d",&size);   
    printf("enter the elements of array:\n");
    for(i=0;i<size;i++){
        scanf("%d\n",&arr[i]);
    }    
    printf("Reverse an array:\n");
    for(i=size-1;i>=0;i--){
        printf("%d\n",arr[i]);
    }    
    return 0;
 }

Output:

enter the size of array:6
enter the elements of array:1
2
2
4
5
6

Reverse an array:
6
5
4
2
2
1

#5 Copy Array1 to Array2 :
 #include <stdio.h>
 int main()
 {
    int i,j,size;
    int arr1[5],arr2[5]; 
    printf("enter the elements of arr1:\n");
    for(i=0;i<5;i++){
        scanf("%d\n",&arr1[i]);
    }    
    for(i=0;i<5;i++){
        arr2[i]=arr1[i];
    }
    printf("Copied elements:\n");
    for(i=0;i<5;i++){
        printf("%d\n",arr2[i]);
    }    
    return 0;
 }

Output:

enter the elements of arr1:
1
2
2
4
5


Copied elements:
1
2
2
4
5

Post a Comment

0 Comments

Ad Code