Preview

Java Sample Program: Pascal's Triangle

Good Essays
Open Document
Open Document
1391 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Java Sample Program: Pascal's Triangle
Java Sample Program

1. Write a program in Java that allows the user to enter a number and then display its Pascal’s Triangle -The triangle is bordered by ones on the right and left sides, and each interior entry is the sum of the two entries above.

Sample Input/Output:
Please enter a value for Triangle: 6

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

Solution:

import java.io.*; import java.util.*; public class Triangle
{
public static void main(String a[]) throws Exception { Scanner pasS = new Scanner(System.in); int pascalN; System.out.print("\nPlease enter a value for Triangle: "); pascalN = pasS.nextInt(); int[][] pascalA; if (pascalN > 0){ pascalA = pascalsTriagnle (pascalN); int space = pascalN; for (int pC=0; pC<pascalA.length; pC++) { space(space); for (int pC2=0; pC2<pascalA[pC].length; pC2++){ System.out.print(pascalA[pC][pC2]+" "); } space(space); space--; System.out.println(""); } } } public static int[][] pascalsTriagnle (int n) { int[][] newPA = new int[n][]; int mycounter = 1; for (int npc = 0; npc<n; npc++){ newPA[npc] = new int[mycounter]; for (int npc0 = 0; npc0<=npc; npc0++){ if (npc0==0 || npc==npc0){ newPA[npc][npc0] =1;} else { int firstN = newPA[npc-1][npc0]; int secondN = newPA[npc-1][npc0-1]; newPA[npc][npc0] =firstN+secondN; } } mycounter++; } return newPA; } public static void space (int n){ // For spacing... for (int x = 0; x<n; x++){ System.out.print(" "); }
}

2. “Guess the Number” using Java.
Welcome to the Guess the Number!
Guess the number from 1 - 100.

Your Guess Number: 65
The number is greater than

You May Also Find These Documents Helpful

  • Satisfactory Essays

    cout << "You are in factorial program of C++. Please enter a +ve integer:- ";…

    • 426 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    QUESTION SET 1 EIMACS FRQ

    • 564 Words
    • 9 Pages

    //Part a (note, only certain parts will be needed for submit, don't just copypasta like crazy…

    • 564 Words
    • 9 Pages
    Good Essays
  • Satisfactory Essays

    Write a program that can be used by a small theater to sell tickets for performances. The theater’s auditorium has 15 rows of seats, with 20 seats in each row. The program should display a screen that shows which seats are available and which are taken. For example, the following screen shows a chart depicting each seat in the theater. Seats that are taken are represented by an * symbol and seats…

    • 703 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Step 1: Examine the following algorithm as a base for how the program should flow. (Reference: Designing a Program, page 31).…

    • 777 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    Arrays store items that have the same type of data type like a group of employees’ names and social security numbers for a team of 2000 personal. Pointer is a variable that greatly extends the power and flexibility of a program, each memory location that is used to store data value has an address. The address provides the means for a PC hardware to reference a particular data item.…

    • 485 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Pt1420 Unit 3 Essay

    • 966 Words
    • 4 Pages

    3- Print multiplication worksheets for everyone. They will try to solve the multiplication problems while pressing the spacebar on the keyboard every thirty seconds. To do that, they will have a timer next to them and are able to look at it whenever needed.…

    • 966 Words
    • 4 Pages
    Good Essays
  • Good Essays

    unit 8 homework

    • 793 Words
    • 5 Pages

    4. Design a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.…

    • 793 Words
    • 5 Pages
    Good Essays
  • Satisfactory Essays

    unit 3

    • 313 Words
    • 2 Pages

    Design a program that calculates the total amount of meal purchased at a restaurant. The program should ask the user to enter the charge for the food, and then calculate the amount of a 15% tip and 7% sales tax. Display each of these amounts and the total.…

    • 313 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Java Chapter 2 Quiz

    • 2047 Words
    • 9 Pages

    4. The ____ is normally considered the standard output and standard input devices, and usually refer to…

    • 2047 Words
    • 9 Pages
    Powerful Essays
  • Satisfactory Essays

    9.) A bug collector collects bugs every day for 7 days. Design a program that keeps a running total of the number of bugs collected during the 7 days. The loop should ask for the number of bugs collected each day, and when the loop is finished, the program should display the total number of bugs collected.…

    • 651 Words
    • 4 Pages
    Satisfactory Essays
  • Powerful Essays

    EAS230Syllabus

    • 1748 Words
    • 8 Pages

    An introduction to computer programming with an emphasis on problem solving will be presented. Specific topics include:…

    • 1748 Words
    • 8 Pages
    Powerful Essays
  • Satisfactory Essays

    /*Class. Project 1*/ import java.io. BufferedReader; import java.io. FileReader; import java.util. StringTokenizer; import java.io.…

    • 779 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    Programming Logic

    • 1499 Words
    • 6 Pages

    2. Design the logic for a program that outputs every number from 1 through 10.…

    • 1499 Words
    • 6 Pages
    Good Essays
  • Satisfactory Essays

    carloe :)

    • 253 Words
    • 2 Pages

    Problem 2. a) Write an application containing three parallel arrays that hold 10 elements each. The first array holds four-digit student ID numbers, the second holds first names, and the…

    • 253 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Math Ia Pascals Triangles

    • 576 Words
    • 3 Pages

    • Display results in a table. Using technology, display your results in a discrete graph…

    • 576 Words
    • 3 Pages
    Satisfactory Essays

Related Topics