RitinMalhotra

Perfect Square

Oct 29th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. /**
  2.  * This program inputs a number and checks whether the number entered is a perfect square or not.
  3.  */
  4. import java.util.Scanner;
  5. public class Perfect_Square
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         Scanner sc=new Scanner(System.in);
  10.         System.out.println("Please enter a number.");
  11.         int num=sc.nextInt();
  12.         int i,prod,flag=0;
  13.         for(i=1;i<=num;i++)
  14.         {
  15.             prod=i*i;
  16.             if(prod==num)
  17.             {
  18.                 flag=1;
  19.                 break;
  20.             }
  21.         }
  22.         if(flag==1)
  23.         {
  24.             System.out.println(num+" is a perfect square.");
  25.         }
  26.         else
  27.         {
  28.             System.out.println(num+" is not a perfect square.");
  29.         }
  30.         sc.close();
  31.     }
  32. }
Add Comment
Please, Sign In to add comment