• Register
3 votes
4k 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.

Thanks for your help.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

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

13 9 6
94,240 points

Related questions

0 votes
1 answer 30 views
30 views
Problem: any help : Write a program that squares an integer and prints the result..
asked Mar 6 Wafa Abu Yousef 6.1k points
1 vote
1 answer 85 views
85 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.
asked Mar 28, 2020 Gavin 15.3k points
0 votes
1 answer 13 views
13 views
Problem: I am trying to write a java program to create an ArrayList, add all the months of a year, and print the months. I have successfully written a program by initializing a string array with names of the months and then adding the months in ArrayList by using a for a loop. But I want to know, is there any efficient way of doing this? A demo program would be much helpful. Thanks!
asked Feb 28 Code Learner 9.2k points
0 votes
1 answer 280 views
280 views
Problem : Write an expression that prints "special number" if specialnum is -99, 0, or 44.
asked Jan 14, 2020 alecxe 7.5k points
0 votes
1 answer 2 views
2 views
Problem: can someone please help for this problem? write a program that prompts the user to enter an integer
asked 4 days ago Rohit kr 16.2k points
0 votes
1 answer 64 views
64 views
Problem: I do not know what I am doing wrong. I want to write a program that prints numbers from 0 to the value entered by the user. There must be one space between two numbers. Here is my attempt: import java.util.Scanner; public class Main { public static void main ... When I run this program, it keeps running unless I terminate it manually. What is the issue in my code and how can I solve that?
asked Feb 27 Code Learner 9.2k points
1 vote
1 answer 879 views
879 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.
asked Jun 10, 2020 samhaz 5k points
0 votes
1 answer 177 views
177 views
Problems: I need help in programming to solve my issue. Is there anyone who can help me ? Write a complete main method that prints Hello, world to the screen?
asked Feb 13, 2020 maddi86 5.4k points
0 votes
1 answer 180 views
180 views
Problem: I am finding code and not able to used right code so that I can get right output, can anyone tell the right code? Rearrange the code so that it forms a correct program that prints out the letter q:?
asked Feb 19, 2020 maddi86 5.4k points