Question

In: Computer Science

You MUST use a while loop to print numbers 1-100. If the number is divisible by 3 print Soda instead of the number.


You MUST use a while loop to print numbers 1-100. If the number is divisible by 3 print Soda instead of the number. If the number is divisible by 5 print Pop instead of the number. If the number is divisible by 3 AND 5 print *SP*.

Print 10 numbers/words to a line. Display the numbers/words in right-aligned columns. Use printf statements to do this. For example:
System.out.printf( "5d", number );
    System.out.printf( "5s", "Soda" );
Every time there are ten numbers on a line print a newline to move down to the next line.

Use the command prompt window (terminal) for output.

And this is the sample out put:

    1    2 Soda    4  Pop Soda    7    8 Soda  Pop
   11 Soda   13   14 *SP*   16   17 Soda   19  Pop
 Soda   22   23 Soda  Pop   26 Soda   28   29 *SP*
   31   32 Soda   34  Pop Soda   37   38 Soda  Pop
   41 Soda   43   44 *SP*   46   47 Soda   49  Pop
 Soda   52   53 Soda  Pop   56 Soda   58   59 *SP*
   61   62 Soda   64  Pop Soda   67   68 Soda  Pop
   71 Soda   73   74 *SP*   76   77 Soda   79  Pop
 Soda   82   83 Soda  Pop   86 Soda   88   89 *SP*
   91   92 Soda   94  Pop Soda   97   98 Soda  Pop

Programmed by Your Full Name


Solutions

Expert Solution

/*The source code of this program is given below*/

public class NumPrint

{

public static void main(String[] args)

{

int number=1;/*Initialize number variable*/

/*while loop will continue till number becomes 100*/

while(number<=100)

{

/*By default printf method is right-justified*/

/*if number is divisible by 3 but not by 5*/

if((number%3==0) && (number%5!=0))

{

System.out.printf("%5s","Soda");

}

/*if number is divisible by 5 but not by 3*/

else if((number%3!=0) && (number%5==0))

{

System.out.printf("%5s","Pop");

}

/*if number is divisible by 3 and also divisible by 5*/

else if((number%3==0) && (number%5==0))

{

System.out.printf("%5s","*SP*");

}

else

{

System.out.printf("%5d",number);

}

/*printing a new line after printing 10 numbers/words in a line */

if(number%10==0)

{

System.out.printf("\n");

}

/*increase number by 1*/

number++;

}

}

}

/*For better understanding code image and output screenshot is given below:*/


Related Solutions

10. Write a for loop which will print every number evenly divisible by 13 between 1...
10. Write a for loop which will print every number evenly divisible by 13 between 1 and 100, inclusive. Your loop will only increment by 1 each loop so you need an if test to see if each number should be printed. Put each number output on the same line, 1 space apart. 11. Write a while loop which will prompt the user for a number and accept their input and then display this message (assuming number input is 3)...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
Write an algorithm that print all numbers that are divisible by 3 between 1000 and 2000
Write an algorithm that print all numbers that are divisible by 3 between 1000 and 2000
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Print greeting Prompt the user for starting odometer reading Use a while loop to validate that...
Print greeting Prompt the user for starting odometer reading Use a while loop to validate that starting odometer reading is a positive number. Initialize variables: last odometer reading, current odometer reading, leg number, total fuel, moreInput While moreInput == ‘y’ Prompt the user for new odometer reading and fuel consumed If fuel is positive and new odometer reading > last odometer reading: Calculate MPG for this leg using mpg = (new odometer – last odometer) / fuel Print MPG for...
1.Write a loop to print a list of numbers starting at 64 and ending at 339....
1.Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax. 2. Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer. Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward” 3.
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
Please, write a loop to print odd numbers an array a with pointers backwards. You can...
Please, write a loop to print odd numbers an array a with pointers backwards. You can use a variable “size” for array size.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT