Perfect Squares – Till N

                    Perfect Square till N Java, Cpp

 

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 1:
Input:
20
Output:
1 4 9 16
Example Input/Output 2:
Input:
1
Output:
1

Code: Perfect Square till N java, cpp

C:
 
#include<stdio.h>
void printSquare(int N)
{
    int i=1,x=0;
    while(x<N)
    {
        x=i*i;
        if(x<=N)
        printf("%d ",x);
        i++;
    }
}
void main()
{
    int N;
    scanf("%d",&N);
    printSquare(N);
}
JAVA:
import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        printSquare(N);

    }

static void printSquare(int N) {
    int i=1,x=0;
    while(x<N)
    {
        x=i*i;
        if(x<=N)
            System.out.print(x+" ");
            i++;
    }
}
}

 

 
 
Please do comment If u have any Queries!
One Comment

Add a Comment

Your email address will not be published. Required fields are marked *