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 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 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...
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 (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value. (Analysis of Algorithms question) Determine the growth function and time complexity (in Big-Oh notation) of the...
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 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...
please don't use handwriting .. use your own words i need unique answer .. please don't...
please don't use handwriting .. use your own words i need unique answer .. please don't copy and paste Q1.Dala Corporation is considering a project, which will involve the following cash inflows and (out) flows Details Amount Initial Outlay SAR (400000) After 1 Year SAR 40000 After 2 Years SAR 300000 After 3Years SAR 300000 What will be the NPV of this project if a discount rate of 15% is used? _________________ Q2.Merck Inc. is about to undertake a project...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT