0

Can anyone solve it?

I have a pattern output 1 11 112 1123 11234 112345

24th Dec 2020, 6:30 PM
Rajdeep Mazumdar
Rajdeep Mazumdar - avatar
11 Respuestas
+ 5
Rajdeep Mazumdar Yes but you need to try first.
24th Dec 2020, 6:47 PM
A͢J
A͢J - avatar
+ 3
Brian You missed to add one line before the loop. 🤪 cout << pattern << endl;
25th Dec 2020, 9:43 AM
A͢J
A͢J - avatar
+ 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
24th Dec 2020, 7:31 PM
Rajdeep Mazumdar
Rajdeep Mazumdar - avatar
+ 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; }
24th Dec 2020, 8:57 PM
Brian
Brian - avatar
+ 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.
25th Dec 2020, 10:24 AM
Brian
Brian - avatar
+ 1
It's not Fibonacci
25th Dec 2020, 4:40 PM
Rajdeep Mazumdar
Rajdeep Mazumdar - avatar
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
25th Dec 2020, 2:29 PM
Atul [Inactive]
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?
25th Dec 2020, 4:09 PM
Brian
Brian - avatar
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
25th Dec 2020, 4:20 PM
Atul [Inactive]
0
Coder/Christmas Kitten I like your use of memoization to avoid unnecessary recursion in Fibonacci!
25th Dec 2020, 4:51 PM
Brian
Brian - avatar