Welcome to Dream.In.Code
Become a Java Expert!

Join 150,066 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,780 people online right now. Registration is fast and FREE... Join Now!




Pascals Triangle

2 Pages V  1 2 >  
Reply to this topicStart new topic

Pascals Triangle, Coding

jdjprabhu
12 Jun, 2008 - 09:54 PM
Post #1

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 3

CODE

class sal
{
    public static void main(String [] args)
    {
        int a,b,c,n=1;
        for(a=5;a>=1;a--)
        {
            for (c=1;c<a;c++)
            System.out.print(" ");
            for (b=1;b<=n;b++)
            System.out.print(n);
            n=n+2;
            System.out.println();
        }
    }
}

I am getting the answer as below
1
333
55555
7777777
999999999
like a triangle.

But I am not able to get the answer as below
1
222
33333
4444444
lik a triangle shape
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Pascals Triangle
12 Jun, 2008 - 10:27 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,161



Thanked: 44 times
Dream Kudos: 125
My Contributions
I wrote this code on the fly [don't have java IDE to see it running.] so please check for syntax errors. But I think it should work for you.

java

int a,b,c,d,n=1;
b= (5/2)+1; //as you are doing this for 5 numbers. but try to keep that 5 as macro or some constant.
d=1;
for(a=5;a>=1;a--,b--)
{
for (c=0;c<=b;c++)
System.out.print(" ");
for (c=1;c<=d;c++)
System.out.print(n);
n++;
d+=2;
System.out.println("");
}



I hope this will help you. smile.gif
User is online!Profile CardPM
+Quote Post

neotrumatrix
RE: Pascals Triangle
12 Jun, 2008 - 10:49 PM
Post #3

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
QUOTE(jdjprabhu @ 12 Jun, 2008 - 10:54 PM) *

CODE


            System.out.print(n);
            n=n+2;
            System.out.println();



Simple mistake just should have checked again change the above code to this
CODE
System.out.print(n);
n++;
System.out.println();

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Pascals Triangle
12 Jun, 2008 - 10:55 PM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,161



Thanked: 44 times
Dream Kudos: 125
My Contributions
QUOTE(neotrumatrix @ 13 Jun, 2008 - 12:19 PM) *

QUOTE(jdjprabhu @ 12 Jun, 2008 - 10:54 PM) *

CODE


            System.out.print(n);
            n=n+2;
            System.out.println();



Simple mistake just should have checked again change the above code to this
CODE
System.out.print(n);
n++;
System.out.println();



won't work. as it's second for loop is dependent on n variable, it won't come up with proper triangle.
User is online!Profile CardPM
+Quote Post

gl3thr0
RE: Pascals Triangle
12 Jun, 2008 - 10:55 PM
Post #5

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
your problem is in this line here
for (b=1;b<=n;b++)

your using the variable tht you print to control the loop, which is a problem because if you want to change the value thats printed then you have to change the whole loop structure.

my advice would be to simply create another variable that is exactly the same as what n is now and use it to control your loop. that way your free to change n

This post has been edited by gl3thr0: 12 Jun, 2008 - 11:01 PM
User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Pascals Triangle
12 Jun, 2008 - 11:06 PM
Post #6

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
QUOTE(AmitTheInfinity @ 12 Jun, 2008 - 11:55 PM) *

QUOTE(neotrumatrix @ 13 Jun, 2008 - 12:19 PM) *

QUOTE(jdjprabhu @ 12 Jun, 2008 - 10:54 PM) *

CODE


            System.out.print(n);
            n=n+2;
            System.out.println();



Simple mistake just should have checked again change the above code to this
CODE
System.out.print(n);
n++;
System.out.println();



won't work. as it's second for loop is dependent on n variable, it won't come up with proper triangle.

The Second for loop is dependant on n variable but the n is changing after the looping !!!!! After the second for loop runs n value is changed ....

Just run the file and check out for yourself smile.gif

P.S Except when a = 10 the code works well .... Reason being obvious as 10 has 2 digits the triangle isnt proper !

This post has been edited by neotrumatrix: 12 Jun, 2008 - 11:07 PM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Pascals Triangle
12 Jun, 2008 - 11:30 PM
Post #7

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,161



Thanked: 44 times
Dream Kudos: 125
My Contributions
checked it sir!!!

and this was your modification's output
CODE

    1
   22
  333
4444
55555

User is online!Profile CardPM
+Quote Post

mensahero
RE: Pascals Triangle
12 Jun, 2008 - 11:35 PM
Post #8

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
hello guys.. just a question.. isn't pascal triangle looks like this..

CODE

                  1
                121
                  12321
                    1234321
              123454321


correct me if i'm wrong.. blink.gif I know it's not a triangle.. but believe me it's hard to allign.. lmao.. blink.gif

Hmm.. that made me think or maybe like this..

CODE


nah probably the first one.. lmao.. :blink:  
      1
    232
  34543
4567654


This post has been edited by mensahero: 12 Jun, 2008 - 11:39 PM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Pascals Triangle
12 Jun, 2008 - 11:49 PM
Post #9

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,161



Thanked: 44 times
Dream Kudos: 125
My Contributions
You are absolutely right mensahero the pascal's triagle is way different than the one he shown here. But I just ignored what name he is using for his triangle as he is SHOWING what he wants. so I was ok even if he could have written prabhu's triangle... lol. biggrin.gif
User is online!Profile CardPM
+Quote Post

jdjprabhu
RE: Pascals Triangle
12 Jun, 2008 - 11:59 PM
Post #10

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 3

Yes It was Useful.

And when I tried to print as below
1
22
333
4444
55555

like a triangle I am not able to get.Please help me.
CODE

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

User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Pascals Triangle
13 Jun, 2008 - 12:04 AM
Post #11

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
QUOTE(AmitTheInfinity @ 13 Jun, 2008 - 12:30 AM) *

checked it sir!!!

and this was your modification's output
CODE

    1
   22
  333
4444
55555



Suprising ... I got a proper triangle when i ran the code !!

And yes that is not the pascal's triangle just went along with him tongue.gif
CODE

                                                 1
                                             1     1
                                          1     2     1
                                       1     3     3     1
                                    1     4     6     4     1
                                 1     5    10    10     5     1
                              1     6    15    20    15     6     1
                           1     7    21    35    35    21     7     1
                        1     8    28    56    70    56    28     8     1
                     1     9    36    84    126   126   84    36     9     1
                  1    10    45    120   210   252   210   120   45    10     1

This being a 10 row pascal triangle to be exact smile.gif

User is offlineProfile CardPM
+Quote Post

mensahero
RE: Pascals Triangle
13 Jun, 2008 - 12:07 AM
Post #12

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
Hmm.. you've discover prabhu triangle.. your a genius... lmao.. blink.gif .. that's way better than pascal..
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month