Write a C program to print a square of an array of elements

Write a C program to print a square of an array of elements, in this C program example, we will see how to print a square of an array of elements.

Problem Statement:

Let’s suppose we have an array[1,2,4] and you have to write c code to print a square of an array [1,4,16].

Sample input

Enter the size of an array: 4

Enter elements:

8

16

25

50

Sample output

square of element is:64
square of element is:256
square of element is:625
square of element is:2500

Algorithm to print a square of an array of elements

  • Declare one array and three variables.
  • Take two inputs first for the size of an array another for elements in the array.
  • Then, take for loop, one for input and another for the logic of square.
  • Print an appropriate message.

C program to print a square of an array of elements

#include<stdio.h>
 int main (){  
    int n,i,a[100],squ;
    printf("enter the size of an array :");
    scanf("%d",&n);
    printf("enter the elements :\n");
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++){
    squ=a[i]*a[i];	
    printf("square of the element:%d\n",squ);
    }
    
 return 0;
}

Explanation of this C program

Step 1: Start.

Step 2: Create a header file and include the library in it.

Step 3: Then create an int main function with return zero in last.

Step 4: Declare one array (a[100]) and three variables (n,i,squ).

Step 5: Create two inputs one for the size of an array and another in for the loop for taking elements in array take input with the help of scanf.

Step 6: Create two foo loops, first for the input of elements and the second one for the logic of square (squ=a[i]*a[i]).

Step 7: Give all the conditions in the loop.

Step 8: Print an appropriate message with the help of print.

Step 9: Create return=0, because we will take int main function.

Step 10: End.

The output of this C program

In this way, we learned how to write a C program to print a square of an array of elements.