Problem:
Design an algorithm to simulate multiplication by addition.
Solution:
package com.myprograms;
import java.util.Scanner;
public class MultiplicationByAddition {
int multiplier;
int multiplicand;
public static void main(String[] args) {
MultiplicationByAddition multiplicationByAddition = new MultiplicationByAddition();
multiplicationByAddition.getTheNumber();
multiplicationByAddition.calculateMultiplication();
}
public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("enter multiplicand value: ");
multiplicand = s.nextInt();
System.out.println("enter multiplier value: ");
multiplier = s.nextInt();
s.close();
}
public void calculateMultiplication(){
int result = 0;
for(int i = 1; i<= multiplier; i++){
result = multiplicand + result;
}
System.out.println("the result is: " + result);
}
}
Output:
enter multiplicand value:
5
enter multiplier value:
20
the result is: 100
Design an algorithm to simulate multiplication by addition.
Solution:
package com.myprograms;
import java.util.Scanner;
public class MultiplicationByAddition {
int multiplier;
int multiplicand;
public static void main(String[] args) {
MultiplicationByAddition multiplicationByAddition = new MultiplicationByAddition();
multiplicationByAddition.getTheNumber();
multiplicationByAddition.calculateMultiplication();
}
public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("enter multiplicand value: ");
multiplicand = s.nextInt();
System.out.println("enter multiplier value: ");
multiplier = s.nextInt();
s.close();
}
public void calculateMultiplication(){
int result = 0;
for(int i = 1; i<= multiplier; i++){
result = multiplicand + result;
}
System.out.println("the result is: " + result);
}
}
Output:
enter multiplicand value:
5
enter multiplier value:
20
the result is: 100
Gud one
ReplyDelete