Triangle from Numbers.

That's not that hard - you really just need a for loop and the / and % operators.

For example, in 54321,
54321 / 10000 will be 5.
54321 % 10000 will be 4321.

4321 / 1000 will be 4.
4321 % 1000 will be 321.

321 / 100 will be 3.
321 % 100 will be 21.

21 / 10 will be 2.
21 % 10 will be 1.

1 / 1 will be 1.
1 % 1 will be 0.

Your approach will have to be slightly different because the order is reversed, but if you think a bit about it I am sure you will find out how it works.
@hanst99:
your tip helped me to solve Uzumaki's question in a nice way :)

@Uzumaki:
maybe this will also help you:
N % 10 will give you the last digit of your number and N / 10 will cut it off, meaning turns 123 into 12.
and i used a do-while-loop besides for, % and /.
Guys I did how to show the digits now I want to implement the code that will show the triangle of these digits. I couldn't find the way to do that. Can you help me on that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
void triangle(int a);
int main()
{
    int v;
    cout<<"Enter your number: ";
    cin>>v;
    triangle(v);
    cout<<'n';
    system("pause");
    return 0;
}
void triangle(int a)
{
    while(a>0){
    cout<<a%10<<endl;
    a=a/10;
}
}


you are almost there.
now you only have to count how often you did a /= 10.
and ptinf a % 10 this often.
Guys it almost worked but there is a subtle error. For example I input number 12345. The program shows :
5
44
333
2222
1
But I want to make the program to show 5 numbers of 1 in the end. Here is my code can you check it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
void triangle(int a);
int main()
{
    int v;
    cout<<"Enter your number: ";
    cin>>v;
    triangle(v);
    cout<<'n';
    system("pause");
    return 0;
}
void triangle(int a)
{
    do{
    for(int row=1; row<=a;row++){
            for(int i=1;i<=row;i++)
    cout<<a%10;
    a=a/10;
    cout<<endl;
}
}while(a>0);
}
I would suggest you to fix your indention, it's currently a bit hard to read your code ;)

There is something a bit weird in your code:
 
row<=a


The problem is, a keeps getting smaller in your code. With a 9 digit number, the last line would never get displayed. The same for numbers with more than 9 digits, and there is also a chance it happens with less digit numbers. Aside from that, it doesn't make too much sense in the first place to do it while rows is smaller than a :O
Last edited on
So how I can change it? Can you write me a block of code for this, I will really appreciate it, Cuz I can't find solution for it. :/
Put a>0 there. You are dividing a by ten each loop, so it will eventually become 0.
So there is already existing these kind of relativity. Here it is:
1
2
3
4
5
6
7
8
9
10
11
void triangle(int a)
{
     while(a>0){
    for(int row=1;row<=a;row++){
            for(int i=1;i<=row;i++)
    cout<<a%10;
    a=a/10;
    cout<<endl;
}
}
}

Still it doesn't work properly. :(
Where did you pull that while loop from? I meant to change the condition in your first for loop to a>0.
Last edited on
Yeah it worked thank you very much. I appreciate it :)))
Topic archived. No new replies allowed.