Print perfect square numbers in a given range

Prime Number:- Perfect Square Number means the product of two same natural numbers.

Example:-

4,9,16.....

Problem statement:- Program to Find out all perfect square numbers present within a given range.

Sample Input/Output:-

Sample Input First: 2 20                      Sample Output First: 4 9 16 

Sample Input Second: 50 100             Sample Output Second64 81 100

Data requirement:-

   Input Data:- fristnumber, endnumber

  Output Data:-i

  Additional Data:- sqr

Program in C
  
Here is the source code of the C Program to Find out all perfect square numbers present within a given range.

Code:

#include <stdio.h>
#include<math.h>
int main()
{
  int i, firstnum, endnumber;
  printf ("Enter the first number:");
  scanf ("%d", &firstnum);
  printf ("Enter the second number:");
  scanf ("%d", &endnumber);

  printf ("perfect squares between %d and %d: \n", firstnum, endnumber);
   for(i=firstnum;i<=endnumber;i++)
    {
    double sqr=sqrt(i);
   if(sqr-floor(sqr)==0)
     printf("%d ",i);
    }
  return 0;
}

Input/Output:
Enter the first number:
2
Enter the second number:20
perfect squares between 2 and 20:
4 9 16 

Program in C++
  
Here is the source code of the C++ Program to print perfect square numbers in a given range.

Code:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int range1,range2;
    cout<<"Enter a range:";
    cin>>range1>>range2;
    int count=0;
    cout<<"Perfect numbers between "<<range1<<" and "<<range2<<" are: ";
    for(int i=range1;i<=range2;i++)
    {
    double sqr=sqrt(i);

   if(sqr-floor(sqr)==0)
     cout<<i<<" ";
    }
}

Input/Output:
Enter a range:
50
100
Perfect numbers between 50 and 100 are: 64 81 100

Program in Java
  
Here is the source code of the Java Program to Find out all perfect square numbers present within a given range.

Code:

import java.util.Scanner;
public class PerfectSquareNumberInRange {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int firstnumber, endnumber, i;
       System.out.println("Enter the First number:");
       firstnumber=cs.nextInt();
       System.out.println("Enter the Last number:");
       endnumber=cs.nextInt();
System.out.println("Perfect squares numbers between "+fristnumber+" and "+endnumber+" are:");
   for(i=firstnumber;i<=endnumber;i++)
    {
    double sqr=Math.sqrt(i);

   if(sqr-Math.floor(sqr)==0)
     System.out.print(i+" ");
    }
cs.close();
}
}

Input/Output:
Enter the First number:
200
Enter the Last number:
300
Perfect squares numbers between 200 and 300 are:
225 256 289 

Program in Python
  
Here is the source code of the Python Program to print perfect square numbers in a given range.

Code:

import math
print("Enter range:")
range1=int(input())
range2=int(input())
print("Perfect squares between ",range1," and ",range2," are: ")
for i in range(range1,range2+1):
    sqr=math.sqrt(i)
    if sqr-math.floor(sqr)==0:
        print(i,end=" ") 

Input/Output:
Enter range:
300
500
Perfect squares between  300  and  500  are: 
324 361 400 441 484 










Post a Comment

0 Comments