Question

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 sum. The input and output should match the sample run exactly.

SAMPLE RUN

Enter the upper limit: 500

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

Enter the upper limit: 1055

1+2+4+8+16+32+64+128+256+512+1024 = 2047

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Class name, function names and variable names are used in such a way that easily understandable. It can be changed according to the user. Comments are given in bold and italic.

Save the file name as same as the class name. Here class name is pow_of_two so the file has been saved as pow_of_two.java. It can be changed to the user's choice. For indentation and better understanding please refers to code snippets given.

Code : pow_of_two.java

import java.util.Scanner;

//for power function
import java.lang.Math;

public class pow_of_two
{

//function to calculate the powers of two
public static double cal_pow_two(double pow_num)
{
       //use of the math pow function to calculate power
       return Math.pow(2,pow_num);
      
   }

public static void main(String[] args)
   {
   //to store upper limit
       int up_limit;
      
       //counter variables
       double cntr=0;
      
       //to store return value of cal_pow_two() function
       double pow_result;
      
       //to store the final sum
       long fin_sum=0;
      
       Scanner val = new Scanner(System.in);
      
       System.out.print("Enter the upper limit: ");
      
       //getting the upper limit fromt the user
       up_limit=val.nextInt();
      
       do
       {
           //calling the cal_pow_two() function
           pow_result=cal_pow_two(cntr);
          
           //checking if power of 2 is less than the upper limit
           if(pow_result<=up_limit)
           {
           //This is condtion just to avoid printing of + sign in the end
               if(cntr==0)
               {
               System.out.print((int)pow_result);
fin_sum=fin_sum+(long)pow_result;
               }
               else
               {
               System.out.print("+"+(int)pow_result);
fin_sum=fin_sum+(long)pow_result;
               }
           }
           cntr++;
          
       }while(pow_result<=up_limit);
      
       //printing out the final sum
       System.out.print("="+fin_sum);
      
   }
}  

Code Snippets

Sample Output runs

Add a comment
Know the answer?
Add Answer to:
write a program in java to print all the powers of 2 below a certain number...
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 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...

  • 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...

  • 1.Given a positive integer number says n, write a java program to print the first n...

    1.Given a positive integer number says n, write a java program to print the first n squared numbers recursively. Sample run 1: Enter the value of n: 5 ------------------------------------- First 5 square numbers are: 1 4 9 16 25 Sample run 2: Enter the value of n: 10 ------------------------------------- First 10 square numbers are: 1 4 9 16 25 36 49 64 81 100 Sample run 3: Enter the value of n: 12 ------------------------------------- First 12 square numbers are: 1...

  • How to write a program to draw the output of a picture use c programming Assignment...

    How to write a program to draw the output of a picture use c programming Assignment Output 2 to power n 2 to power-n 2 4 8 16 3 2 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 1OOOOO0ooo000 O.50O0OO0000OOO 0.250000000OOO O.125000ooooOO O.062500OoooOO O.031250OoooOO 0.015625000O00 O.00781250000O O.0039062500OO O.0019531250OO O.000976562500 O.000488281250 O.000244140625 O.0o0122070313 O.000061035156 O.0o0030517578 O.ooO015258789 O.ooO007629395 O.ooO003814697 O.ooO001907349 O.0oOO00953674 2 3 6 8 10 12 1 3 15 16 17 18 19 20

  • Write a Java program that prompts the user for the page size used in a virtual...

    Write a Java program that prompts the user for the page size used in a virtual memory system; this will be a power of two between 512 (29) and 16384 (214), inclusive. Your program should check the user input for page size to make sure it is one of the allowable inputs (must be a power of 2 and cannot be smaller than 512 or larger than 16384), and should then prompt the user for a virtual address (assume 32-bit...

  • Write a C++ program to calculate the value of y = x2 for all values of...

    Write a C++ program to calculate the value of y = x2 for all values of x where x = 1, 2, 3, 4, 5. Use nested FOR statement to calculate and display the results as shown on the table below. Compile and run the program then submit the CPP file. x y^2 a^3 a^4 a^5 1 1 1 1 1 2 4 8 16 32 3 9 27 81 243 4 16 64 256 1024 5 25 125 625...

  • 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...

  • Week 5 Project Write a program that calculates the amount of money a person would earn...

    Week 5 Project Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, four pennies the third day, and continues to double each day. Output the amount earned each day .. and the total pay at the end. The Algorithm (Plan) for your program would be: Ask the user for the number of days to be...

  • Java Submit a program in a .java file. The program should print a multiplication table for...

    Java Submit a program in a .java file. The program should print a multiplication table for the numbers 1-9. This requires a nested for loop. The outer loop moves from row to row, while the inner loop prints the row. Use tabs to separate the output, such as in the following statement:      System.out.println(“1\t2\t3\t4\t5\t6\t7\t8\t9”); Output should look similar to the following. 1      2     3     4     5     6     7     8     9 ------------------------------------------------------------------ 1       2     3     4     5     6        7     8     9...

  • Write a complete Java program, including comments in both the main program and in each method,...

    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...

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