首頁  >  文章  >  Java  >  爪哇島的廣場

爪哇島的廣場

王林
王林原創
2024-08-30 16:26:31860瀏覽

當一個數字與自身相乘時,所形成的數字就是該數字的平方。數字的平方很容易找到。一般來說,每當我們求一個整數的平方根時,我們只能得到整數的結果。同樣,每當我們求十進制數的平方時,我們也會得到十進制的答案。關於數字平方的一個有趣的事實是,每當我們將整數平方時,所得數字的值就會增加。然而,當我們計算 0 和 1 之間的小數平方時,所得數字會減少。一個例子是 0.5 的平方。當我們平方 0.5 時,數字就會減少到 0.25。在本文中,我們將了解如何使用 Java 程式語言計算數字平方的各種方法。

工作– 在 Java 中可以透過多種技術求出數字的平方。我們希望看到一些與數字的平方相關的範例,透過這些範例我們可以更好地理解數字的平方。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

如何用Java計算Square?

讓我們學習如何用java計算平方:

範例#1

求數字平方最簡單的方法是Math.pow(),它可以用來計算數字的任意冪。

代碼:

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));
}
}

輸出:

爪哇島的廣場

範例#2

在下一個程式中,我們將以通常的形式計算一個數字的平方,以便將兩個數字順序相乘並找到相應數字的平方。

代碼:

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
}
}

輸出:

爪哇島的廣場

範例#3

在這個例子中,我們將檢查一個數字是否為完全平方數。這是一個有點複雜的程序,因為它檢查一個數字是否是另一個數字的平方。

代碼:

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");
}
}

輸出:

爪哇島的廣場

範例#4

在這個程式中,我們找到特定範圍內的平方數的數量。我們輸入數字範圍,程式碼將產生該特定範圍內的平方數。在下面的程式中,我們找到 0 到 100 之間的平方整數的數量。

代碼:

// 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);
}
}
}
}

輸出:

爪哇島的廣場

範例#5

在這個程式中,我們將看到前 N 個自然數的平方和。我們輸入 N 的值,程式計算前 N 個自然數的平方和。

代碼:

// 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));
}
}

輸出:

爪哇島的廣場

結論

  • 在本文中,我們看到了一系列方法,透過這些方法我們可以計算一個數字的平方,確定一個數字是否在特定範圍內平方,以及前 N 個自然數的整數總和。然而,還有一些其他技術可用來求數字的平方。可用於查看和檢查數字是否為平方的技術的名稱是遞歸技術,它使用函數來檢查數字是否為完全平方。
  • 雖然遞歸技術很難使用,但它可以在幾行程式碼內計算出數字的平方。此外,使用平方數,我們可以產生很多模式程式。我們可以列印螺旋格式或之字形格式的方形圖案。同樣,原始碼中可以使用平方數來產生雙平方,例如數字16,其中雙平方就是數字2。

以上是爪哇島的廣場的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn