Har13526574
11.03.2020 •
Computers and Technology
Write Java programs with loops that compute:a.The sum of all even numbers between 2 and 100 (inclusive).
b.The sum of all squares between 1 and 100 (inclusive).
c.All powers of 2 from 20 up to 220.
d.The sum of all odd numbers between a and b (inclusive), where a and b are inputs.
e.The sum of all odd digits of an input. (For example, if the input is 32677, the sum would be .)
Solved
Show answers
More tips
- F Food and Cooking Discover Delicious Recipes You Can Make with Ground Meat...
- C Computers and Internet Google Search Tips and Tricks: Everything You Need to Know...
- S Science and Technology Why is there no gravity on other planets?...
- L Leisure and Entertainment How to Properly Wind Fishing Line onto a Reel?...
- L Leisure and Entertainment How to Make a Paper Boat in Simple Steps...
- T Travel and tourism Maldives Adventures: What is the Best Season to Visit the Luxurious Beaches?...
- H Health and Medicine Kinesiology: What is it and How Does it Work?...
- O Other How to Choose the Best Answer to Your Question on The Grand Question ?...
- L Leisure and Entertainment History of International Women s Day: When Did the Celebration of March 8th Begin?...
- S Style and Beauty Intimate Haircut: The Reasons, Popularity, and Risks...
Answers on questions: Computers and Technology
- C Computers and Technology Type the correct answer in the box. Spell all words correctly. Which network protocol allows administrators to access a remote computer in a secure way? BLANK is...
- C Computers and Technology Which type of light has the most neutral tone in photography...
- S Social Studies One might expect university president compensation to increase when contributions rise, as more effective fundraisers are rewarded for their success. Nevertheless...
- B Business Polarix is a retailer of ATVs (all-terrain vehicles) and accessories. An income statement for its Consumer ATV Department for the current year follows. ATVs sell...
- C Chemistry A compound contains only change and n combustion of 35.0mg of the compound produces 33.5mg co2 and 41.1mg h2o. what is the empirical formula of the compound....
- E Engineering Should the ship breaking business continue why or why not?...
- E English Nice mud mask, miss america hyperbole paradox understatement verbal irony...
- S Social Studies Bill views himself as a Democrat. He is watching a television broadcast of a debate between two congressmen, a Republican and a Democrat. His roommate starts to...
- P Physics A clock radio has 0.1 amps of current in it, and it uses a standard voltage of 120 V. What is the power rating for this radio?...
- M Mathematics Evaluate the expression of 8x - 20 if x=10...
Ответ:
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
Ответ:
pls where is the image I can't see any image