#Making addition pattern

46 messages · Page 1 of 1 (latest)

wintry fossil
#

hi, can someone help me with this? i dont know how to make a program for this

gusty bladeBOT
#

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.

knotty island
#

You've learned about for loops already ?

wintry fossil
knotty island
#

Here you just need for loop logic

wintry fossil
#

yeah

#

im confused

knotty island
#

How would you write a for loop which prints from 1 to 3

wintry fossil
knotty island
#

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;    
    }
wintry fossil
#

oh okay

knotty island
#

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

wintry fossil
knotty island
#

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

knotty island
#

So what would you try if you'd need to extend from this on your own ?

wintry fossil
knotty island
#

endline as in a new line ?

wintry fossil
#

yes

knotty island
#

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

wintry fossil
#

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

knotty island
#

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

wintry fossil
#

what-

knotty island
#

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 ?

wintry fossil
#

uh

#

maybe an example?

knotty island
#
#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';
    }
  }
}
jagged sedgeBOT
#
Program Output
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 |
wintry fossil
#

okay I'll try it

wintry fossil
#

it worksss

#

TYSM

knotty island
#

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

topaz streamBOT
#
How to Format Code on Discord

• 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

Example Input

```cpp
int main() {
    return 0;
}
```

Example Output
int main() {
    return 0;
} ```
#
Why Is `using namespace std` Considered Bad Practice?

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;

wintry fossil
wintry fossil
# topaz stream

oo i thought it would've be easier haha, thank you for telling me this

gusty bladeBOT
#

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.