Concept explainers
Write a fragment of code that will read words from the keyboard until the word done is entered. For each word except done, report whether its first character is equal to its last character. For the required loop, use a
- a. while statement
- b. do-while statement
Explanation of Solution
a.
Using “while” statement:
The code fragment to read words until the user enters “done”. For every word, the program should check whether the first and last characters are match or not. The program using “while” condition is as follows. Code fragment is highlighted.
//Import required package
import java.util.Scanner;
//Define the Main class
public class Main
{
//Define main method
public static void main(String[] args)
{
//Create an object for scanner class
Scanner sc = new Scanner(System.in);
//Declare a variable
String word = " ";
//Do till the user enters "done"
while(!word.equals("done"))
{
//Get a word from the user
System.out.print("\nEnter a word: ");
word = sc.next();
//Check if word is equal to "done"
if(word.equals("done"))
//Break the loop
break;
//Check if 1st and last characters are equal
if(word.charAt(0) == word.charAt(word.length()-1))
//Print the message
System.out.println("The first and last character matches in '"+ word + "'");
//Else
else
//Print the message
System.out.println("The first and last character does not matches in '"+ word + "'");
}
}
}
Explanation:
- The statements under the condition “while(!word.equals("done"))” gets executed till the user enters “done”.
- The program then gets a word from the user and then it checks whether the 1st and last characters are match or not.
- The program gets halted if the user enters “done”.
Output:
Enter a word: abi
The first and last character does not matches in 'abi'
Enter a word: aruna
The first and last character matches in 'aruna'
Enter a word: done
Explanation of Solution
b.
Using “do-while” statement:
The code fragment to read words until the user enters “done”. For every word, the program should check whether the first and last character are match or not. The program using “do-while” condition is as follows. Code fragment is highlighted.
//Import required package
import java.util.Scanner;
//Define the Main class
public class Main
{
//Define main method
public static void main(String[] args)
{
//Create an object for scanner class
Scanner sc = new Scanner(System.in);
//Declare a variable
String word = " ";
//Do till the user enters "done"
do
{
//Get a word from the user
System.out.print("\nEnter a word: ");
word = sc.next();
//Check if word is equal to "done"
if(word.equals("done"))
//Break the loop
break;
//Check if 1st and last characters are equal
if(word.charAt(0) == word.charAt(word.length()-1))
//Print the message
System.out.println("The first and last character matches in '"+ word + "'");
//Else
else
//Print the message
System.out.println("The first and last character does not matches in '"+ word + "'");
} while(!word.equals("done"));
}
}
Explanation:
- The statements inside “do-while” condition gets executed till the user enters “done”.
- The program then gets a word from the user and then it checks whether the 1st and last characters are match or not.
- The program gets halted if the user enters “done”.
Output:
Enter a word: charles
The first and last character does not matches in 'charles'
Enter a word: david
The first and last character matches in 'david'
Enter a word: done
Want to see more full solutions like this?
Chapter 4 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Modern Database Management (12th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Modern Database Management
Starting Out with Java: From Control Structures through Objects (6th Edition)
- The body of a while loop can consist of ____________. a. a single statement b. a block of statements within curly braces c. either a or b d. neither a nor barrow_forwardWrite a for loop that prints out the numbers 15 down to 5, one number per line. Declare all variables needed, only write the code required. Do not write a complete program. Comments and system libraries should not be included.arrow_forwardRewrite the code below first using a while loop, and then a do loop instead of a for loop. for (int i = 0; i <= 10; i++) System.out.println(i);arrow_forward
- Write a program with a loop to computer the sum of all the even numbers from 2 to 100 inclusive.arrow_forwardOne sub for "For Loop" and second one for "For Each Loop" and third one for Do-While loops. Write a VBA program that reads a Number from a cell and fills the cell next to it with the factorial of that number n! = (n)*(n-1)*(n-2)*….*3*2*1 Make sure your program also works for the value of 0!, since this is equal to 1. Take the code you just made for the previous for loop exercise, and put it in a For Each loop that evaluates the factorial of several cells. Simulate the tossing of a coin 1000 times, and see how many heads/tails you get. To do this, you will need the random number function. (HINT:The line x = Rnd will generate a number between 0 and 1. If x <=0.5 then the result is Head Else Tails)arrow_forwardQ1: Rewrite the following using a for-loop instead of a while-loop: while (i<20){ cout<<i<<endl; i +=2;arrow_forward
- Write a python program using a while or for loop that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.arrow_forwardSum of Positive Numbers using while loop or do while?arrow_forwardRe-write the following while loop using as a for loop int i = 5; while (i < 50) { System.out.print(i + " "). i += 5; }arrow_forward
- Write while loops to do the following: Repeatedly print the value of the variable xValue, decreasing it by 0.5 each time, as long as xValue remains positive. Print the square roots of the first 25 odd positive integers. Repeats a block of code as long as the user indicates they want it to. Drive the user crazy by insisting they re-enter a particular input no matter what they enter.arrow_forwardConsider the following for loop. int j, s;s = 0;for (j = 1; j <= 10; j++)s = s + j * (j - 1);In this for loop, identify the loop control variable, the initialization statement, loop condition, the update statement, and the statement that updates the value of s.arrow_forwardUsing the while loop, create a program that adds the numbers from 1 – 10.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning