1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Sergeeva-Olga [200]
3 years ago
8

Write Java programs with loops that compute:a.The sum of all even numbers between 2 and 100 (inclusive).

Computers and Technology
1 answer:
Ronch [10]3 years ago
4 0

Answer:

a.The sum of all even numbers between 2 and 100 (inclusive).

public class SumofEvenNumbers

{ public static void main(String[] args) {

  int sum=0; // stores the sum of even numbers

/* loop starts from 2 and terminates when value of i becomes greater than 100 At each iteration the value of i is incremented by 2 and added to the sum variable to add the even numbers */

for(int i=2; i<=100; i+=2){

 sum = sum + i; }  

System.out.println("Sum of all even numbers between 2 and 100 (inclusive) is " + sum);} }

//displays the sum of even nos from 2 to 100

Output:

Sum of all even numbers between 2 and 100 (inclusive) is 2550

b. The sum of all squares between 1 and 100 (inclusive).  

Sum of square from 1 to 100 is 1+4+9+16+25+36+49+64+81+100

public class SumofSquares

{ public static void main(String[] args) {

int square;

int sum = 0;

for(int i = 1; i<=10; i++) {

   square= (i*i);

   sum += square; }

System.out.println("The sum of all squares between 1 and 100 (inclusive) is " + sum);}}

Output:

The sum of all squares between 1 and 100 (inclusive) is 385

c. All powers of 2 from 2^0 up to 2^20.      

The program if you want to display all the the powers of 2 from 2^0 to 2^20:

import java.lang.Math;

public class Powers

{ public static void main(String[] args) {

   double  power = 0; // stores the powers

       for(int i = 0; i <= 20; i++) // loop starts from i=0 and terminates at i>20

           {power = Math.pow(2, i); // computes power of 2 from 2^0 to 2^20

     System.out.println("2 ^ " + i + " = " + (int) power);}

//displays all powers of 2 by converting each power of 2 to integer

} }

Output:

2 ^ 0 = 1                                                                                                    

2 ^ 1 = 2                                                                                                                    

2 ^ 2 = 4                                                                                                                  

2 ^ 3 = 8                                                                                                                  

2 ^ 4 = 16                                                                                                                  

2 ^ 5 = 32                                                                                                                

2 ^ 6 = 64                                                                                                                

2 ^ 7 = 128                                                                                                                

2 ^ 8 = 256                                                                                                              

2 ^ 9 = 512                                                                                                              

2 ^ 10 = 1024                                                                                                            

2 ^ 11 = 2048                                                                                                            

2 ^ 12 = 4096                                                                                                          

2 ^ 13 = 8192                                                                                                            

2 ^ 14 = 16384                                                                                                          

2 ^ 15 = 32768                                                                                                        

2 ^ 16 = 65536                                                                                                        

2 ^ 17 = 131072                                                                                                        

2 ^ 18 = 262144                                                                                                      

2 ^ 19 = 524288                                                                                                      

2 ^ 20 = 1048576

The following program is to find the sum of all powers of 2 from 2^0 to 2^20

import java.lang.Math;

public class Powers

{ public static void main(String[] args) {

   double  power = 0;

   double sum=0;

       for(int i = 0; i <= 20; i++){

          power = Math.pow(2, i);// calculates all powers of 2 from 2^0 to 2^20

          sum = sum + power;} // calculates sum of all powers from 2^0 to 2^20

       System.out.println("sum of all powers of 2 from 2^0 to 2^20 is " + sum);}}

Output:

sum of all powers of 2 from 2^0 to 2^20 is 2097151.0

d. The sum of all odd numbers between a and b (inclusive), where a and b are inputs.

import java.util.Scanner; //to take input from user

public class SumOddNumbers

{public static void main(String[] args) { // main() function body

Scanner input = new Scanner(System.in); //create object of Scanner class

      System.out.print("Enter two numbers: ");

//prompts user to enter two numbers

      int a = input.nextInt();  // scans and reads first number input by user

      int b = input.nextInt();//scans and reads 2nd number entered by user

      int sum = 0; // stores the sum of odd numbers

/* loop starts from i = a which is the first input number and continues till the value of i gets greater than b which is the 2nd input number */

      for (int i = a; i <= b; i++)       {

          if (i % 2 == 1) //checks if the number in the range a to b is odd

            sum += i;       } // adds the odd numbers

      System.out.println("The sum of all odd numbers between " + a + " and "+ b + " (inclusive) is " + sum); } }

// displays sum of odd numbers in the given range

Output:

The sum of all odd numbers between 1 and 10 (inclusive) is 25      

e. The sum of all odd digits of an input.      

import java.util.Scanner;  // to take input from user

public class SumOfOddDigits

{ public static void main(String[] args) { // main() function body

Scanner input = new Scanner(System.in); // create object of class Scanner

//prompts user to enter a number

   System.out.println("Enter a number: ");

//scans and reads numbers from user

   int number = input.nextInt();

   int sum=0; //stores the sum of odd digits of input number

   while(number>0){

// loop executes till value of number variable is greater than 0

     int remainder = number % 10;

/*takes mode of input number to calculate the remainder of the number, this means each digit of the number is separated. For example if the number is 32677 then after taking mod we get 7 in first iteration */

     if(remainder % 2!=0){

/*checks if the value in remainder is odd which means the digit is not completely divisible by 2 and the mod of the digit with 2 is not equal to 0 */

       sum = sum + remainder;      } // adds the odd digits of input number

     number = number / 10;    }

// divides the number by 10 for example 32677/10 is 3267 in first iteration

   System.out.println("sum of all odd digits is " + sum); } }  

// displays sum of the odd digits of input number    

Output:        

Enter a number:                                                                                                    

32677                                                                                                                    

sum of all odd digits is 17  

You might be interested in
The process of converting information from a form/questionnaire is referred to as data preparation. This process follows a four-
Sergio [31]

Answer:

Data tabulation.

Explanation:

6 0
3 years ago
You type a complex formula in cell A20. You would like to make the same calculation in cells B20 through H20. To be efficient, y
fomenos

Answer:

Use autofill

Explanation:

In excel "autofill" is used to fill data in cells, which follows the pattern or logic applied in other cells. To apply same calculation of A20 in B20 to H20, we use autofill technique. Instead of copy pasting, or manually entering the complex formula this method is more suitable.  

4 0
4 years ago
Which nation has a command economy
Over [174]

Answer:command economy is a key feature of any communist society. Cuba, North Korea, and the former Soviet Union are examples of countries that have command economies, while China maintained a command economy for decades before transitioning to a mixed economy that features both communistic and capitalistic elements

5 0
3 years ago
Read 2 more answers
Which of the following factors will have the greatest impact on your credit score? I. Length of Credit History II. Payment Histo
Veseljchak [2.6K]
<span>the one that would have the greatest impact on my credit score would be : B. II and III If you had a troublesome payment history, it would be very likely that you'll end up with credit score because you make yourself became untrustworthy and if you have a low Debt, people will trust your current financial condition and you will be more likely to earn high credit score</span>
5 0
3 years ago
Read 2 more answers
What are some industries of aerodynamics and hydrodynamics? explain each one in detail.
maria [59]
Aerodynamic- Wind turbine, computational fluid dynamics, and Wind power

Hydrodynamics- Computational Fluid Dynamics, Hydraulics, and Microfluidics
3 0
2 years ago
Read 2 more answers
Other questions:
  • Your friend is working on fixing their Homework assignment. They need a lot of help. You know all the bugs in the file, but due
    12·1 answer
  • When did the digital revolution begin
    10·1 answer
  • Once the data center routes to the destination server that hosts the website, whats the next step in the internet process?
    10·1 answer
  • NIST recommends selecting cloud providers that support strong encryption, have appropriate redundancy mechanisms in place, emplo
    11·1 answer
  • (Financial application: compound value) Suppose you save $100 each month into a savings account with the annual interest rate 5%
    8·1 answer
  • why is there a need of properly interpreting teacher's/manufacturer's specifications before operating any food processing equipm
    9·1 answer
  • What is computer system<br>explain the role of bank in computer<br>​
    15·2 answers
  • Which statement correctly differentiates how to use list and table styles?
    12·2 answers
  • Which of these tools stick to the edge of an image, thus making it easy to select the shape of an image (adobe Photoshop)
    5·1 answer
  • Which type of service offers a preconfigured testing environment for application developers to create new software applications
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!