write a java program that reads two positive integer number (x and y) and calculates x using only addition and multiplication operations.
(For Anindyaadhikari13 , CopyThat , BrainlyProgrammer, poonamtikka87100, Oreki only)
Answers
Correct question :
Write a java program that reads two positive integer number (x and y) and calculates xʸ using only addition and multiplication operations.
Program : {JAVA}
import java.util.Scanner;
public class Power
{
public static void main(String args [ ] )
{
int power = 1;
int counter = 1;
Scanner keyboard = newScanner(System.in);
System.out.println(''Enter the first number: '');
int x = keyboard.nextInt();
System.out.println(''Enter the second number: '');
int y = keyboard.mextInt();
while (counter <=y)
{
power = power * x;
counter++;
}
System.out.println (x + '' ^ '' + y + '' = '' +power);
keyboard.close();
}
}
Output :
Enter the first number: 2
Enter the second number: 7
2^7 = 128
Answer:
Explanation:
import java.util.Scanner;
public class Power
{
public static void main(String args [ ] )
{
int power = 1;
int counter = 1;
Scanner keyboard = newScanner(System.in);
System.out.println(''Enter the first number: '');
int x = keyboard.nextInt();
System.out.println(''Enter the second number: '');
int y = keyboard.mextInt();
while (counter <=y)
{
power = power * x;
counter++;
}
System.out.println (x + '' ^ '' + y + '' = '' +power);
keyboard.close();
}
}
Output :
Enter the first number: 2
Enter the second number: 7
2^7 = 128