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 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 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
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..
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
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Use the following two loop strategies to write a function that sums all even numbers before...
Use the following two loop strategies to write a function that sums all even numbers before an odd number is observed in any given numeric vector, and test your code in R. For example, if the input vector is 2, 4, -2, 3, 4, 5, then the first odd number appears in the fourth position, which is 3. The output should be the sum of the first to the third numbers, which will be 2 + 4 − 2 =...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Sum of numbers between 1 and 10
Calculate the values from the  smallest number to the largest number
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT