Concept explainers
(Math: pentagonal numbers) A pentagonal number is defined as n(3n − 1)/2 for n = 1, 2, ... , and so on. Therefore, the first few numbers are 1, 5, 12, 22, .... Write a method with the following header that returns a pentagonal number:
public static int getPentagonalNumber(int n)
For example, getPentagonal Number (1) returns 1 and getPentagonalNumber(2) returns 5. Write a test
Program Plan:
- Declare the class “Penta”
- Definition for main class
- Use for loop to iterate the variable “j” equals to “100”.
- Check whether “j” mod “10” equals to 0.
- Call the method “getPentagonalNumber()”.
- Otherwise, call the method “getPentagonalNumber()” concatenate with space.
- Method definition for “getPentagonalNumber()”
- Return “n * (3 * n - 1) / 2”.
Program to display first 100 pentagonal numbers with 10 numbers on each line using the method “getPentagonalNumber ()”
Explanation of Solution
Program:
//Definition for class "Penta"
public class Penta
{
//Definition for main class
public static void main(String[] args)
{
//For loop to iterate the variable
for (int j = 1; j <= 100; j++)
//Check whether the variable "j" mod 10 equals to "0"
if (j % 10 == 0)
//Call the method "getPentagonalNumber"
System.out.println(getPentagonalNumber(j));
else
//Call the method "getPentagonalNumber"
System.out.print(getPentagonalNumber(j) + " ");
}
//Method definition for "getPentagonalNumber"
public static int getPentagonalNumber(int n)
{
//Return the result
return n * (3 * n - 1) / 2;
}
}
1 5 12 22 35 51 70 92 117 145
176 210 247 287 330 376 425 477 532 590
651 715 782 852 925 1001 1080 1162 1247 1335
1426 1520 1617 1717 1820 1926 2035 2147 2262 2380
2501 2625 2752 2882 3015 3151 3290 3432 3577 3725
3876 4030 4187 4347 4510 4676 4845 5017 5192 5370
5551 5735 5922 6112 6305 6501 6700 6902 7107 7315
7526 7740 7957 8177 8400 8626 8855 9087 9322 9560
9801 10045 10292 10542 10795 11051 11310 11572 11837 12105
12376 12650 12927 13207 13490 13776 14065 14357 14652 14950
Want to see more full solutions like this?
Chapter 6 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Modern Database Management
Starting Out with Python (4th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
- Here's the problem " (Math: approximate the square root) There are several techniques for implementingthe sqrt method in the Math class. One such technique is known as theBabylonian method. It approximates the square root of a number, n, by repeatedlyperforming a calculation using the following formula: nextGuess = (lastGuess + n / lastGuess) / 2 When nextGuess and lastGuess are almost identical, nextGuess is theapproximated square root. The initial guess can be any positive value (e.g., 1).This value will be the starting value for lastGuess. If the difference betweennextGuess and lastGuess is less than a very small number, such as 0.0001,you can claim that nextGuess is the approximated square root of n. If not, next-Guess becomes lastGuess and the approximation process continues. Implementthe following method that returns the square root of n.public static double sqrt(long n)" I have my java code here " import java.util.Scanner;public class Main2 {public static void main(String[]…arrow_forwardThis is a Java Programming questionarrow_forward1)Write a method that takes an array of ints and returns the sum of all numbers that are even. For example, given an array {3, 4, 6}, you would return 10. 2)For this exercise, you are given an array of integers. Return true if every element in the array is an odd number, otherwise return false. 3) Given an array of strings, return the array in reverse order. For example, given: reverse({"one", "two", "three"}); You would return: {"three", "two", "one"} 4) For this exercise, you need to shift all the elements in the array one space to the right (increase the index of the element by 1). The last element will wrap around and become the new first element. For example, given: moveUp({"first", "second", "third", "fourth"}); You would return: {"fourth", "first", "second", "third"}arrow_forward
- task4-Change Summation Integers problem to Finding the minimumproblem. Make sure you properly wrote/updated all text messages, method names,and math calculations.Hint: You can use java.lang.Math.min() method.Example: System.out.printf("The min of the integers %4d and %4d and %4dis %7d\n", a, b, c, MinTest(a, b, c));Submit the source code files (.java files) and the console output screenshotsarrow_forward2. A pentagonal number is defined as n(3n-1)/2 for n = 1, 2, ..., etc.. Therefore, the first few numbers are 1, 5, 12, 22, ... . Write a method with the following header that returns a pentagonal number: %3D public static int getPentagonalNumber(int n) For example, getPentagonalNumber(1) returns 1 and getPentagonalNumber(2) returns 5. Write a test program that uses this method to display the first 100 pentagonal numbers with 10 numbers on each line. Numbers are separated by exactly one space.arrow_forwardWhat is the solution of this problem using java language?arrow_forward
- change Summation Integers problem to Finding the minimumproblem. Make sure you properly wrote/updated all text messages, method names,and math calculations.Hint: You can use java.lang.Math.min() method.Example: System.out.printf("The min of the integers %4d and %4d and %4dis %7d\n", a, b, c,d MinTest(a, b, c,d));arrow_forwardchange Summation Integers problem to Finding the minimumproblem. Make sure you properly wrote/updated all text messages, method names,and math calculations.Hint: You can use java.lang.Math.min() method.Example: System.out.printf("The min of the integers %4d and %4d and %4dis %7d\n", a, b, c,d MinTest(a, b, c,d)); fix below code structurearrow_forward(Number of days in a year) Write a method that returns the number of days in a year using the following header: public static int numberOfDaysInAYear(int year)Write a test program that displays the number of days in year from 2000 to 2020.arrow_forward
- a.Write the method that finds the first 4 numbers out of 100 randomly generated numbers. After the numbers are generated in main, they should be sent to the method given as an example below and the smallest four numbers should be found. Write the numbers found on the console screen in the main method.(will be coded in java) Method name: public static int[] smallestFindFirstFourth(int[] array) Note: Sorting algorithms or ready-made functions will never be used. b.In the attached city list.txt file, the names of the provinces in Turkey are given in a single line according to the format given in Figure 1. In the application you will write, write the city names read from this file into the license plate list.txt file in the format shown in Figure 2. In more descriptive terms, citylist.txt is a file given to you. license plate list.txt is the file you will get with your code. city list: Adana, Adıyaman, Afyon, Ağrı, Amasya, Ankara, Antalya, Artvin, Aydın, Balıkesir, Bilecik, Bingöl, Bitlis,…arrow_forwarda.Write the method that finds the first 4 numbers out of 100 randomly generated numbers. After the numbers are generated in main, they should be sent to the method given as an example below and the smallest four numbers should be found. Write the numbers found on the console screen in the main method.(will be coded in java) Method name: public static int[] smallestFindFirstFourth(int[] array) Note: Sorting algorithms or ready-made functions will never be used. b.In the attached city list.txt file, the names of the provinces in Turkey are given in a single line according to the format given in Figure 1. In the application you will write, write the city names read from this file into the license plate list.txt file in the format shown in Figure 2. In more descriptive terms, citylist.txt is a file given to you. license plate list.txt is the file you will get with your code. city list: Adana, Adıyaman, Afyon, Ağrı, Amasya, Ankara, Antalya, Artvin, Aydın, Balıkesir, Bilecik, Bingöl, Bitlis,…arrow_forwardComplete the convert() method that casts the parameter from a double to an integer and returns the result.Note that the main() method prints out the returned value of the convert() method. Ex: If the double value is 19.9, then the output is: 19 Ex: If the double value is 3.1, then the output is: 3 code: public class LabProgram { public static int convert(double d){ /* Type your code here */ } public static void main(String[] args) { System.out.println(convert(19.9)); System.out.println(convert(3.1)); }}arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education