+ 1

[SOLVED] I Need Help With This C++ Problem

I want to make Pyramid with this pattern, so it will look like this: 1 1 3 1 3 6 1 3 6 10 1 3 6 10 15 And so on... This is my code so far: #include <iostream> Using namespace std; Int main(){ Int deret; For (int i=1; i<=5; i++){ For (int n=1; n<=i ; n++){ deret = i*(i+1)/2; Cout<<deret; } Cout << endl; } return 0; } PS: Happy New Year, Thanks for the help.

1st Jan 2022, 7:37 AM
Jevon Harry
Jevon Harry - avatar
6 Answers
+ 8
Shouldn't there be number 10 at the 4th row? if so, this might work #include <iostream> int main() { int deret {5}; for (int i {1}; i <= deret; i++) { for (int j {0}, n {1}; n <= i ; n++) { std::cout << (j += n) << ' '; } std::cout << '\n'; } return 0; }
1st Jan 2022, 9:45 AM
Ipang
+ 7
deret = n*(n+1)/2;
1st Jan 2022, 7:52 AM
Simba
Simba - avatar
+ 2
Ipang Yes, There's should be number 10 at the 4th row. I forgot to put it and I'm gonna put it so everyone can comprehend your code and the problem set. Thanks for the answer.
1st Jan 2022, 4:55 PM
Jevon Harry
Jevon Harry - avatar
+ 2
Simba Thanks! Appreciate it.
1st Jan 2022, 4:55 PM
Jevon Harry
Jevon Harry - avatar
+ 2
#include <iostream> int main() { int deret {5}; for (int i {1}; i <= deret; i++) { for (int j {0}, n {1}; n <= i ; n++) { std::cout << (j += n) << ' '; } std::cout << '\n'; } return 0; }
3rd Jan 2022, 1:05 AM
Habeeb Abdulwaheed
Habeeb Abdulwaheed - avatar
+ 1
Try this one , its working very simple logic #include <iostream> using namespace std; int main() { int n; cin >> n; int val = 0; for (int i = 1; i <= n; i++) { val = 0; for (int j = 1; j <= i; j++) { val = val + j; cout << val << " "; } cout << endl; } return 0; }
3rd Jan 2022, 4:54 AM
Ashutosh Patel
Ashutosh Patel - avatar