Friday, September 29, 2017

Perfect Squares - Till N

Given a positive integer N as the input, print all the perfect squares till N (inclusive of N).

Input Format:
The first line contains N.

Output Format:
The first line contains the perfect squares till N separated by a space.

Boundary Conditions: 1 <= N <= 9999999

Example Input/Output:
Input:
20
Output:
1 4 9 16

Solution:
#include <stdio.h>
void printSquare(int n)
{
 for(int i=1;i*i<=n;i++)
  printf("%d ",i*i);
}
void main()
{
int N;
scanf("%d",&N);
printSquare(N);
}
Share: