Question

In: Computer Science

java. please don't use complicated language I can follow up. Write a for loop that prints...

java. please don't use complicated language I can follow up.

Write a for loop that prints the integers from 1 to 100, all on one line, space-separated. However, after printing 3 numbers, you need to skip the next number and print a counter in parenthesis.

1 2 3 (1) 5 6 7 (2) 9 10 11 (3) 13 14 15 (4) 17 18 19 [... and so on ...]

Write this code once with for loop, once with while loop, and once with do while in three separate files.

Put all three files inside a folder, compress the folder (create a zip file) and submit the zip file.

Solutions

Expert Solution

Using for loop:

Source Code:

Output:

Code in text format (See above images of code for indentation):

import java.util.*;

/*class definition*/

public class usingFor

{

    /*main method*/

    public static void main(String[] args)

    {

        /*variables*/

        int c=1,i;

        /*using for loop print numbers*/

        for(i=1;i<=100;i++)

        {

            /*after 3 numbers skip next and print count*/

            if(i%4==0)

                System.out.print("("+c+++") ");

            /*print numbers*/

            else

                System.out.print(i+" ");

        }

    }

}

Using while loop:

Source Code:

Output:

Code in text format (See above images of code for indentation):

import java.util.*;

/*class definition*/

public class usingWhile

{

    /*main method*/

    public static void main(String[] args)

    {

        /*variables*/

        int c=1,i=1;

        /*using while loop print numbers*/

        while(i<=100)

        {

            /*after 3 numbers skip next and print count*/

            if(i%4==0)

                System.out.print("("+c+++") ");

            /*print numbers*/

            else

                System.out.print(i+" ");

            i++;

        }

    }

}

Using do-while loop:

Source Code:

Output:

Code in text format (See above images of code for indentation):

import java.util.*;

/*class definition*/

public class usingDoWhile

{

    /*main method*/

    public static void main(String[] args)

    {

        /*variables*/

        int c=1,i=1;

        /*using do-while loop print numbers*/

        do

        {

            /*after 3 numbers skip next and print count*/

            if(i%4==0)

                System.out.print("("+c+++") ");

            /*print numbers*/

            else

                System.out.print(i+" ");

            i++;

        }while(i<=100);

    }

}


Related Solutions

*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a Java loop statement that prints count. Count begins with 1. It increments as multiples...
Write a Java loop statement that prints count. Count begins with 1. It increments as multiples of 2 and does the loop until count is less than 50.
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks 1. A driver class with a main method, which creates instances of various objects (two of each subclass) and adds them as items to an ArrayList named "inventory". A foreach loop should loop through the ArrayList and call the use() method of each item. Define the ArrayList in a way that it only holds elements of the GameItem class and its subclasses. Make proper...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you...
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you complete my answer , i need you answer b only Question: 3- Al Yamamah Steel Industries Co. uses the step method for allocating the costs of its service departments to operating departments. The company has two support departments (Human Resource and Information Technology) and two operating departments (Hot Rolled Hollow Steel and Cold Rolled Hollow Steel). Al Yamamah Steel Industries Co. decided to allocate...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT