Hello guys, if you are looking for Java program to calculate area of Square or program to calculate perimeter of square or just learning Java and looking for programming exercises then you have come to the right place. I have share many programming exercises and homework in this blog and today, I am going to share another simple programming assignment for beginners. Yes, we ae going to write a program which will calculate the area and perimeter of a square in Java. If you don't remember, area of a square is nothing but the multiplication of side with itself or square of side, while perimeter of a square is 4 times of its side because in case of square, each side is equal.
Along the way, you will also learn how to take user input in Java because we will make this program interactive. I mean, you will take input from user about the side of square from console and then print the area and perimeter into console as well.
This is a good programming exercise for anyone who wants to learn coding and programming. It will teach you how you can automate things, how you can create program which can do real stuff like calculating area of triangle, calculating interest, and checking if a year is leap year or not.
This kind of program will teach how to convert logic to code and also get familiar with essential programming construct like loops, if-else and conditional statement, function, class, method as well essential JDK API classes like Scanner which can be used to take user input.
Java Program to Calculate Area and Perimeter of a Square
Without wasting anymore of your time, here is a complete Java program which can calculate area of square for you. All the code for taking user input is inside main() method but code to calculate area and perimeter is encapsulate into two separate method area(int side) and perimeter(int side), both method take side as input and calculate area and perimeter and return it.
import java.util.Scanner;
/*
* Java Program to calculate the Area and Perimeter a given Square
*/
public class Main {
public static void main(String args[]) {
// accepting square's side as user input
Scanner scanner = new Scanner(System.in);
System.out.println("Hello there");
System.out.println("Please enter length or side of square :");
int side = scanner.nextInt();
int perimeter = perimeter(side);
int area = area(side);
System.out.println("perimeter of square: " + perimeter);
System.out.println("area of square: " + area);
scanner.close();
}
/**
* Java program to calculate perimeter of a square given its side
*
* @param side
* @return perimeter of square
*/
public static int perimeter(int side) {
return 4 * side;
}
/**
* Java program to calculate area of a square given its side
*
* @param side
* @return area of square
*/
public static int area(int side) {
return side * side;
}
}
Output
Hello
Please enter length or side of square :
4
perimeter of square: 16
area of square: 24
That's all about how to calculate area and perimeter of a square in Java. This is actually one of the simplest program you can write but its good to learn coding. You will learn how to convert mathematical formula to code as well as how to take user input in Java etc. It's also a fun exercise because you can see an interactive program which can calculate area and perimeter of a square for any given input.
Other Java Programs for Practice to Learn Coding
- How to check if a given number is prime or not? (solution)
- 10 Free Courses to learn Data Structure and Algorithms (free courses)
- How do you reverse the word of a sentence in Java? (solution)
- How to print factorial of a given number in Java? (factorial)
- How to find if the given String is a palindrome in Java? (solution)
- 100+ Data Structure and Algorithms problems for interviews (questions)
- How to reverse an int variable in Java? (solution)
- How to find a missing number in a sorted array? (solution)
- 10 Free Courses to learn Java Programming (free courses)
- Write a program to check if a number is a power of two or not? (solution)
- 10 Dynamic Programming problems for interviews (dynamic programming)
- How to reverse String in Java without using StringBuffer? (solution)
- 15 Recursion exercise for Java Programmers (recursion)
- How do you swap two integers without using a temporary variable? (solution)
Thanks for reading this Java programming tutorial so far. If you like this programming exercise then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
No comments :
Post a Comment