Write a program that squares an integer and prints the result.

2.8k views

Problem:

Hello good soul,

I am a student of basic java programming. I was solving beginner-level problems from there. Right now I got a problem that says, write a program that squares an integer and prints the result.

Well, I am not one of those who always look solution for their assignment on StackOverflow or other sites. I just want to learn the process.

1 vote

Solution:

We can do it in several ways. Let me show you some ways, and then you decide which one is best for you.

``````import java.util.Scanner;
public class SquareNumber {
private static Scanner roy;
public static void main(String[] args)
{
int num, sqr;
roy = new Scanner(System.in);
System.out.print(" Please Enter a Number : ");
num = roy.nextInt();
sqr = num * num;
System.out.println("The Square of the Given Number is: "+ square);
}
}``````

This is the easiest way I think. You just need to prompt a number from the user and multiply with itself (basics of squaring a number).

Using a method:

``````import java.util.Scanner;
public class SquareNumber1{
private static Scanner roy;
public static void main(String[] args)
{
int num, sqr;
num = new Scanner(System.in);
System.out.print("Please Enter a Number : ");
num = roy.nextInt();
sqr = calsquare(num);
System.out.println("The Square of the Given Number is: "+ square);
}

public static int calsquare(int number)
{
return number * number;
}
}``````

We did the same thing in our last program but calling a method only. What we’ve done in our entire program (first one), the function calsquare() did the same in our last program.

``````import java.util.*;
public class SquareNumber {
public static void main(String args[]){
Scanner roy = new Scanner(System.in);
int num;
System.out.print("Enter an integer number: ");
num=roy.nextInt();

System.out.println("Square of "+ num + " is: "+ Math.pow(num, 2));
}
}``````

This one is a pretty advance level. All you need to prompt a number and pass it through a built-in function Math.pow() to make it square. Keep in mind the parameters inside the function expressing the data type and executable power

Thanks

Related questions

1 vote
30 views
Problem: I have got an assignment from my online java class and doing the assignment and can't quite figure out what I'm doing wrong. I have to write a program that finds the sum of all squares between 1 and 100. As part of the assignment, I'm required to use a "while" ... ); a++; sum = (sum + a); } System.out.println(sum); } Thanks for your positiveness and effort. I must recognise your help.
30 views
215 views
Problem : Write an expression that prints "special number" if specialnum is -99, 0, or 44.
1 vote
325 views
Problem: I wrote a program in python that asks the user for a number n and prints the sum of the numbers 1 to n: x = 1, addition = 0 n = int(input('Please enter a number:')) for i in range(x, n): addition = addition + i print (addition) But when ... n): TypeError: 'tuple' object cannot be interpreted as an integer Is there any thing that is missing from my code. Please help me to solve this out.
35 views
Problem: how java provides a class Base64 to deal with encryption? You can encrypt and decrypt your data by using provided methods. You need to import java.util.Base64 in your source file to use its methods. how much class provides different encoders and decoders to encrypt information at each level.
90 views
Solutions: I am new in programming and my logic is not good, can anyone give the right solution? Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"?
1 vote
132 views
Problem: Can anyone provide me an expression that executes the loop body as long as the user enters a non-negative number. Thanks in advance.