Question

In: Computer Science

Any idea why the code below prints the numbers up and down instead of sideways? They...

Any idea why the code below prints the numbers up and down instead of sideways?

They should print like this:

299 297 275 273 264   

260 253 247 242 234   

231 221 220 209 208   

Found 15 integers totaling 3733

Mine prints like this:

299
297
275
273
264

260
253
247
242
234

231
221
220
209
208

Number of multiples are: 15 The sum of multiples are: 3733

public static void main(String[] args) { //start of the main
        {
            int highestNumber = 300;
            int countOfNumbers = 0;
            int totalSum = 0;

            while (highestNumber >= 200) {
                if ((highestNumber % 11 == 0) && (highestNumber % 13 != 0)) {
                    System.out.println(highestNumber + "");
                    countOfNumbers++;
                    totalSum += highestNumber;
                    if (countOfNumbers % 5 == 0)
                        System.out.println();
                }
                if ((highestNumber % 13 == 0) && (highestNumber % 11 != 0)) {
                    System.out.println(highestNumber + "");
                    countOfNumbers++;
                    totalSum += highestNumber;
                    if (countOfNumbers % 5 == 0)
                        System.out.println();
                }
                highestNumber = highestNumber - 1;
            }
            System.out.println("Number of multiples are: " + countOfNumbers + " The sum of multiples are: " + totalSum);
        }
    }
}

Solutions

Expert Solution

It is solved by removing ln from println which prints a newline character. And also included space in both the print statements. (These have been commented for reference).

CODE

import java.util.*;

 // Compiler version JDK 11.0.2

 class Dcoder
 {
   
public static void main(String[] args) { //start of the main
        {
            int highestNumber = 300;
            int countOfNumbers = 0;
            int totalSum = 0;

            while (highestNumber >= 200) {
                if ((highestNumber % 11 == 0) && (highestNumber % 13 != 0)) {
                    System.out.print(highestNumber + " ");// remove ln from println
                                                          // and include space
                    countOfNumbers++;
                    totalSum += highestNumber;
                    if (countOfNumbers % 5 == 0)
                        System.out.println();
                }
                if ((highestNumber % 13 == 0) && (highestNumber % 11 != 0)) {
                    System.out.print(highestNumber + " ");// same editing here also 
                    countOfNumbers++;
                    totalSum += highestNumber;
                    if (countOfNumbers % 5 == 0)
                        System.out.println();
                }
                highestNumber = highestNumber - 1;
            }
            System.out.println("Number of multiples are: " + countOfNumbers + " The sum of multiples are: " + totalSum);
        }
    }
}

Refer below for output preview.

Please Upvote if you found it helpful :)


Related Solutions

Modify the code below to implement the program that will sum up 1000 numbers using 5...
Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the user. #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define N_OF_THREADS 4 void * print(void...
I came up with a venture design idea of Cardboard Window Blinds instead of the conventional...
I came up with a venture design idea of Cardboard Window Blinds instead of the conventional wood blinds or paper blinds for my finals. I was taught that the definition of distinctive competency follows the VRIO framework i.e. True distinctive competency means a product has to be: Valuable to your customer, Rare among solution providers, superior to those of other competitors and Costly for others to imitate!. QUESTION: Product: Cardboard Window Blind Imagine you are the one developing this product,...
Write LMC assembly code that prints the minimum of two numbers. HINT: use and extend the...
Write LMC assembly code that prints the minimum of two numbers. HINT: use and extend the table below for your solution. Mailbox Mnemonic Code Instruction description
You throw out the idea of working your way up the bureaucracy for obvious reasons. Instead,...
You throw out the idea of working your way up the bureaucracy for obvious reasons. Instead, you decide to go into elected office. You just got elected to the Indiana State Legislature after serving for the past four years on city council. As a new member you are filled with innovative ideas and want to hit the ground running. You ask a colleague where you can find help drafting legislation on agriculture. They reply “well, if you have an idea,...
When writing code in any language, it is always a good idea to work in a...
When writing code in any language, it is always a good idea to work in a modular fashion. In this approach you will write a small piece of code (usually a small logical module), and test to ensure it works. If it does, you will add more code, and test it again. This way, if the program were to crash after you last addition, you will know where to look for bugs. Debugging a few lines of code is much...
The code numbers are listed in the Alphabetic Index. why not code directly from it?
The code numbers are listed in the Alphabetic Index. why not code directly from it?
Need to modify the below code to return the time in minutes instead of seconds. (Using...
Need to modify the below code to return the time in minutes instead of seconds. (Using Python 2.7.6 ) def numberPossiblePasswords(numDigits, numPossiblePerDigit):     numPasswords = numPossiblePerDigit**numDigits     return numPasswords def maxSecondsToCrack(numPossiblePasswords, secPerAttempt):     time = numPossiblePasswords*secPerAttempt     return time nd = int(input("How many digits long is the passcode? "))       nc = int(input("How many possible characters are there per digit? ")) secondsPerAttempt = .08 npp = numberPossiblePasswords(nd, nc) totalSeconds = maxSecondsToCrack(npp, secondsPerAttempt) print("It will take you " + str(totalSeconds) + "...
Is there any idea that which of the below is true regarding data preparation and integration...
Is there any idea that which of the below is true regarding data preparation and integration thing? 1. Due to data silos exist for the organization and the data must be integrated to reduce data inconsistency often 2. For increasing decision efficiency & data security it must be isolated in separate information systems anyway 3. Actually, vertical data integration enriches existing information and on the other hand horizontal data integration merges tables that hold the same information most of the...
Use BASH to make a code that takes any list of numbers and calculate the Median...
Use BASH to make a code that takes any list of numbers and calculate the Median and Mode.
Python. 1.Why is it a good idea to write and test the code for laying out...
Python. 1.Why is it a good idea to write and test the code for laying out a window's components before you add the methods that perform computations in response to events? 2.Explain why you would not use a text field to perform input and output of numbers. 3.Write a line of code that adds a FloatField to a window, at position (1, 1) in the grid, with an initial value of 0.0, a width of 15, and a precision of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT