Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a script that ask the user to input an integer n. The script should print

ID: 3873060 • Letter: W

Question

Write a script that ask the user to input an integer n. The script should print the factorials. If up to as shown in the example below. Example: Enter a number 1 4 1 = 1 1 * 2 = 2 1 * 2 * 3 = 6 1 * 2 * 3 * 4 = 24 Make sure you test your script with multiple input values. Write a function called shiftedWord which takes a string S and a number "n" as a input and returns the original string shifted n places to the right as shown in the examples below. ShiftedWord ('today', 3) = 'dayto' shiftedWord ('123456789;, 4) = '678912345' shiftedWord ('1234', 4) = '1234' Then write a driver that asks the user to type a word and number, calls shiftedWord, and prints a message with the result. The driver should ask for another input string and number, and stop when the user enters a negative number.

Explanation / Answer

______________

Factorial.java

import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {

// Declaring variables
int num, tot = 1;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter a number :");
num = sc.nextInt();

// Displaying the factorial of a number
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j != i) {
System.out.print("*");
}
tot *= j;

}
System.out.print(" = " + tot + " ");
tot = 1;
}

}

}

___________________

Output:

Enter a number :4
1 = 1
1*2 = 2
1*2*3 = 6
1*2*3*4 = 24

________________

2)

ShiftWordDemo.java

import java.util.Scanner;

public class ShiftWordDemo {

public static void main(String[] args) {

//Declaring variables
String word;
int n;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);


/* this loop continues to execute until the user enters
* a negative number as input for the variable 'n'
*/
while (true) {

//Getting a number entered by the user
System.out.print("Enter a number :");
n = sc.nextInt();
if (n < 0) {
break;
}

//Getting the word entered by the user
System.out.print("Enter a String :");
word = sc.next();


//calling the method by passing the user entered inputs as arguments
ShiftedWord(word, n);


}

}

//This method will shift the characters in the word based on the number
private static void ShiftedWord(String word, int n) {

String newWord = word;
for (int i = 0; i < n; i++) {
newWord = newWord.charAt(newWord.length() - 1) + newWord.substring(0, newWord.length() - 1);
}

System.out.println("The Shifted Word is :" + newWord);
}

}

_____________________

Output:

Enter a number :3
Enter a String :today
The Shifted Word is :dayto
Enter a number :4
Enter a String :123456789
The Shifted Word is :678912345
Enter a number :4
Enter a String :1234
The Shifted Word is :1234
Enter a number :-1

__________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote