Triangle Pattern (For Loop)

Feb 21, 2015

I'm trying to make a triangle which should look like this. But I cant seem o figure it out.

1
2 1
4 2 1
8 4 2 1
16 8 4 2 1
32 16 8 4 2 1
64 32 16 8 4 2 1
128 64 32 16 8 4 2 1

This is the code I have written so far.

public class TestProgram
{
public static void main(String[]args)
{
for (int columns=0; columns<=8; columns++)
{
for (int rows=columns; rows>=1; rows --)
{
System.out.print(rows+ " ");
}
System.out.println();
}
}
}

View Replies


ADVERTISEMENT

Print Triangle - Loop Is Not Working

Jul 7, 2014

I am trying to make a program that prints triangle... and I did various test on each method to realise that the problem lies with this segment.When I call this method, nothing prints out, I figure there is something with the loop that I am not realizing.the loop is backwards because it's supposed to have the right side edge parralel (when I try to print it out the spaces do not appear, imagine the x are space...), so as each line is looped the # of spaces diminishes

xxxx*
xxx*x*
xx*xx*
x*xxx*
*****

public class test {
public static void main(String[] args){  
for (int countdown = 5; countdown <= 1; countdown = countdown--){
showNTimes(countdown, ' ');
showNTimes(5- countdown, '*');
System.out.println("");
}
}
public static void showNTimes ( int nbTimes, char carac ) {
for ( int i = 1 ; i <= nbTimes ; i = i + 1 ) {
System.out.print( carac );
}
}
}

View Replies View Related

Nested For Loop - Pattern Printing

Feb 13, 2014

I don't know how to write a program based on for loop like pattern printing.etc

View Replies View Related

Printing Pattern Using Nested Loop

Jan 19, 2015

I built a java code to print the following pattern using nested loop:

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

And here is my code

