Question

In: Computer Science

Question #2 Write a program that prints a nicely formatted table of the product of every...

  • Question #2 Write a program that prints a nicely formatted table of the product of every pair of numbers in a given set of integer numbers. Your program reads two integer numbers x and y that represent the lower and upper bounds of the set, (?≤?x≤y), respectively. Then, for every pair of numbers in this set {?,?+1,?+2,…,?}{x,x+1,x+2,…,y}, your program should compute the product of the two numbers.
  • Please note the following:
    1. Your program should read each input (lower bound and the upper bound) one at a time, and should make sure that each input is an integer. If not, it should prompt the user to re-enter a valid input.
    2. Once the two numbers are read, your program should validate that the lower bound is less than or equal to the upper bound. Otherwise, the input should be read again, starting from the lower bound.

Solutions

Expert Solution

Here's the program that prints the desired output as per the question :

Note:- As the programming environment is not mentioned I've used Java Programming language and Python for the above question, If you need this code in C, C++ please let me know in comments.

(IN Python)

print ("Enter lower and upper bounds :")
l=int(input())
u=int(input())
while l>u:
    print ("Incorrect bounds please re-enter :")
    print ("Enter lower and upper bounds :")
    l=int(input())
    u=int(input())
size=u-l+1
arr=[]
prdct=[]
a=l
for i in range(0,size):
    arr.append(a)
    a=a+1
for i in range (0,size):
    for j in range(0,size):
        prdct.append(arr[i]*arr[j])


print("Set of numbers are :")
for i in arr:
    print(i,end=" ")

print("\n Products of each pair are :")
for i in prdct:
    print(i,end=" ")

Output of above python program

(IN JAVA)


import java.util.*;
public class Main
{
        public static void main(String[] args) {
                System.out.println("Enter Lower and Upper bounds :");
                Scanner s=new Scanner(System.in);
                int l=s.nextInt();
                int u=s.nextInt();
                while(l>u)
                {System.out.println("\nIncorrect bounds please re-enter");
                 System.out.println("\nEnter Lower and Upper bounds :");
                 l=s.nextInt();
                 u=s.nextInt();
                  
                }
                int size=u-l+1;
                int []arr=new int [size];
                int []prdct= new int [size*size];
                int k=0;
                int a=l;
                for(int i=0;i<size;i++)
                {
                   arr[i]=a;
                   a++;
                }
                for(int i=0;i<size;i++)
                {
                    for(int j=0;j<size;j++)
                    {   
                        prdct[k]=arr[i]*arr[j];
                        k++;
                    }
                }
                System.out.println("Set of numbers are : ");
                for(int i=0;i<size;i++)
                System.out.print(" "+arr[i]);
                System.out.println();
                System.out.println("Product of pair are :" );
                for(int i=0;i<size*size;i++)
                System.out.print(" "+prdct[i]);
        }
}

It compiles and runs successfully and gives output as:


Related Solutions

For this problem, write a Ruby script named arr2cols.rb that prints an array’s contents as formatted...
For this problem, write a Ruby script named arr2cols.rb that prints an array’s contents as formatted rows and columns. The data is provided by the predefined arrays below: escargot_player_data, employees, andartists. Your script must work with all three arrays. The number of columns in the data will be consistent within each data set, but each dataset will have a different number of columns. We can the use Array#each method to iterate all of the elements in an array. each will...
Write the program that prints the product of 79 and 3.684. The output statement should be...
Write the program that prints the product of 79 and 3.684. The output statement should be descriptive, and should include the printing of all three variables (the two operands as well as the product). Write In C++
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
Write a program that prints a custom conversion table from Celsius temperatures to Fahrenheit and Newton...
Write a program that prints a custom conversion table from Celsius temperatures to Fahrenheit and Newton (Links to an external site.) temperatures. The formula for the conversion from Celsius to Fahrenheit is : F=9/5*C+32 F is the Fahrenheit temperature, and C is the Celsius temperature. The formula for the conversion from Celsius to Newton is C = 100/33*N N is the Newton Temperature and C is the Celsius temperature Your C++ program should prompt the user for a lower value...
Write a program that prints the question “Do you wish to continue?” and reads the input....
Write a program that prints the question “Do you wish to continue?” and reads the input. If the user input is “Y”, “Yes”, “YES”, then print out “Continuing”. If the user input is “N” or “No”, “NO” then print out “Quit”. Otherwise, print “Bad Input”. Use logical operators. c++
java language question: Write a thread that continuously prints a message every 100 milliseconds while it...
java language question: Write a thread that continuously prints a message every 100 milliseconds while it is still alive. The thread should be written in such a way that it can be terminated at any time by a control program (main).
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
write a program to calculate and print payslips write program that calculates and prints payslips. User...
write a program to calculate and print payslips write program that calculates and prints payslips. User inputs are the name of employee, numbers of hours worked and hourly rate c++ language
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are multiples of 10, starting at 0 and ending at 100. Write a python program that computes the average of 10 random odd integers, ranging from 0 to 500. Write a python program that prints whatever the user enters, and stops when the user types “done”, using an infinite loop and a break statement. Same as number three but use a condition. The loop terminates...
Create a Java program which prints a truth table for any 2 variables propositional function. Assuming...
Create a Java program which prints a truth table for any 2 variables propositional function. Assuming variables p and q. Instead of the symbols for "and" and "or", just used the words. For "not", use the tilde "~" Prompt the user for 3 questions 1: "p and q"? Or "p or q"? 2: Do you want to negate p? 3: Do you want to negate q? For example, suppose in a menu system the user chose "p or q", and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT