#Making addition pattern
46 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
You've learned about for loops already ?
using if else and for?
Here you just need for loop logic
How would you write a for loop which prints from 1 to 3
cout<<"Enter the value of n: ";
cin>>x;
for(i=0;i<x;++i)
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}```
1 thing directly, don't declare i outside of the loop if you don't need it outside of the loop.
So it would look like this instead
int n2=1,n1=0,n3,x;
cout<<"Enter the value of n: ";
cin>>x;
for(int i=0;i<x;++i)
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
oh okay
what should i do next?
In either case, i had the hope that you'd just write it as
int main()
{
int n = 3; // repalce with user input
for(int i = 1; i < n + 1; i++)
{
std::cout << i;
}
}
So that we could extend from here
This alone will just print from 1 to 3 till we'd take user input to change that dynamically
ah yes i put too many unecessary variables
Just to make things clear, i do n + 1 so that we can start with an index of 1, if we'd start with a index of 0 we would need to do this + 1 logic in the loop body itself which can get messy pretty quickly
hmm true
So what would you try if you'd need to extend from this on your own ?
how do i state an endline in a loop?
endline as in a new line ?
yes
Either << std::endl or << '\n'.
Usually you should prefer \n over std::endl as std::endl is doing additional stuff which isn't always needed
ah okay thanks
using namespace std;
int main()
{
int n,a,b,c;
cout << "Enter the value of n: ";
cin >> n;
for(int i = 1; i < n + 1; i++)
{
cout << i << " + " << i << " = " << i+i << "\n";
}
}```
so i made it like this
but it only shows the same numbers additions
1 + 1 = 2
2 + 2 = 4
Yeah, the problem here is that you try to do it with a single loop.
But here's the trick. You can just have a loop in a loop
what-
Generally i'd like to avoid showing the solution directly, but i do know that it can help to understand.
So, would you like to see a solution or rather not ?
#include <iostream>
int main()
{
for(int outer = 0; outer < 5; outer++)
{
for(int inner = 0; inner < 5; inner++)
{
std::cout << "outer: " << outer << " | " << "inner: " << inner << '\n';
}
}
}
;compile
outer: 0 | inner: 0
outer: 0 | inner: 1
outer: 0 | inner: 2
outer: 0 | inner: 3
outer: 0 | inner: 4
outer: 1 | inner: 0
outer: 1 | inner: 1
outer: 1 | inner: 2
outer: 1 | inner: 3
outer: 1 | inner: 4
outer: 2 | inner: 0
outer: 2 | inner: 1
outer: 2 |
okay I'll try it
#include <iostream>
using namespace std;
int main()
{
int n,a,b;
cout << "Enter the value of n: ";
cin >> n;
for(int a=1; a<=n; a++)
{
for(b=1; b<=n; b++)
{
cout <<a<<" + "<< b << " = "<< a+b<< "\n";
}
}
return 0;
}
it worksss
TYSM
Looks good, also a smart move to use <= here xD
That would've been my version
#include <iostream>
int main()
{
int n;
std::cout << "Enter the value of n: ";
std::cin >> n;
n += 1; // Just here to remove the + 1 logic in the loop
for(int outer = 1; outer < n; outer++)
{
for(int inner = 1; inner < n; inner++)
{
std::cout << outer << " + " << inner << " = " << outer + inner << '\n';
}
}
}
There are 3 things you should improve tho.
int n,a,b;
a and b should not be there, it's only needed in the loop itself so better keep it in the loop scope.
You try to give your variable meaningful names. It might be easy to understand now as you wrote it, but in a few days, weeks, months.. it might make no sense at all
You should get rid of the using namespace std; habit as it's very error prone.
I'll send you 2 resources through the bot
• type three "backticks" (not quotes/apostrophes, on QWERTY layout, left of 1-key)
• on the same line, type the file extension for that language (c or cpp)
• enter your code on a new line and put another three backticks at the end
```cpp
int main() {
return 0;
}
```
int main() {
return 0;
} ```
using namespace std will import all of the symbols from std into the enclosing namespace. This can easily lead to name collisions, as the standard library is filled with common names: get, count, map, array, etc.
A key concern with using namespace std; is not what is imported now but rather what may suddenly be imported in the future.
While using namespace std; is alright for tiny projects, it is important to move away from it as soon as possible. Consider less intrusive options, if you insist on not using scope resolution: cpp // OK: *only* import std::vector using std::vector; // OK: namespace alias namespace chr = std::chrono; chr::duration x;
oh yes i forgot the 1st point sorry
oo i thought it would've be easier haha, thank you for telling me this
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.