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 to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
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...
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 String 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT