JAVASCRIPT: To carry out a program that prints Pascal's triangle, the user must enter the number of rows to display.
Here is the complete code to print Pascal's Triangle while input being given by the user to print the number of rows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .container { margin: 0 auto; } .block { display: inline-block; position: relative; border: 1px solid grey; text-align: center; font-size: .8em; width: 2em; } .divblock { text-align: center; vertical-align: middle; } #pascal { margin: 0 auto; text-align: center; } .block:nth-child(odd) { background: cyan; } .block:nth-child(even) { background: grey; } </style> <script> function printPascalTriangle(){ document.getElementById("pascal").innerHTML = ""; var rows = document.getElementById("numRows").value; var arr = generatePascal(+rows); for(var i=0;i<arr.length;i++){ var div = document.createElement('div'); div.className ="divblock" for(var j=0;j<arr[i].length;j++){ var span = document.createElement('span'); span.innerHTML=arr[i][j]; span.className ="block"; div.appendChild(span); } document.getElementById("pascal").appendChild(div); } } function generatePascal(n){ var arr = []; var tmp; for(var i=0;i<n;i++){ arr[i]=[]; for(var j=0; j<=i; j++){ if(j==i){ arr[i].push(1); }else{ tmp = (!!arr[i-1][j-1]?arr[i-1][j-1]:0)+(!!arr[i-1][j]?arr[i-1][j]:0); arr[i].push(tmp); } } } return arr; } </script> </head> <body> <div class="container"> <input type="text" placeholder="Enter number of rows for pascal Triangle" style="width:80%" id="numRows"> <input type="button" " value="Go " name="Go " onClick="printPascalTriangle() "> <div id="pascal"></div> </div> </body> </html>
Snapshots of Output:
1) Taking Input: As you can see on the console (right side), it is
asking the user to input number of lines to be printed.
2) Printing 7 lines for triangle:
3) Printing 10 lines for triangle:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I have uploaded the code as per your requirement.
Please give a thumbs-up to this answer, it'll be a great help for me.
Thank you.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
JAVASCRIPT: To carry out a program that prints Pascal's triangle, the user must enter the number...
Create a Pascal's Triangle in Python or Atom Javascript the program allows the user to input any # and creates the triangle from it
Please solve in C++ Pascal's Triangle Videos [edit] write a C++ that asks the user to enter the number of rows (N) and then prints the first N rows of Pascal's Triangle, che row per line. Your program should echo print N. Good News [edit] The best way to construct Pascal's Triangle is to use a nested loop with repeated calls to the combination already written for a previous programming assignment, Just #include "myFunctions.h" and use the combination function from...
Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * * * * * * * **...
complete a program so that it asks the user to enter a number and then prints out the user's number, 2x the user's number, and 10x the user's number
use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. (pascals 0 0) → 1 (pascals 2 0) → 1 (pascals 2 1) → 2 (pascals 4 2) → 6 (printTriangle 5) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 [5 marks] Write a procedure...
Write a program that asks the user for a negative number, then prints out the product of all the numbers from -1 to that number Enter a negative number: -6 720 (The answer is 720 because-1x-2x-3x-4x-5 x-6 720).
Starting Out with C++ (9th Edition) Chapter 4, Problem 7PCWrite a program that asks the user to enter a number of seconds.• There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.• There are 3600 seconds in an hour. If the number of seconds entered by the user is less than 86400, but is greater...
please solve using python thank you! Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * *...
1) Write a Python program that prompts the user to enter the current month name and prints the season for that month. Hint: If the user enters March, the output should be "Spring"; if the user enters June, the output should be "Summer". 2 )Write a Python program using the recursive/loop structure to print out an equilateral triangle below (double spacing and one space between any two adjacent asterisks in the same row). * * * * *...
How to write a Java program that after prompting the user to enter the lengths of sides (3) of a triangle, it prints: "Enter the lengths of the sides of triangle 1: 6 6 6" "A triangle with sides of 6,6,6 is equilateral."