+ 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.
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;
}
+ 7
deret = n*(n+1)/2;
+ 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.
+ 2
Simba Thanks! Appreciate it.
+ 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;
}
+ 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;
}