+ 1
Pascal's Triangle
guys can someone give me the algorithm for the following typa output. 1 2 4 3 6 12 4 8 16 32
24 ответов
+ 1
Frost done
+ 8
That's not how Sololearn works.
We are here to support your work with advice; so first you got to get your own hands dirty! ;)
+ 8
And by the way, this is not the Pascal's triangle. Try to see how the numbers are related. It shouldn't be too hard to find the pattern.
+ 6
So What!! , As said by Honfu and Kishalaya, we do not encourage direct answers to homework-type of questions.
We would greatly appreciate if you could show us your attempts first.
If you aren't understanding the question itself and haven't even attempted yet, Kishalaya's answer contains hints to how this pattern can be generated.
Once you get that you can start attempting the code. If at any point you face any difficulties , you can ask your queries here again.
Hope you understand and cooperate with us.
+ 3
Kishalaya Saha
Lets assune ur not kidding me dood!!!
int n=new Scanner(System.in).nextInt();
For(int i=0;i<n;i++){
System.out.print(n+" ");
n=n*2;
}
dood tank u so much...d thng is am a kinda guy making thngs complex nd i dnt wanna luk into a problem in a easy way....so all at once i've posted tis question...sorry if i annoyed any of u guys...am so sorry...i literally lost my mind nd tis question is such an easy stuff tat no one could make a mess with it but i did...shame on me😣😣
HonFu sry dood..!!!!
+ 3
https://code.sololearn.com/cmz9fhVDZFJ8/?ref=app
See this solution you will get the logic....
by the way, Pascal triangle is looks like...
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
........ ........
Thank you
+ 3
So What!! You have to able to code at any compiler, that's why, how we called a programmer? 😅😅😅😅
+ 2
So What!!, I am not speaking Java, so I am a bit limited in my ability to say anything...
Your for is capitalized, that doesn't work.
Then the line with the Scanner didn't work when I copypasted it, but as I said, I don't know Java and it might be my mistake.
EDIT: Scanner has to be imported, alright, then it works.
You should try the 'Playground' here on Sololearn: You can write your code there and run it directly (or get helpful error messages), and you can link the code to your posts here. Like that, you can make sure that your code works, and it's easier for us too because we can just click and run.
That much said: If i correct that for and substitute the input line with a 4 or any number... it works!
So you're almost there now!
If you look at that original field: Every line starts with a number 1 higher than before. Does the user really have to enter all of them?
Or are you now ready for the next step, creating all the lines only from input 4?
Go for it!
+ 2
I'm not an expert in Java either; I mostly code in Python. In Java, I would have used an auxiliary variable to save the value of n as well. However, some people might find this more elegant:
int n = new Scanner(System.in).nextInt();
for(int i=0; i<n; i++) {
System.out.print(n * (int) Math.pow(2, i) + " ");
}
Anyway, like HonFu said, you should now think about extending it to the original problem. You know how to print each row separately. Now just use another loop to print all of them together! (You have used nested loops before, right?)
Good luck!
+ 1
So What!!, this is not about acting huge - this is about not doing your homework for you. ;-p
+ 1
Then what's wrong with showing us the best you tried?
(I'm quite chilled btw. 😂)
+ 1
Okay, if this is too hard for you, try something simpler: can you write a code that prints just a single row of the triangle? It should take the row number as input. So
Input: 3
Output: 3 6 12
Input: 4
Output: 4 8 16 32
What do you think the pattern is?
+ 1
Oh, that's very good, thanks for sharing your code!
But have you tried running it yet? It has a tiny bug: you're setting the for loop condition as i<n, and doubling n inside the loop. That won't work: i would remain less than n even after we have displayed enough numbers (eventually n overflows the limit of ints in Java, becomes negative, and then the loop stops).
Can you fix it? And please, can you save your code in Code Playground and share a link to that? It's easier to debug that way. Thanks!
+ 1
Kishalaya Saha
Dood hw abt tis....
int n=new Scanner(System.in).nextInt();
int temp;
temp=n;
For(int i=0;i<temp;i++){
System.out.print(n+" ");
n=n*2;
}
If ter is anyother best and elegant way to do...pls let me knw
0
HonFu dood,, dnt take it offensive chill man...i meant tat i didnt post tis question jst right aftr coming across it...i tried my best but in vain nd eventually thot of making it here...anyway chill folk
0
Kishalaya Saha wit tat being said...i was able to find the solution for my very first question...nd i didnt run ur question..bt nw i did nd yes it has flaw nd d loop stopped aftr printing a number of values
0
HonFu
Dood..watevr u said is right..
And the thing is i didnt concern much abt all thoz capital for loop and Scanner stuff and all thoz imports....
I just thought of asking u the heart of program and i thought its ok to give the gist of it rather than writing every bit of line(entire code)...
Its ok dood...ill try posting the complete code from now if it has to be
0
Kishalaya Saha fella..i am completey ok with that program dood,, i mean it ran very well nd tank u fr making me realize the stuffs i need to work on...
0
So What!!, no problem, it's totally reasonable to just give the jist. If we had spoken Java we would have seen the reason more quickly.
I am curious though, have you succeeded creating that pattern?
0
HonFu yeh dood....i did...!!!