write a program to display all numbers between m and n input from the keyboard (where m0, n>0), check and print the numbers that are a perfect square. e.g. 25, 36, 49, ..... are said to be a perfect square numbers
Answers
C program to display all numbers between m and n input from the keyboard (where m0, n>0), check and print the numbers that are a perfect square
Explanation:
Hence, the perfect squares are numbers : 1, 4, 9, 16, 25, etc.
C Program To Find Perfect Squares Between Two Numbers
#include<stdio.h>
#include<math.h>
int main(){
int i,j,m,n;
float ps;
printf("enter the first number\n");
scanf("%d",&m);
printf("enter the last number\n");
scanf("%d",&n);
printf("perfect squares between %d and %d:",m,n);
for(j=m+1;j<n;j++){
ps=sqrt(j);
i=(int)ps;
if(i==ps){
printf("%d\n",j);
}
}
return 0;
}
Output
enter the first number
5
enter the last number
100
perfect squares between 5 and 100: 9
16
25
36
49
64
81
Answer:
import java.util.*;
import java.io.*;
public class Program {
public static void main(String []args) throws IOException{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.print("Enter m: ");
int m = Integer.parseInt(in.readLine());
System.out.print("Enter n: ");
int n = Integer.parseInt(in.readLine());
if (m < n && m > 0 && n > 0) {
for (int i = m; i <= n; i++) {
System.out.println("Number = " + i);
double sroot = Math.sqrt(i);
if (sroot == Math.floor(sroot))
System.out.println(i + " is a perfect square");
}
}
else {
System.out.println("Invalid input");
}
}}
Explanation:
Enter m: 10
Enter n: 20
Number = 10
Number = 11
Number = 12
Number = 13
Number = 14
Number = 15
Number = 16
16 is a perfect square
Number = 17
Number = 18
Number = 19
Number = 20