0
Can anyone solve it?
I have a pattern output 1 11 112 1123 11234 112345
11 Respuestas
+ 5
Rajdeep Mazumdar Yes but you need to try first.
+ 3
Brian You missed to add one line before the loop. 🤪
cout << pattern << endl;
+ 2
#include <iostream>
using namespace std;
int main()
{
int i,j,n,k=1;
cout<<"Enter the value of n"<<endl;
cin>>n;
for(i=0;i<=n;i++)
{
cout<<k;
for(j=1;j<=i;j++)
cout<<j;
cout<<endl;
}
return 0;
}
i tried it it's right but i want another way
+ 2
Here is another way. Build the output string and print it incrementally.
#include <iostream>
using namespace std;
int main()
{
int i, n;
cout << "Enter the value of n" << endl;
cin >> n;
string pattern = "1";
cout << pattern << endl; // Edited
for (i = 1; i<=n; i++) {
pattern += to_string(i);
cout << pattern << endl;
}
return 0;
}
+ 2
Good catch, I Am Groot !
Initially I wrote two loops, one to generate the last string with all the numbers, and one to print pattern.substr(0,i) per row (i=1 to pattern.size). Then I discovered a minor problem. When i=10 it printed "...7891" and then "...78910", instead of only "...78910". Combining the loops corrected it, but I had overlooked the initial row.
I added your correction. Thank you.
+ 1
It's not Fibonacci
0
If u observe it carefully it's a Fibonacci triangle starting with 1 . So at the end u have to write the value as 1 instead of 0
0
Atul Panda I agree there is a strong correlation with the Fibonacci sequence. But then the 4 breaks the pattern.
Rajdeep Mazumdar, is 4 really part of the sequence, or is it a typographical error?
0
Brian I know but it's possible but it's bit tricky too. But I really esteem your words because I don't have experience with c++. If it would have been java then I should have solved it and sent it to the required person
0
Coder/Christmas Kitten I like your use of memoization to avoid unnecessary recursion in Fibonacci!