In: Computer Science
1. For all of the following words, if you move the first letter to the end of the word and then spell the word backwards, you get the original word:
banana dresser grammar potato revive uneven assess
Write a program that gets a word from the keyboard and determines whether it has this property. treat uppercase and lowercase letters alike. That means that poTato also has this property.
2. Write a program that generates a random number (between 1 and 10 inclusive) 100 times. Output the total number of 1s, 2s, ….10s.
Code language is Java. using JGrasp to code
1.
Code:
import java.util.*; // library file that contains string functions
import java.io.*; // library file that contains input/output stream reader/writer
// defining class
class main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.nextLine(); // taking input
String word1 = word.substring(0); // extracting a substring of the word leaving out first letter
word1 = word1 + word.charAt(word.length()-1); // adding the extracted substring with 1st letter
if(word1.equalsIgnoreCase(word)){ // checking condition
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
Output:
2.
Code:
import java.util.*;
import java.io.*;
class main {
public static void main (String[] args) {
// define the range
int max = 10;
int min = 1;
int range = max - min + 1;
int[] arr = new int[11];
// generate random numbers within 1 to 10
for (int i = 0; i < 100; i++) {
int rand = (int)(Math.random() * range) + min;
arr[rand]++;
}
for(int i=1; i<11; i++){
System.out.println("Number of "+i+"'s: "+arr[i]);
}
}
}
Output: