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
  • 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
  • Better Essays

    Nt1310 Unit 1 Assignment

    • 1994 Words
    • 8 Pages

    * The binary numbering system plays a central role in how information of all kinds is stored on the computer. Understanding binary can lift a lot of the mysteries from computers because at a fundamental level they're really just machines for flipping binary digits on and off. There are several activities on binary numbers in this document, all simple enough that they can be used to teach the binary system to anyone who can count! Generally children learn the binary system very quickly using this approach, but we find that many adults are also excited when they finally understand what bits…

    • 1994 Words
    • 8 Pages
    Better 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

    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
  • Better Essays

    The phantom tollbooth

    • 1441 Words
    • 6 Pages

    The Phantom Tollbooth is a novel and fairy tale by Norton Juster. The story is about there’s one a boy name Milo, he never know what to do with himself. He always is bored at school and home. Everything he does is always a waste of time. One day Milo open envelope and it say one genuine turnpike tollbooth. So he built a tollbooth in his room, and he had a map that colorful with different road names. Then he imagine with his toy car, suddenly he found himself on a road he never seen, going to a place he never heard of and all because of a tollbooth which change from nowhere. The first place he ever went to expectations and he saw a good friend call a Watchdog but his name is Tock. Tock was really happy when he saw Milo. Tock wanted to travel with Milo through Island, so when they get out of expectation, they headed to Dictionopolis. After all they been through, Milo met many strange people he been through a quest to rescue rhyme and reason. In fact, it’s exciting beyond his wild dream. When he look back in his room and his dream, he learned more about life and better thing to do and he never wasted his time again.…

    • 1441 Words
    • 6 Pages
    Better 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