Question

In: Computer Science

Java program to print all the powers of 2 below a certain number. Calculate sum, accept...

Java program to print all the powers of 2 below a certain number. Calculate sum, accept upper limit and make sure sum variable is long type.  

Run-

Enter the upper limit: 100

5 + 8 + 9 + 11 + 20 + 32 + 30 = 115

Solutions

Expert Solution

import java.util.Scanner;

public class SumSqure {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter upper limit: ");
       int upper=sc.nextInt();
       long temp,sum=0;
       //iterating untill the upper limit
       for(int i=0;;i++){
           //generate powers of 2
           temp=(long)Math.pow(2, i);
           //checking if generated power is in range
           if(temp<=upper){
               System.out.print(temp+" ");
               sum+=temp;
           }
           else{
               break;
           }
       }
       System.out.println(" = "+sum);
   }
}

The given sample output looks wrong


Related Solutions

JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Modify Java program to accept a telephone number with any numberof the letters, using the...
Modify Java program to accept a telephone number with any number of the letters, using the if else loop. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
: Create a Java program that will accept a regular expression and a filename for a...
: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format : The following operators need to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation, no character spans – the...
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
FOR JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by...
FOR JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT