C Programming: Square number generator

This is an example of how to square numbers using a simple function in the C programming language.

file: square.c

#include <stdio.h>

int square(int x)
{
    int sq;

    sq = x * x;
    return sq;
}

int main()
{
    int i, count = 10;

    for(i = 1; i <= count; ++i)
    {
        printf("%dn", square(i));
    }

return(0);
}

Compile and run

$ gcc -o square square.c
$ ./square
1
4
9
16
25
36
49
64
81
100

Squaring a number is, of course, as simple as multiplying a number by itself. If you ever need to do this using the C programming language, the above is a simple proof of concept.

You may also like...