Answer the following: A-In the main function provided, write a program that asks
ID: 3586113 • Letter: A
Question
Answer the following:
A-In the main function provided, write a program that asks the user to input two integers and
prints out the rst integer raised to the second (e.g. if the user enters 2 and 4, the program prints 16,
which is 2^4). You do not have to write any imports or public class : : : . Please just fill in main.
public static void main(String args[]) {
b-Write the few lines of code that print all of the integers between 5 and 1000 that are evenly
divisible by 6.
c-Write a java program which asks the user to enter three integers. The program prints "between"
if the 2nd number falls in between the 1st and 3rd, and "not between" if it doesn't. For example, if I
enter 1 10 27, the program prints "between", and if I enter 5 9 6, the program prints "not between".
d-Write a static method named twoConsecutive that accepts three integers as parameters and
returns true if there is at least one pair of integers that di er by exactly 1. For example, the integers 3
and 4 di er by 1. The integers 12 and 11 also di er by 1.
Your method should return false if there are no such consecutive values. The integers could be passed
in any order; the two consecutive values could be any of the two values passed in.
Here are some sample calls:
Call Output
twoConsecutive(1, 2, 12) true
twoConsecutive(1, 12, 2) true
twoConsecutive(2, 12, 1) true
twoConsecutive(4, 5, 3) true
twoConsecutive(2, 4, 6) false
twoConsecutive(8, 8, 8) false
Explanation / Answer
Please find below the required answer.
A) required mani method:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number : ");
int no1= sc.nextInt();
System.out.print("Enter the second number : ");
int no2= sc.nextInt();
int result = 1;
for (int i=1;i<=no2;i++)
{
result*=no1;
}
System.out.println(no1+"^"+no2+" = "+result);
}
b)
public class Test2 {
public static void main(String[] args) {
//if no divisded by zero and no rmindere
for(int i=5;i<1000;i++)
{
if(i%6==0)
System.out.println(i);
}
}
}
c)
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first no. : ");
int no1 = sc.nextInt();
System.out.print("Enter second no. : ");
int no2 = sc.nextInt();
System.out.print("Enter third no. : ");
int no3 = sc.nextInt();
//if second no greater than first and less than third
if(no2>no1&&no2<no3)
System.out.println("between");
else
System.out.println("not between");
}
}
d)
public class Test4 {
public static boolean twoConsecutive(int n1, int n2, int n3)
{
//if subraction of two integers will one then true return
if((int)Math.abs(n1-n2)==1||(int)Math.abs(n1-n3)==1||(int)Math.abs(n2-n3)==1)
return true;
return false;
}
public static void main(String[] args) {
System.out.println(twoConsecutive(1, 2, 12));
System.out.println(twoConsecutive(1, 12, 2));
System.out.println(twoConsecutive(2, 12, 1));
System.out.println(twoConsecutive(4, 5, 3));
System.out.println(twoConsecutive(2, 4, 6));
System.out.println(twoConsecutive(8, 8, 8));
}
}
//Please do let me know if u have any concern
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.