Write a program using while loop and for loops to print first 50 prime numbers in 5 lines, each line containing 10 numbers.
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
Write a program using while loop and for loops to print first 50 prime numbers in 5 lines, each line containing 10 numbers.
1. Create a program called CountCharacters.java.
2. At the top of your java file add the following documentation comments. Replace the italicized text with the appropriate content. /**
@author Your Name
CS 110 Section 012
Lab 9 Final
Today's Date
3. Import the Scanner class and ask user to enter a line. Implement the while loop so that the user can input any number of lines and one line at a time. To stop entering lines, the user can write "quit. (Hint: Use the string method <str>.equals() in the while condition to stop the while loop OR a break keyword).
4. Implement the for loop inside the while loop to iterate through the line one character at a time to count and output the number of blank spaces, alphabetic characters, digit characters, and other characters in all the lines. You need to use different methods of character class like charAt(), isDigit(), isLetter() etc.
Your code's output should look similar to the image shown below.
Enter a line: Aaron Paul,
Type quit to show the results or enter one more line 1243 St. Cloud Ave.
Type quit to show the results or enter one more line Apt. #9-3A
Type quit to show the results or enter one more line Browning City, 95849-889
Type quit to show the results or enter one more line quit
Number of lines: 4
Total white spaces: 9
Total digits: 14
Total alphabets: 35
Other Characters: 8
Trending now
This is a popular solution!
Step by step
Solved in 4 steps