Question

Write a program to compute the product of two positive integers by repeated addition. Note: You...

  1. Write a program to compute the product of two positive integers by repeated addition.

Note: You are not allowed to use either multiplication or division operator.

Understanding the Meaning of Multiplication:

Multiplication of whole numbers can be thought of as repeated addition.

// not allowed to use multiplication or division operator

For example, suppose that a parking lot has 6 rows of parking spaces with 8 spaces in each row. How many parking spaces are in the lot?

rows

To get total we add 8 six times,

8+8+8+8+8+8=48 or we can use a shortcut: 6 rows of 8 , which equal to 8.

This is multiplication, a shortcut for repeated addition.

When numbers are large, multiplication is easier than addition.

// Multiplication could be done by   reapeated Addition

package forinfinity;

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

                        int a, b, i, product ;

                        

// Create a Scanner object for keyboard input.

                         Scanner keyboard = new Scanner(System.in);

                        

System.out.print("Enter the values of a and b: ");

                        a= keyboard.nextInt();//

                        b= keyboard.nextInt();//

                        i=0;

                        product=0;

                        while (i

                         {

                                     i=i +1;

                                    product=product + b;

                         }

                        System.out.print("The product is:" + product);       

            }

}

The Question I need help in is using the previous concept:

Write a JAVA application that determines volume of a cylinder by repeated addition.

You are not allowed to use either multiplication or division operator.

Use the formula

V = π r^2 L

Where V is the volume of the cylinder

r is the radius of the cylinder (user input)

l is the length of the cylinder (user input)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Main
{
   public static void main(String[] args) {
   Scanner keyboard=new Scanner(System.in);
       System.out.println("Enter radius");
       int r,l;
       r=keyboard.nextInt();
       System.out.println("Enter length");//read input from user
       l=keyboard.nextInt();
       int i=0,temp=0;
       while(i<r){
       temp=temp+r;//to calculate r^2 value
       i++;
       }
       i=0;
       int temp1=0;
       while(i<l){
       i++;
       temp1=temp+temp1;//calculate h*(r^2)
       }
       //pi value can be treated as 22/7.
       //So we multiply by 22 and divide the result by 7 to get the volume.
       i=0;
       temp=0;
       while(i<22){
       i++;
       temp=temp+temp1;//calculating 22*h*(r^2)
       }
       double quotient = 0,dividend=temp,divisor=7;
while (dividend >= divisor)
{
dividend -= divisor; //calculating 22*h*(r^2)/7.
++quotient;
}
System.out.println("The volume is:");
System.out.println(quotient);
   }
}

Screenshots:

Enter radius 9 Enter length 3 The volume is: 763.0 ..Program finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Write a program to compute the product of two positive integers by repeated addition. Note: You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to take  two numbers as input from the user , compute the addition,...

    Write a C++ program to take  two numbers as input from the user , compute the addition, subtraction, multiplication and division of these numbers and display the desired formatted results.

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • How would I code this method in Java using a scanner? Begin by asking how many...

    How would I code this method in Java using a scanner? Begin by asking how many spaces the row should be with a prompt using System.out.print like the following, with 40 as the maximally allowed number of spaces: (user input shown bold) Please enter the number of spaces for the game (1-40): eight To do this, use the promptNumberReadLine method that you will write, described on the back page. If the user does not type a number in the correct...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program...

    Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program that prompts the user to enter a year, month, day of month and it displays the name of the week. Here is my source code. I need it broke down into a flowchart. public static void main(String[] args) {        //all variables are initialized to 0, because local variables require at least an initial value before use        Scanner input = new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT