Question

In: Computer Science

Using Java, write a program that takes in two integers from the keyboard called m and...

Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.

Solutions

Expert Solution

Here is the code:

import java.util.*;
import java.io.*;

class PrintNums {
//ceiling function for m/n
public static int ceil(int m, int n) {
//if m is divisible by n
if (m % n == 0) return m/n;
else return m/n + 1;
}
   public static void main (String[] args) {
//take input
int m, n;
System.out.print("Enter m,n(m>n): ");
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
//each row will contain at most ceil(m/n) elements
int elsInRow = ceil(m, n);
//create a row
for (int i=0; i<n; i++) {
//print at most ceil(m,n) elements
for (int j=0; j<elsInRow && m>0; j++) {
//print m and decrease it
System.out.print(m--+" ");
}
//print a newline
System.out.println();
}
   }
}

Here is a screenshot of the code:

Here is a screenshot of the output:

Comment in case of any doubts.


Related Solutions

Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Write a C++ program that takes input from the keyboard of the 3 dimensions of a...
Write a C++ program that takes input from the keyboard of the 3 dimensions of a room (W,L,H in feet) and calculates the area (square footage) needed to paint the walls only. Use function overloading in your program to pass variables of type int and double. You should have two functions: one that accepts int datatypes and one that accepts double datatypes. Also assume a default value of 8 if the height parameter is omitted when calling the functions.
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
In.java In this program write a method called upDown that takes three integers as arguments and...
In.java In this program write a method called upDown that takes three integers as arguments and returns one of these 3 strings: "increasing" if they are in strictly increasing order (note that 3,3,4 - are not strictly increasing), "decreasing" if they are in strictly decreasing order. "none" otherwise. I recommend you use a complex condition to check for this (that is, have a single if statement with one big question). In the main method do the following: read three integers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT