Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Pseudo Code:
- Pascals's triangle:
- first create a method that will calculate factorials:
- The number will accept an integer as a parameter and return a factorial. It will do this by using a for loop which will
- run equal to the size of the number itself, and every iteration it will multiply the total number by the number of the current value of the for loop
- Create a method which will create Pascals Triangle
- In the method it will have two for loops, one for the row, and one for the number in the row. In the row loop:
- if it is the first number in the row it will print a certain amount of spaces based on the row, if it is not the first number, it will just print uses a 4 padded printf
- It will print the number itself by calling the factorial number and plugging in values depending on its place in the for loop
- In the main:
- It will prompt the user for a number of rows, and then call the pascals triangle method.
- */
- /**
- * Created by Daniel on 4/22/2016.
- */
- import java.util.*;
- public class Project7PascalsTriangle {
- public static void main(String[] args){
- Scanner c = new Scanner(System.in);
- System.out.println("Create Pascal's Triangle!");
- System.out.print("Enter a number of rows : ");
- int n = c.nextInt();
- System.out.println();
- triangle(n);
- }
- public static void triangle(int row){
- int num = 0;
- row--;
- for(int r = 0; r < row; r++){
- for(int n = 0; n < r+1;n++){
- num = (int) ((factorial(r))/(factorial(n)*factorial(r-n)));
- if (n == 0) {
- for (int i = 1; i <= r * -2 + row * 2 -2; i++)System.out.print(" ");
- System.out.print(num);
- }
- else System.out.printf("%4d", num);
- }
- System.out.println();
- }
- }
- public static double factorial(int n){
- double num = 1;
- for(int i = 1; i <= n;i++)num *= i;
- return num;
- }
- }
- /*
- Pseudo code for roman numerals!
- Create a method that will accept an integer value. The method will take the integer value, and print it in Roman Numerals.
- It will convert the number to roman numerals by doing:
- There will be a while loop that will run as long as the inputed integer is not 0.
- In the while loop it will follow a sequence of if/else/if statements which will test to see if the remaining integer value is above a certain point,
- and will then subtract that much from the number, and add the quantity it subtracted to the end of the string.
- This will follow a general sequence of 1000,500,100,50,10,5,1 but will have numbers in between like 900, 400, 90, 40, 9, 4 to satisfy the rule that numbers before subtract. After
- the while loop is done, it will print the final number in roman numerals.
- There will be a second method which will prompt the user for values. It will run convert numbers into roman numerals by calling the previous method over and over again in
- a while loop, but it will tell the user that they can end the while loop by entering an invalid roman numeral (not between 1-3999).
- In the main it will call the 2nd method.
- */
- /*
- /**
- * Created by Daniel on 4/26/2016.
- */
- import java.util.*;
- public class Project1RomanNumerals {
- public static void main(String[] args){
- Scanner c = new Scanner(System.in);
- con(c);
- }
- public static void num(int n){
- String r = "";
- while(n != 0) {
- if (n>= 1000) {
- r += "M";
- n -= 1000;
- }
- else if(n >= 900){
- r+= "CM";
- n-= 900;
- }
- else if (n >= 500) {
- r += "D";
- n -= 500;
- }else if(n >= 400){
- r += "CD";
- n -= 400;
- }
- else if (n >= 100) {
- r += "C";
- n -= 100;
- }
- else if(n>= 90){
- r+= "XC";
- n-= 90;
- }
- else if (n >= 50) {
- r += "L";
- n -= 50;
- }
- else if(n >= 40){
- r += "XL";
- n -= 40;
- }
- else if (n >= 10) {
- r += "X";
- n -= 10;
- }
- else if(n >= 9){
- r += "IX";
- n -=9;
- }
- else if (n >= 5) {
- r += "V";
- n -= 5;
- }
- else if(n>= 4){
- r += "IV";
- n-= 4;
- }
- else if (n >= 1){
- r += "I";
- n -= 1;
- }
- }
- if(n == 0){
- System.out.println("in roman numerals : " + r);
- }
- }
- public static void con(Scanner c){
- int x = -1 ;
- System.out.println("Convert ints to roman numerals!");
- while(x == -1){
- System.out.print("Enter a value between 1-3999 (enter a valid not between 1-3999 to quit) :");
- int n = c.nextInt();
- if(n >3999 || n<1)x=1;
- else num(n);
- }
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment