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,...
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?
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.
Match the numbers below with the correct answer on the drop down menu. On answer can...
Match the numbers below with the correct answer on the drop down menu. On answer can be used more than once or not at all. 1. Stimulates liver for overall growth. 2. Inactive enzyme released by the liver for RAAS. 3.Zona of suprarenal glands where glucocorticoids are secreted. 4. Main hormones secreted by the zona fasciculata of suprarenal glands. 5. Released from suprarenal cortex and key player in RAAS. 6.Stimulates production of testosterone in males and post ovulation phase in...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
SOLVE FOLLOWING a.   Desgin and VERILOG code of a 3 bit up down counter USING T...
SOLVE FOLLOWING a.   Desgin and VERILOG code of a 3 bit up down counter USING T FLIP FLOP..... b. using behavioural module.Write a verilog discription of an N-BIT up down binary counter. Record the simulation output waveform in observation.....
Write up 2 pages about the case below, including these elements (What is the main idea...
Write up 2 pages about the case below, including these elements (What is the main idea of the article?,What are you own personal thoughts, ideas, reflections?, How might apply the ideas in the article in practice? o Sharing personal experiences is a plus) Despite millions of dollars spent on combating the high turnover rate among insurance agents, the rate—approximately 50% within the first year and 80% within the first three years—had remained steady for the more than 35 years preceding...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Having indentation problems. I have no idea why one of my breaks are screwing up. This...
Having indentation problems. I have no idea why one of my breaks are screwing up. This should be a easy fix and thumbs up for you lads out there. here's my code. Ill leave a comment on where the break problem is. Also if you could do be a solid and run it to see if it executes. Thanks! Python Code: def to_euro(dollar): return float(dollar)*0.81; def to_yen(dollar): return float(dollar)*106.45; def to_peso(dollar): return float(dollar)*18.58; def main(): while(1): x=input("Enter 1 for Euro,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT