In java please.
SumOfPowerOfTwo
The following program can be done in a single class.
Write a program that requests and reads in a integer number between 1 and 40 inclusively. Repeat the loop until a proper number is entered.
Call the method powersOfTwoSum with the number as the actual parameter. Expect a value of type long as a return value.
powersOfTwoSum(int number)
This method must use a recursive technique to return the sum of the powers of two from the number down to zero. For example, if the input is 5 the return value would be:
25 + 24 + 23 + 22 + 21 +1
Two Samples:
Enter an integer between 1 and 20 inclusively: 2
The sum of the powers of two from 0 to 2 is 7
Enter an integer between 1 and 40 inclusively: 40
The sum of the powers of two from 0 to 40 is 2199023255551
The answer to this question is as follows:
The code I provided should work is as follows:
for 2 the output is 7 because 20+21+22=1+2+4=7
for 5 the output is 63 because of 20+21+22+23+24+25=1+2+4+8+16+32=63
import java.util.*;
public class MyClass {
long powersOfTwoSum(int num)
{
int sum=(int)(Math.pow(2,num));
if(num==0)
{
return 1;
}
else
return powersOfTwoSum((num-1))+sum;
}
public static void main(String args[]) {
int num;
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
while(true)
{
if(num>=1 && num<=40)
{
break;
}
else
{
System.out.println("Enter an integer between 1 and 20
inclusively:");
num=sc.nextInt();
}
}
MyClass m=new MyClass();
System.out.println(m.powersOfTwoSum(num));
}
}
In java please. SumOfPowerOfTwo The following program can be done in a single class. Write a...
PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...
Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...
Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once. This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...
Problem 1: Write a program that reads a positive float number and displays the previous and next integers. Sample Run: Enter a float: 3.5 The previous and next integers are 3 and 4. Problem 2: Write a program that reads two integers and displays their sum, difference, product, and the result of their division. Sample Run: Enter two integers: 8 5 Sum: 8, Difference: 3, Product: 40, Division: 1.6 Problem 3: Write a program that reads a three-digit integer from...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
In Java* Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...
A java program Write a program that prompts for and reads in a positive integer into a variable n. Your program should then sum the first n ODD integers and display the sum. For example, if 3 is entered for n, your program should display the the sum 1 + 3 + 5. If 5 is entered, your program should display the sum 1 + 3 + 5 + 7 + 9. What is the...
This is for a java program public class Calculation { public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object //Task 1. Write your code here //Print the prompt and ask the user to enter an...