Question

Write a java program to print all the powers of 2 below a certain number and...

Write a java program to print all the powers of 2 below a certain number and calculate their sum

Requirements:

1. Accept the upper limit from the user as an integer

2. Make sure the sum variable is of the “long” type. You can assume that the test output will be less than LONG MAX.

3. Print all the numbers as a running sum, and finally print their sum.

Please try and restrict yourself to loops, selection statements, and calls to library methods

Example Run:

Enter the upper limit: 500

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class SumPowers2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the upper limit: ");
        int max = in.nextInt();
        int sum = 1, n = 1;
        System.out.print("1");
        while (n*2 <= max) {
            n *= 2;
            sum += n;
            System.out.print(" + " + n);
        }
        System.out.println(" = " + sum);
    }
}

Enter the upper limit: 500 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511 Process finished with exit code 0,

Add a comment
Know the answer?
Add Answer to:
Write a java program to print all the powers of 2 below a certain number and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a program in java to print all the powers of 2 below a certain number...

    write a program in java to print all the powers of 2 below a certain number and calculate their sum. Make sure your program conforms to the following requirements. 1. Accept the upper limit from the user (as an integer). 2. These numbers grow very fast. make sure the sum variable is of the "long" type. You can assume that the test output will be less that LONG MAX. 3.Print all numbers as a running sum, and finally print their...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • Write a C program to sum up all the odd numbers between a lower limit number...

    Write a C program to sum up all the odd numbers between a lower limit number and an upper limit number provided by a user. The requirements are: First, request the user to provide a lower limit integer number and an upper limit integer number that is larger than the lower limit number, and save them in variables lowerLimit and upperLimit, respectively. If the user has entered an upper limit number (upper Limit) that is NOT larger than the lower...

  • I must create a Loop in C++ that will print all prime numbers less than the...

    I must create a Loop in C++ that will print all prime numbers less than the number entertered by the user. 1. Accept the upper limit from the user (as an integer). (5 points) 2. Make sure the number is positive. If it is not, terminate the program. (5 points). 3. Go from 1 to the number. If you happen to find a number that is prime, print it. (35 points) example: Enter the upper limit: 25 The prime numbers...

  • JAVA turn this psuedo code into java code. USING NO LOOPS! ALL LOOPS MUST BE TURNED...

    JAVA turn this psuedo code into java code. USING NO LOOPS! ALL LOOPS MUST BE TURNED INTO RECURSIVE CALLS. English: 1. Prompt for and read a number between 1 and 5. Repeat this step until the input is 1..5. 2. Repeat the following multiple times according to the number read in step 1. a. Read in a list of integers ending with a 0. The 0 marks the end of the input and is not considered part of the list...

  • Question: Write a java class to accept data from user via keyboard for (1) User name...

    Question: Write a java class to accept data from user via keyboard for (1) User name (2) three integer values,then calculate sum& average. O/P in DOS with one(1) print method for all I/O data. Line l Line 2 Line 4: Line 6 Line 7 Line 8

  • program in Java or C We read a number i and print all the bits in...

    program in Java or C We read a number i and print all the bits in the binary representation of i+1 strictly to the right of the first 1. One option to of doing that is with a recursive function that checks if its argument is strictly greater than 1 and, if so, calls itself on j right-shifted 1 bit and then prints j mod 2.

  • What is the java code for this. The goal of this lab is to write two...

    What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works....

  • Java: Write a static method named longestName that reads a series of names typed by the...

    Java: Write a static method named longestName that reads a series of names typed by the user and prints the longest name (i.e., the name with the most characters). The method should accept a Scanner object, console and an integer, n as parameters. After prompting and reading in n names, the longest name should be printed as follows: its first letter should be capitalized and all subsequent letters lowercase, regardless of how the user entered the name. If there is...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT