In: Computer Science
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.
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);
}
}