Question

In: Computer Science

Summary In this lab the completed program should print the numbers 0 through 10, along with...

Summary

In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop.

Instructions

  1. Write a for loop that uses the loop control variable to take on the values 0 through 10.
  2. In the body of the loop, multiply the value of the loop control variable by 2 and by 10.
  3. Execute the program. Is the output the same?
  4. // NewMultiply.java - This program prints the numbers 0 through 10 along
    // with these values multiplied by 2 and by 10.
    // Input: None.
    // Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10.


    public class NewMultiply
    {
    public static void main(String args[])
    {
      
    String head1 = "Number: ";
    String head2 = "Multiplied by 2: ";
    String head3 = "Multiplied by 10: ";   
    int numberCounter; // Numbers 0 through 10.
    int byTen; // Stores the number multiplied by 10.
    int byTwo; // Stores the number multiplied by 2.
    final int NUM_LOOPS = 10; // Constant used to control loop.

    // This is the work done in the housekeeping() method
    System.out.println("0 through 10 multiplied by 2 and by 10" + "\n");

    // This is the work done in the detailLoop() method
    // Write for loop

    // This is the work done in the endOfJob() method
    System.exit(0);
    } // End of main() method.

    } // End of NewMultiply class.

Solutions

Expert Solution

public class NewMultiply
{
  public static void main(String args[])
  {

    String head1 = "Number: ";
    String head2 = "Multiplied by 2: ";
    String head3 = "Multiplied by 10: ";
    int numberCounter; // Numbers 0 through 10.
    int byTen; // Stores the number multiplied by 10.
    int byTwo; // Stores the number multiplied by 2.
    final int NUM_LOOPS = 10; // Constant used to control loop.

// This is the work done in the housekeeping() method
    System.out.println("0 through 10 multiplied by 2 and by 10" + "\n");

// This is the work done in the detailLoop() method
    System.out.println(head1+"\t"+head2+"\t"+head3);
    for(numberCounter = 0;numberCounter<=NUM_LOOPS;numberCounter++){
      byTwo = numberCounter * 2;
      byTen = numberCounter * 10;
      System.out.println(numberCounter+"\t\t\t"+byTwo+"\t\t\t\t\t"+byTen);
    }
// Write for loop

// This is the work done in the endOfJob() method
    System.exit(0);
  } // End of main() method.

} // End of NewMultiply class.


Related Solutions

USING a LOOP for C++ In this lab the completed program should print the numbers 0...
USING a LOOP for C++ In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10....
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
Instructions:  Code a for statement that increments from 1 through 10, but print only the even numbers...
Instructions:  Code a for statement that increments from 1 through 10, but print only the even numbers using a fall-thru switch. You can catch the odd numbers using the option in the switch that handles everything else. Name your program ForFallThruSwitch.java. Use Java Style Guide in line advancing and spacing. --------------------OUTPUT RESULTS-------------------- Not printing odd numbers! 2 is an even number. Not printing odd numbers! 4 is an even number. Not printing odd numbers! 6 is an even number. Not printing...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
There are three things wrong with this program. List each. print("This program takes three numbers and...
There are three things wrong with this program. List each. print("This program takes three numbers and returns the sum.") total = 0 for i in range(3):     x = input("Enter a number: ")     total = total + i print("The total is:", x)
Summary In this lab, you will make additions to a C++ program that is provided. The...
Summary In this lab, you will make additions to a C++ program that is provided. The program is a guessing game. A random number between 1 and 10 is generated in the program. The user enters a number between 1 and 10, trying to guess the correct number. If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise, the program asks the user if he or she wants to guess...
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT