C Program to Print the Square of Array Elements

C Program to Input an Array, Store the Squares of these Elements in an Array & Print it

Problem Description

This is a C program which inputs array & stores the squares of these elements in an array & print them.

Problem Solution

1. Create a two-dimentional array of define its elements statically.
2. Run a for loop, the number of rows in this two-dimentional array times.
3. Inside this for loop, a function is called passing every row of the 2D array as a parameter(i.e passing 1D array).
4. Inside this function, value of every column of that row is multiplied by its own number, and stored in the same place, thus storing the square of the previously stored number.
5. Now print all the elements of the 2D array.

Program/Source Code

Here is source code of the C Program to input array & stores the of these elements in an array & print them. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C Program to Input an Array, Store the Squares of these Elements in an Array & Print it
  3.      */
  5.     #include <stdio.h>
  6.     #define MAX_ROWS 3
  7.     #define MAX_COLS 4
  9.     void print_square(int [ ] );
  10.     void main (void)
  11.     {
  13.         int i;
  14.         int num [MAX_ROWS][MAX_COLS] = { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120} };
  15.         for (i = 0; i < MAX_ROWS; i++)
  16.             print_square(num[i]);
  18.     }
  20.     void print_square(int x[ ])
  21.     {
  23.         int j;
  24.         for (j = 0; j < MAX_COLS; j++)
  25.             printf ("%d\t", x[j] * x[j]);
  26.         printf("\n");
  28.     }
Program Explanation

1. Declare a 2D array of some fixed row and column size (lets say 3 and 4 respectively).
2. Define all its elements at the time of declaration.
3. Run a for loop from 0 to row-1, sending each row of the 2D array in the function printSquare() as its parameter.
4. Inside this function, again a loop runs to access all the column values of the given row.
5. Thus the loop runs from 0 to column-1, accessing each column value, evaluating its square, storing the result at the same place.
6. Now, print the array.

Runtime Test Cases
100    400    900    1600
2500   3600   4900   6400
8100   10000  12100  14400

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.