public static void main(String[] args) {
for(int i=1; i<=8;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(" ");

[Code] .....

which gives the partially true output:

123456781
123456721
123456321
123454321
123454321
123654321
127654321
187654321

I couldn't figure out how to place the spaces.I have made some changes on the code, but it didn't work.

View Replies View Related

Write Nested Loop For Pyramid Pattern

Sep 26, 2014

Write a nested loop for a pyramid pattern. Well here's an ugly description of what it should look like:

1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 128 64 32 16 8 4 2 1

I managed to make the pyramid using addition. However, how to turn it into multiplication x 2.

import java.util.*;

public class NumbersPyramid
{
public static void main(String[] args) {
int odd = 1;
int numSpaces = 7;

[code]....

View Replies View Related

Returns Area Of Triangle / Unless Triangle Not Value So Return String?

Dec 28, 2014

I'm doing a problem where the area of a triangle is returned (if valid). However, I want to return a message (i.e. 'triangle is not valid) if the triangle is invalid.

I'm not sure how to go about to doing this as my method (called area) will only let me return doubles. Possible to return a string in an else within my area method?

public class MyTriangle {
public static void main(String[] args) {
//triangle is valid if the sum of any two sides is bigger than the third
System.out.println(isValid(3, 4, 5));
System.out.println(area(543, 4, 5));

[Code] ...

View Replies View Related

How To Build Triangle Using Loops

Jan 16, 2014

I need to build the triangle like below. How to solve this using loops.

*
* *
* * *
* * * *
* * * * *

View Replies View Related

Properties Of A Triangle - X And Y Coordinates

Oct 29, 2014

A triangle is defined by the x- and y- coordinates of its three corner points. Compute the following the following properties of a given triangle: the lengths of all sides, the angles at all corners, the perimeter and the area. The program must prompt a user for the point coordinates. I have created a class Triangle and a class TriangleSimulator, I am stuck and can't figure out why my program won't run correctly.

import java.util.Scanner;
public class Triangle {
Scanner in = new Scanner(System.in);
private int x1;
private int x2;
private int x3;

[Code] ....

View Replies View Related

Programming About Finding Triangle

Nov 25, 2014

class triangle
{
public static void main (String[] args)
{
System.out.println("Provide three side lengths - 000 to terminate.");
int a = In.getInt();
int b = In.getInt();
int c = In.getInt();
 
[code]....

My problem is that when I enter 5,2,5 it should be isosceles and acute but it comes out as isosceles and obtuse, and when I type 5,5,5 it comes out equilateral and right. The only one that works is if I enter 3,5,4 it will come out as scalene and right. I been at this for a while and my math looks correct.

View Replies View Related

How To Get Main Triangle Program To Run

Oct 17, 2014

I am having trouble getting my main triangle program to run. My teacher gave us a sample program, so I tried running his, and it doesn't run either.

Here is the class:
 
import java.util.Scanner;
 public class ShelbyHarms_3_06_Triangle {
private double sideA, sideB, sideC; // Instance variables, numbers for area and perimeter
static Scanner console = new Scanner(System.in); // Establish keyboard
 
[Code] .....

My errors for the main program(the class has no errors) are:

helbyHarms_3_06.java:37: error: cannot find symbol
theSides.getSides();
^
symbol: variable theSides
location: class ShelbyHarms_3_06

[Code] ....

5 errors

View Replies View Related

Print Triangle Shape Using Numbers?

Jun 1, 2014

i want to print triangle shape using number like

this

1

12

123

1234

12345

123456

this is my code

class shape{
public static void main(String [] args){
for(int x =0 ; x<=6;x++){
for(int y =0 ; x > y ; y++){
System.out.print(x);
}
System.out.print("
");
}
}
}

but my output is

1

22

333

4444

55555

666666

View Replies View Related

Area Of Triangle Java Program?

Sep 2, 2014

I've been having trouble with this code for about a week and I've finally got it down to one error. Here is the code:

import java.util.Scanner;
public class Triangle {
public static void main (String[] args) {
Scanner Console = new Scanner(System.in);
System.out.print("Please enter the three lengths of your Triangle: ");
double a = console.nextDouble();

[Code] ....

And here is the error:

Triangle.java:30: error: class, interface, or enum expected
} // End class
^
1 error

View Replies View Related

How To Create A Dragable Triangle With JavaFX

Nov 6, 2014

I'm trying to make a triangle that plots the point in where the corners show their position in the window and also that the user may drag each corner to a desired position.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.input.MouseButton;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;

[code]....

View Replies View Related

Why Program Not Printing Square And Triangle

Nov 21, 2014

import java.util.Scanner;
public class justin10a

public static void main(String [] args)
{
int n;
n = getSize();
 
[Code] .....

View Replies View Related

JavaFX 2.0 :: Triangle Mesh Rendering

Aug 7, 2014

I'm new to JavaFX and I'm exploring its 3D capabilities, and in particular the TriangleMesh visualisation.
 
I have developed a very basic application, based on the Molecule Sample Application provided in the examples, that read a mesh file generated by an external tool (Gmsh) and displays it. Pretty basic.
 
My problem is that the rendering is not what I expected (see attachement)
  
My guess is that I got something wrong with the texture coordinates or the triangles orientation.

- I don't what to have textures attached to my meshes. These are 3D meshes used for scientific simulation purposes. I thus have set all texCoords to 0.
- I don't control the orientation of my triangles, they are generated by Gmsh. Their orientation is also not interesting for my end users so I would like to display all triangles the same way. I used the meshView.setCullFace(CullFace.NONE); method.

View Replies View Related

Create Instance Of Own Triangle Class And Use One Of Its Methods

Mar 15, 2014

So in the code below I create an instance of my own triangle class and use one of its methods. The thing is I use one of my triangle classes methods in a method other the main method of my main program so I'm thinking it can't access it?

Any way here's the code for my triangle class
 
import java.util.Scanner;
public class QudratullahMommandi_Triangle_06 {
Scanner keyboard = new Scanner(System.in);
private double side1;
private double side2;
private double side3;

[Code] ....

and here's the error message

QudratullahMommandi_S_06.java:46: error: cannot find symbol
{ triangle1.outPut();
^
symbol: variable triangle1
location: class QudratullahMommandi_S_06
1 error

View Replies View Related

Creating Program For Area And Perimeter Of Triangle

Sep 1, 2014

import java.lang.Math;
public class triangle {
double area; double s;
public double findArea(double sideA, double sideB, double sideC);
{
s= 0.5 *(sideA + sideB + sideC);
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));

[Code] ...

Errors I am getting:

Triangle.java:23: error: class, interface, or enum expected
import java.util.Scanner
^
Triangle.java:25: error: class, interface, or enum expected
public static void main(String[] args) {

[Code] ....

View Replies View Related

How To Create Pascal Triangle Code In Java

Oct 18, 2014

How to create pascal triangle codes?

View Replies View Related

Error In Program To Display Floyd Triangle

Aug 9, 2014

I want to display triangle with for or if loop in java prog as:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
.
.
.
79. . . . . . . . . .91

here is code and error file

eror of for: E:Javajdkbin>javac tri.java tri.java:16: error: reached end of file while parsing } ^ 1 error code of above error:

Java Code:

class tri
{
public static void main (String args[]) {
int i,j,a=0; for(i=1;i<=79;i++) {
for(j=1;j<=i;j++) {
a++;
System.out.print(" ");
System.out.print(a);
}
System.out.print("
");
}
} mh_sh_highlight_all('java');

View Replies View Related

How To Calculate Area Of Triangle / Circle / Rectangle

Mar 3, 2015

package areatest;
import javax.swing.JOptionPane;
public class AreaTest {
 
public static double areaTriangle (double length, double width){ //How to calculate the area of a triangle
return .5f * length * width;

[Code] .....

When I try to get the area of a rectangle it gives me 9 no matter what input I give it. When I try to get the area of a triangle it gives me .5 no matter what input I give it. Same with the circle but it always gives me 12.56370...

View Replies View Related

How To Add Spaces To A Pattern

Apr 21, 2015

I have created this program that outputs

1
12
123
1234
12345
123456

I tried looking up some ways to create spaces in between each number, but I failed miserably. Here is what I have right now.

public class Iterations {
public static void main(String[] args){
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j);

[Code] ....

View Replies View Related

Printing Pattern When Name Is Given

Mar 10, 2014

I want to know how to print this type of pattern when a name is given ???

Example:

name given TEJA

Capture.PNG

View Replies View Related

Print Isosceles Triangle Depending On Variables Height

Nov 14, 2014

The program needs to print an isosceles triangle depending on variables Height, BorderSymbol and Interior symbol, how to do that. Here is what i did so far :

{
int height = 5;
int i = 0;
int count = 0;
String line = "";
 
[code]....

and the output is a blanc page nothing on the screen only spaces...

View Replies View Related

Coordinates Are Inside Of A Right Triangle On Imaginary Coordinate Plane

May 24, 2014

I'm having a random x and y coordinate generate and I need to check if the coordinates are inside of a right triangle on this imaginary coordinate plane.

View Replies View Related

Array Is A Set Of Numbers In Triangle - Find Maximum Path

Aug 12, 2014

If you aren't familiar with the euler problem 67, you are given an array that is a set of numbers in a triangle. Like this

3
7 4
2 4 6
8 5 9 3

and you have to find the maximum path, which for this one is

(3)
(7) 4
2 (4) 6
8 5 (9) 3

I have solved this problem iteratively with the code below

depth = depth-2;
while (depth >=0) {
for (int j = 0; j <= depth; j++) {
values[depth][j] += Math.max(values[depth+1][j], values[depth+1][j+1]);
}
depth -= 1;
}

depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have

public static int findMax(int[][] array,int depth) {
if (depth==0)
return array[0][0];
else if
}

View Replies View Related

Fractal Triangle - Recursion To Make Children Triangles

Oct 7, 2014

I have a FractalGUI and a FractalTriangle class. The FractalTriangle class is what does a lot of the work, it uses ATriangle(s) to make a triangle, and it uses recursion to draw three children triangles along each parent triangle. Fractal Triangle has a method "makeTriangle" private ATriangle makeTriangle( Point p0, Point p1 ) which takes the ATriangles and sets them the right size and everything, but for some reason I can only get one level of children triangles, and they are also already their upon running which they shouldn't be, they should appear when the depth is 2. When the depth is three it should add three triangles each onto the three added at depth two. At depth 4 it should add three each onto the three previously made triangles, and so on until depth reaches 6, the max depth. Here is some code of my FractalTriangle.

The Atriangle class given to us ("starter code") does not extend or implement anything, and has basic methods (setLocation, setSize, setThickness, setColor, etc. )

//---------------- class variables ------------------------------
//---- recursive generation parameters
public static double sizeRatio = 0.5; // integer represent %
public static double offset = 0.5; // offset/100 = parametric value
// child positioning offset
public static double p2projection = 0.5;
// parametric value of projection of
// vertex p2 onto the base; can be < 0
public static boolean outside = true;

[code]....

the way it looks without the recursion looks much more right, but I am supposed to have recursion in that constructor but I can't figure out how to get it right with the recursion. It's so weird how with the recursion the big red triangle ( base triangle ) disappears, and you can see the children are really really small.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved