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. 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
/*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:*/