Question

In: Computer Science

Write assembly instructions that compute the following: The sum of all even numbers between 2 and...

Write assembly instructions that compute the following:

  1. The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550
  2. Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b.
  3. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350

All of the statements above should print the answers using print_int

MUST BE IN ASSEMBLY LANGUAGE PLEASE

Solutions

Expert Solution

1. The sum of all even numbers between 2 and 100 (inclusive)?
public static void main(String[] args) {
int number = 1;
int sum = 0;

while (number >= 2 && number <= 100){

if (number % 2 == 0)

sum = number + sum;
number ++;
}
System.out.println("The sum of all positive numbers is: " + sum);
}
}

2.
sum of two numbers A and B

:public class ComputeSumAAndB
{

   public static void main (String[] args)
   {
       Scanner in = new Scanner(System.in);
       System.out.print("Please enter 2 integers: "); //prompts user for ints
       int a = in.nextInt();
       int b = in.nextInt();
       int sum = 0;

       for (int j = a; j <= b; j++)
       {
           if (j % 2 == 1)
             sum += j;
       }
       System.out.println("The sum of all odd numbers (inclusive) between " + a + " and "+ b + " is " + sum);
   }
}

----------------------------
The sum of all the squares between 1 and 100 (inclusive)

int n = 1;
int squareValue;
int sum = 0;
while (n <= 10) {

    squareValue= (n*n);

    sum += squareValue;

    n++;
}
System.out.println(sum);

1. The sum of all even numbers between 2 and 100 (inclusive)?
public static void main(String[] args) {
int number = 1;
int sum = 0;

while (number >= 2 && number <= 100){

if (number % 2 == 0)

sum = number + sum;
number ++;
}
System.out.println("The sum of all positive numbers is: " + sum);
}
}

2.
sum of two numbers A and B

:public class ComputeSumAAndB
{

   public static void main (String[] args)
   {
       Scanner in = new Scanner(System.in);
       System.out.print("Please enter 2 integers: "); //prompts user for ints
       int a = in.nextInt();
       int b = in.nextInt();
       int sum = 0;

       for (int j = a; j <= b; j++)
       {
           if (j % 2 == 1)
             sum += j;
       }
       System.out.println("The sum of all odd numbers (inclusive) between " + a + " and "+ b + " is " + sum);
   }
}

----------------------------
The sum of all the squares between 1 and 100 (inclusive)

int n = 1;
int squareValue;
int sum = 0;
while (n <= 10) {

    squareValue= (n*n);

    sum += squareValue;

    n++;
}
System.out.println(sum);


Related Solutions

Write a loop for each of the following: The sum of all even numbers between 2...
Write a loop for each of the following: The sum of all even numbers between 2 and 100 (inclusive). The sum of all squares between 1 and 100 (inclusive). (If you use the pow function the first parameter must be a float or double). The sum of all odd numbers between a and b (inclusive). Where a and b are read in by the user. How often do the following loops execute? Assume that i is not changed in the...
Q1) Write programs with iterators that computes: • The sum of all even numbers between 2...
Q1) Write programs with iterators that computes: • The sum of all even numbers between 2 and 100 (inclusive). • The sum of all squares between 1 and 100 (inclusive). • The product of all powers of 2 from 20 up to 220 . • The product of all odd numbers between a and b (inclusive), where a and b are inputs. sum would be 3 + 7 + 7 = 17.) Q2) Recall the Fibonacci sequence: 1, 1, 2,...
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a MIPS assembly program that calculates the sum of all the elements in the following...
Write a MIPS assembly program that calculates the sum of all the elements in the following array: int array[10]={12,21,3,40,15,6,17,8,29,10}
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
Use a For loop to compute the sum of all the odd numbers from 1 through...
Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic for zyBook Challenge activity 4.6.1 (Nested loops: Indent text) so that the first line is indented userNum...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in...
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in 20 numbers. A method with an int parameter should display whether the number is odd or even for the number passed
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT