Question

In: Computer Science

For input you can either a console Scanner or a dialog box (a modal dialog) and...

For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble()

-----

Write a Java program to calculate the first n ( the user enters the integer n) rows of the Pascal triangle. On demand display the first n rows of the Pascal triangle, a range of rows or an individual row n. Test by displaying the first 10 rows, the range 7:12 ( rows 7 through 12) or an individual row 15. The display of the first rows should looks like this:

                     1

                   1 1

                 1   2 1

               1 3   3 1

             1 4 6   4   1

and having the property that each row begins and ends with ones and every element is the sum of the two entries just above it.

Solutions

Expert Solution

Please refer the following JAVA code:

We have used console scanner for input and printed pascals triangle till n th row.

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

class Main { 

public static void printPascal(int n) 
{ 
    int val=n;
        for(int line = 1; line <= n; line++) 
        { 
            for(int k=0;k<val;k++){
                System.out.print(" ");
            }
            val--;      
            int C=1;
        
        for(int i = 1; i <= line; i++) 
        { 
                System.out.print(C+" "); 
                C = C * (line - i) / i; 
        } 
        System.out.println(); 
        } 
} 

public static void main (String[] args) { 
        int n;
        Scanner snr = new Scanner(System.in);
        System.out.println("Enter the number of rows till which you want to print pascals Triangle");
        n=snr.nextInt();
        printPascal(n); 
    } 
} 

Output for test case n=10 as metioned in question:


Related Solutions

For input you can either a console Scanner or a dialog box (a modal dialog) and...
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble() ---- Write a java the ”Letter grade” program that inputs several test scores from user and displays for each the corresponding letter grade using a scale 90 – 80...
For input you can either a console Scanner or a dialog box (a modal dialog) and...
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble() --- Write a java ”Statistics program” to calculate the mean, variance and standard deviation for a given set of numbers. The user in entering the data interactively. The size...
For input you can either a console Scanner or a dialog box (a modal dialog) and...
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble() ----- . If an amount of A dollars is borrowed at an interest rate (expressed as a decimal) r for y years, and n is the number of annual...
Please write a java program (using dialog box statements for user input) that has the following...
Please write a java program (using dialog box statements for user input) that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled,...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
You can purchase an optical scanner today for $450. The scanner provides benefits worth $66 a...
You can purchase an optical scanner today for $450. The scanner provides benefits worth $66 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 20% per year. Suppose the discount rate is 10%. Should you purchase the scanner today? Yes No When is the best purchase time? Wait for 4 years Wait for 5 years Wait for 6 years
You can purchase an optical scanner today for $400. The scanner provides benefits worth $60 a...
You can purchase an optical scanner today for $400. The scanner provides benefits worth $60 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 20% per year. Suppose the discount rate is 10%. Should you purchase the scanner today or wait to purchase? When is the best purchase time?
Text effects can play an important role in polishing a document, but they also can overwhelm a document and distract readers. Consider the text effects available in the Font Dialog Box.
Microsoft Word ClassText effects can play an important role in polishing a document, but they also can overwhelm a document and distract readers. Consider the text effects available in the Font Dialog Box.How many effects are too many/just right for use in a document?What is the result of using too many effects?At what point is the document overwhelmed?Should the type of effect used vary according to the type of document?Please share your experiences in which content was undermined by animated...
1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
Create a lottery game application. Include a menu that allows the user to play more than once, or quit after a game. You can either use dialog boxes or text based, up to you.
FOR JAVA:Summary:Create a lottery game application. Include a menu that allows the user to play more than once, or quit after a game. You can either use dialog boxes or text based, up to you.The solution file should be named Lottery.javaGenerate four random numbers, each between 0 and 9 (inclusive).Allow the user to guess four numbers.Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four-digit number,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT