When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...
import java.util.Scanner;
public class DiagonalOfSquare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input: side length of the square
System.out.print("Enter the side length of the square: ");
double side = sc.nextDouble();
// Formula: diagonal = side × √2
double diagonal = side * Math.sqrt(2);
// Output
System.out.println("The diagonal of the square is: " + diagonal);
sc.close();
}
}
๐ง Explanation
-
A square has 4 equal sides and 2 equal diagonals.
-
The diagonal of a square can be calculated using the Pythagoras theorem:
[
\text{Diagonal} = \text{side} \times \sqrt{2}
] -
Math.sqrt(2)is used to get the square root of 2 in Java. -
The program takes the side length as input and prints the diagonal value.
๐งฎ Example Output
Enter the side length of the square: 5
The diagonal of the square is: 7.0710678118654755
Comments
Post a Comment