• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Java Tutorials

          • Cheat Sheet Java
          • Cheat Sheet Python
          • C# vs Js
        • Java Tutorials
        • Python Tutorials

          • Angular 5 vs Angular 4
          • Careers in Python
          • Kali Linux vs Ubuntu
        • Python Tutorials
        • Top Differences

          • Cheat Sheet JavaScript
          • Python Interview Questions
          • Cloud Computing or Virtualization
        • Top Differences
        • Others

          • Resources (A-Z)
          • Top Interview Question
          • Programming Languages
          • Web Development Tools
          • HTML CSS Tutorial
          • Technology Basics
          • Technology Careers
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Software Development Course 2
        • All in One Bundle

          All-in-One-Software-Development-Bundle
        • Become a Python Developer

          Python-Certification-Training
        • Others

          • Java Course
          • Become a Selenium Automation Tester
          • Become an IoT Developer
          • Ruby on Rails Course
          • Angular JS Certification Training
          • View All
  • 600+ Courses All in One Bundle
  • Login

Squares in Java

Home » Software Development » Blog » Java Tutorials » Squares in Java

Squares in Java

Introduction to Squares in Java

When a number is multiplied by itself, the resulting number formed is the Square of the Number. Squares of a number are very easy to find. Generally, whenever we find the square root of an Integer number, we get the result in Integer only. Similarly, whenever we find the square of a decimal number, we get the answer in decimal as well. An interesting fact about the square of a number is that whenever we do a square of an integer number the value of the resulting number increases. However, when we do the square of decimals between 0 and 1, the resulting number decreases. An example would be that of a squaring of 0.5. When we square 0.5, the number gets decreased to 0.25. In this article, we are going to see the various methods of how we can square a number using the Java programming language.

Working – Square of a number can be found out in Java by a variety of techniques. We would like to see some examples related to the square of a number by which we can understand the square of a number better.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to calculate Square in Java?

Let us learn how to calculate square in java:

Example #1

The simplest way of finding the square of a number is Math.pow() where it can be used to calculate any power of a number.

Code:

import java.util.*;
public class Square
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;
System.out.print("Enter a number which is integer format: ");
num=sc.nextInt();
System.out.println("The square of "+ num + " is: "+ Math.pow(num, 2));
}
}

Output:

square in java-1

Example #2

In the next program, we are going to calculate the square of a number in the usual form such that it multiplies two numbers sequentially and finds the square of the respective number.

Code:

import java.util.*;
public class Square2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int no;
System.out.print("Enter a number which is integer format: ");
no=sc.nextInt();
System.out.println("Square of "+ no + " is: "+(no*no));//the number is multiplied with its own
}
}

Popular Course in this category
Cyber Week Sale
Java Training (38 Courses, 26 Projects) 38 Online Courses | 26 Hands-on Projects | 274+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (3,568 ratings)
Course Price

View Course

Related Courses
JavaScript Certification Training (39 Courses, 20 Projects)jQuery Training Training (8 Courses, 3+ Projects)

Output:

integer format

Example #3

In this example, we are going to check if a number is a perfect square or not. This is a little bit complex program as it checks if a number is a square of another number.

Code:

import java.util.Scanner;
class JavaExample {
static boolean checkPerfectSquare(double x)
{
// finding the square root of given number
double s= Math.sqrt(x);
return ((s - Math.floor(s)) == 0); //Math.floor() is used here to calculate the lower value.
}
public static void main(String[] args)
{
System.out.print("Enter any number:");
Scanner scanner = new Scanner(System.in);
double no= scanner.nextDouble();
scanner.close();
if (checkPerfectSquare(no))
System.out.print(no+ " is a perfect square number");
else
System.out.print(no+ " is not a perfect square number");
}
}

Output:

perfect square

Example #4

In this program, we find the number of square numbers within a specific range. We enter the range of numbers and the code would produce the square number in that specific range. In the below program, we find the number of square integers between 0 and 100.

Code:

// Finding the range of perfect square numbers in Java programming language
import java.io.IOException;
public class SquareNumbersInRange {
public static void main(String[] args) throws IOException {
int starting_number = 1;
int ending_number = 100;
System.out.println("Perfect Numbers between "+starting_number+ " and "+ending_number);
for (int i = starting_number; i <= ending_number; i++) {
int number = i;
int sqrt = (int) Math.sqrt(number);
if (sqrt * sqrt == number) {
System.out.println(number+ " = "+sqrt+"*"+sqrt);
}
}
}
}

Output:

system out

Example #5

In this program, we are going to see the sum of squares of the first N natural numbers. We enter the value of N and the program calculates the sum of squares of the first N natural numbers.

Code:

// Java Program to find sum of
// square of first n natural numbers
import java.io.*;
class SumofSquares
{
// Return the sum of the square of first n natural numbers
static int square sum(int n)
{
// Move the loop of I from 1 to n
// Finding square and then adding it to 1
int sum = 0;
for (int i = 1; i <= n; i++)
sum += (i * i);
return sum;
}
// Main() used to print the value of sum of squares
public static void main(String args[]) throws IOException
{
int n = 6;
System.out.println("The sum of squares where N value is 6 is "+ squaresum(n));
}
}

Output:

squaresome

Conclusion

  • In this article, we see a list of methods by which we can square a number, how we can find whether a number is square or not within a specific range and also the sum of integers of the first N natural numbers. However, there are also some other techniques that can be used to find the square of a number. The name of a technique that can be used to see and check if a number is square or not is the Recursion technique which uses a function within a function to check if the number is a perfect square or not.
  • Although the recursion technique is difficult to use, it can be used to calculate the square of a number within a few lines of code. Further, using square numbers we can generate a lot of pattern programs. We can print a square pattern in a spiral format or a zig-zag format. Similarly, the square numbers can be used in the source code to generate the double square such as the number 16 where the double square is number 2.

Recommended Articles

This is a guide to the Squares in Java. Here we have discussed the Introduction along Examples and codes with Output of Squares in Java. You can also go through our other suggested articles to learn more–

  1. Patterns in Java
  2. Cheat Sheet JavaScript
  3. What is Web Application?
  4. Types of Web Hosting
  5. Square Root in PHP
  6. Arrays in Java Programming
  7. Working and Top 3 Enum Methods in C#
  8. Square Root in JavaScript

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

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

Primary Sidebar
Technology Blog Tutorials
  • Java Tutorials
    • Hashing Function in Java
    • JavaFX Charts
    • JavaFX Applications
    • 2D Graphics in Java
    • Final Keyword in Java
    • Hibernate Framework
    • JavaFX Slider
    • jQuery querySelector
    • JProgressBar
    • Jslider
    • Print 2D Array in Java
    • JavaFX Button
    • JavaFX TextField
    • Regular Expressions in Java
    • JavaFX FileChooser
    • HashMap in Java
    • HashMap in Java
    • JTree in Java
    • JTextPane
    • What is JavaFX?
    • Iterator in Java
    • jQuery Elements
    • Hibernate Session
    • JavaFX Text
    • Slide in jQuery
    • jQuery plugins
    • Type Conversion in Java
    • Versions of Java
    • Overriding in Java
    • Overloading and Overriding in Java
    • Static Keyword in Java
    • Java Naming Conventions
    • Object in Java
    • Polymorphism in Java
    • Quick Sorting Algorithms in Java
    • JavaFX Label
    • Overloading in Java
    • JavaFX Layouts
    • Multidimensional Array in Java
    • JLabel in Java
    • Super Keyword in Java
    • Arrays in Java Programming
    • Java Booleans
    • Flowlayout in Java
    • Array Methods in Java
    • GridBagLayout in Java
    • JTextArea in Java
    • Protected Keyword in Java
    • JPopupMenu
    • JTabbedPane in Java
    • Inheritance in Java
    • Java Keywords
    • Fibonacci Series in Java
    • Java Deployment Tools
    • What is Angular 2?
    • For-Each loop in Java
    • Wrapper Class in Java
    • JFileChooser in Java
    • JFrame in Java
    • GroupLayout in Java
    • SpringLayout in Java
    • JToggleButton
    • Random Number Generator in Java
    • Recursion in Java
    • JList in Java
    • JColorChooser
    • JUnit Annotations
    • Squares in Java
    • JCheckBox in Java
    • Break Statement in Java
    • do-while loop in Java
    • Continue Statement in Java
    • JComponent in Java
    • Insertion Sort in Java
    • Palindrome in Java
    • Program for Merge Sort in Java
    • What is JNDI in Java?
    • Swing Components in Java
    • Java Virtual Machine
    • JMeter Alternatives
    • Sorting Algorithms in Java
    • Sorting in Java
    • JOptionPane in Java
    • Bubble Sort in Java
    • JEditorPane
    • Jquery IF Statement
    • For Loop in Java
    • Factorial in Java
    • Jspinner
    • JButton in Java
    • What’s New in Java 8?
    • Singleton Class in Java
    • JPanel in Java
    • Swapping in Java
    • jQuery Attributes
    • Java Annotations
    • JDialog in Java
    • JTextField in Java
    • JScrollPane in Java
    • 3D Arrays in Java
    • What is Java Garbage Collector?
    • Constructor in Java
    • Math Functions in Java
    • CardLayout in Java
    • Socket Programming in Java
    • Print Array in Java
    • Merge Sorting Algorithms in Java
    • StringBuffer in Java
    • JComboBox in Java
    • Reverse Number in Java
    • Square Root in Java
    • BorderLayout in Java
    • Java Packages
    • Quick Sort in Java
    • 2D Arrays in Java
    • Access Modifiers in Java
    • Case Statement in Java
    • Patterns in Java
    • Variables in Java
    • Encapsulation in Java
    • Selection Sort In Java
    • Types of Selector in Jquery
    • What is Java SE?
    • Abstract Class in Java
    • Thread Life cycle in Java
    • Loops in Java Programming
    • BoxLayout in Java
    • Gridlayout in Java
    • Destructor in Java
    • Heap Sort In Java
    • Bit Manipulation in Java
    • Best Java IDE
    • What is Servlet?
    • Best Java Compilers
    • Star Patterns in Java
    • While Loop in Java
    • Constructor and Destructor in Java
    • Merge Sort In Java
    • Copy Constructor In Java
    • What is Java Interface?
    • Frameworks In Java
    • Layout in Java
    • What is Synchronization in Java?
    • Testing Frameworks for Java
    • Java Tools
    • Jenkins Plugins
    • Java Compilers
    • JSP Directives
    • JSP Life Cycle
    • JSP Architecture
    • Java Programming Language Features
    • Servlet Life Cycle
    • Object Oriented Programming in Java
    • Applications of Java
    • Careers as Java Developer
    • String Functions in java
    • Is Javascript Case Sensitive
    • Jenkins Alternatives
    • What is Java Inheritance
    • Best Javascript Frameworks
    • Redux Alternatives
    • Grid Layout in Java
    • What is a Binary Tree in Java
    • Java App Development
    • What is Hibernate
    • What is Design Pattern in Java
    • What is API in Java
    • Java and JavaScript
    • Is Jenkins Free
    • What is JDK
    • What is JVM
    • What is AWT in Java
    • What is Swing
    • What is JSF
    • What is Spring Boot
    • What is Generics in Java
    • What is EJB
    • What is Jenkins
    • What is Java Hibernate
    • What Javascript Can Do
    • What is Concurrency in Java
    • What is Multithreading in java
    • What is JavaBeans
    • What is Redux
    • Template in Java
    • What is J2EE
    • JDBC Architecture
    • Cheat Sheet Java
    • Uses of Jenkins
    • What is Swift
    • Serialization in Java
    • What is JNI in Java
    • What is JSP
    • Uses of JavaScript
    • JSF Life Cycle
    • What is Maven Plugins
  • Database Management (71+)
  • Ethical Hacking Tutorial (33+)
  • HTML CSS Tutorial (47+)
  • Installation of Software (54+)
  • Top Interview question (188+)
  • JavaScript (71+)
  • Linux tutorial (32+)
  • Network Security (85+)
  • Programming Languages (232+)
  • Python Tutorials (89+)
  • Software Development Basics (321+)
  • Software Development Careers (38+)
  • SQL Tutorial (33+)
  • String Functions (12+)
  • Technology Commands (38+)
  • Top Differences (368+)
  • Web Development Tools (33+)
  • Mobile App (60+)
Technology Blog Courses
  • Java Course
  • JavaScript Certification Course
  • jQuery Training Course
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Course Programming
  • Free course Python
  • Free Course Java
  • Free Course Javascript
  • Free Course on SQL
  • Free Course on Web Design
  • Free HTML Course
  • Free Android App Development Course
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • Ruby on Rails Course
  • ASP.NET Course
  • VB.NET Course
  • Bootstrap Training Course
  • Become a Linux System Administrator
  • PHP Course
  • Joomla Training
  • HTML Course
Resources
  • Resources (A To Z)
  • Java Tutorials
  • Python Tutorials
  • Top Differences
  • Top Interview Question
  • Programming Languages
  • Web Development Tools
  • HTML CSS Tutorial
  • Technology Basics
  • Technology Careers
  • Ethical Hacking Tutorial
  • SQL Tutorials
  • Digital Marketing
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect