C program to find the squares of natural numbers from 1 to n

C program to find the squares of natural numbers from 1 to n:
Natural numbers
are all the positive integers that span from 1 to ∞.

The Square of a number is the product of that integer with itself.

/*C program to find the squares of natural numbers 
from 1 to n*/

#include <stdio.h>

void printSquares(int n)
{
    int sq = 1;
    //Check number is positive and greater than 0
    if (n < 1)
    {
        printf("Please enter number greater than 0!");
    }
    // Calculate and print squares
    for (int i = 1; i <= n; i++)
    {
        sq = i * i;
        printf("Square of %d = %d\n", i, sq);
    }
}

int main()
{
    int num;
    printf(" Enter the number:");
    scanf("%d", &num);
    printSquares(num);
    return 0;
}
$ gcc square.c
$ ./a.out
Enter the number:-2
Please enter number greater than 0!
$ ./a.out
Enter the number:10
Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
Square of 4 = 16
Square of 5 = 25
Square of 6 = 36
Square of 7 = 49
Square of 8 = 64
Square of 9 = 81
Square of 10 = 100
$

If you like the post C program to find the squares of natural numbers from 1 to n please give your feedback and share the post. Thank you!


also see

C Programming language
Go Programming language
Linked List Array
Simplification Queue
DBMS Reasoning
Aptitude HTML
Previous articleSPPU FY B.SC ( COMPUTER SCIENCE ) 2013 PATTERN Question Bank
Next articleC program to find the squares of natural numbers using a do while statement

LEAVE A REPLY

Please enter your comment!
Please enter your name here