(This is from Review Exercise R4.1 at the end of Chapter 4 of Java for Everyone.) Assume variables SimpleWriter out and int n are already declared in each case. Write a separate while loop for each of the following tasks:Print all squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.Print all positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90.Print all powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.
Question
(This is from Review Exercise R4.1 at the end of Chapter 4 of Java for Everyone.) Assume variables SimpleWriter out and int n are already declared in each case. Write a separate while loop for each of the following tasks:Print all squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.Print all positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90.Print all powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.
Solution
Sure, here are the step-by-step instructions for each task:
- Print all squares less than n:
- Initialize a variable i to 0.
- While i squared is less than n, do the following:
- Print i squared.
- Increment i by 1.
Here's the code:
int i = 0;
while (i * i < n) {
out.print(i * i + " ");
i++;
}
- Print all positive numbers that are divisible by 10 and less than n:
- Initialize a variable i to 10.
- While i is less than n, do the following:
- Print i.
- Increment i by 10.
Here's the code:
int i = 10;
while (i < n) {
out.print(i + " ");
i += 10;
}
- Print all powers of two less than n:
- Initialize a variable i to 1.
- While i is less than n, do the following:
- Print i.
- Multiply i by 2.
Here's the code:
int i = 1;
while (i < n) {
out.print(i + " ");
i *= 2;
}
Similar Questions
(This is from Programming Exercise P4.1 at the end of Chapter 4 of Java for Everyone.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:The sum of all even numbers between 2 and 100 (inclusive).The sum of all squares between 1 and 100 (inclusive).All powers of 2 from 20 up to 220 (inclusive).The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)
Single File Programming QuestionProblem StatementA curious young student named Liam stumbled upon a mathematical problem that computes the sum of the squares of its digits.Help Liam compute the result by writing a program using a do-while loop.Note: This question helps in clearing AMCAT exam.Input format :The input consists of a positive integer n.Output format :The output displays the result representing the sum of the squares of its digits.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 103Sample test cases :Input 1 :1Output 1 :1Input 2 :1000Output 2 :1Input 3 :55Output 3 :50
James, a mathematics teacher, is developing a programming exercise to help his students practice continuously summing the digits of a number until it becomes a single-digit integer. He wants to create a simple program using a 'while' loop that takes a positive integer input and generates the final single-digit result. Input format :The input consists of a positive integer n.Output format :The output prints "The single-digit sum of n is Y." where n is the input integer and Y is its single-digit sum.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 1010Sample test cases :Input 1 :456Output 1 :The single-digit sum of 456 is 6.Input 2 :999Output 2 :The single-digit sum of 999
Sophie, an aspiring Java programmer, is embarking on an adventure to explore arrays. She wants to create a Java program that allows users to input elements into an array and then prints the array of the squares of the given array elements. Can you assist Sophie by providing the Java code for her array adventure? Input Format: An integer N represents the size of the array. N integers represent the elements of the array, separated by space. Output Format: Print the squares of the elements of the array.
Write a Python program to print the square of first 10 natural numbers, using while loop
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.