Need help with a number diagram

I am having problems with diagram 2 and diagram 3. Can someone please help
Diagram 1 12345
1234
123 This has left alightment
12
1

Diagram 2 1 This should be centered
12
123
The actual question can be seen at http://venus.cs.qc.cuny.edu/~ctse/cs111/slides/hw7.pdf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include<cmath>
using namespace std;

void number(int number){
        for(int b=1; b<=number; b++){
                 cout << b;
        }
        return;
}
int Fibonacci(int n){
        if(n<=0) return 0;
        else if(n==1) return 1;
        else {
                return Fibonacci(n-1)+Fibonacci(n-2);
        }
        }
int isPrime(int x) {
        int trial=2;
        while (trial<=x){
                if(x%trial==0) return trial;
        else trial++;
        }
        return x;
}
void spaces(int n){
        for(int d=n; d>0; d--){
        cout << n;
        }
        return;
}
int main (){
        cout << "Please enter a  number: ";
        int n;
        cin >> n;
//Fibonacci number
        cout << "Fibonacci number of " << n << " is ";
        for(int i=1; i<=n; ++i){
                cout << Fibonacci(i);
        }
        cout << endl;

//isPrime
        int num=n;
        if ((n=isPrime(num))==num)
        cout << num << " is a prime number" << endl;
        else cout << num << " is not a prime number" << endl;

//Sequential Order
        int c=num;
        number(c);
        cout << endl;

//n copies of n
        int e=num;
        spaces(e);
        cout<< endl;

// Fibonacci and Prime
        for(int f=1; f<=6; f++){
                cout << isPrime(Fibonacci(f));
        }
        cout << endl << endl;

//Diagram 1
        cout << "Diagram 1" << endl;
        for(int r=1; r<=e; r++){
                number(r);
                cout << endl;
        }
//Diagram 2
        cout << endl << "Diagram 2" << endl;
        for (int r=1; r<e; r++){
                        cout << " ";
                for(c=e; c>=r; c--){
                number(c);
                cout << endl;
        }
                cout << endl;
        }
//Diagram 3
        cout << endl << "Diagram 3" << endl;
        for(int r=1; r<=e; r+=2){
                number(r);
                cout << endl;
        }
//Diagram 4
        cout << endl;
        cout << "Diagram 4" << endl;
        for(int r=e; r>0; r--){
                number(r);
                cout << endl;
        }
        cout << endl;
Your spaces function is wrong, it should cout << ' '; and not cout << n;.

As for diagram 2, think about it. The first row has 0 spaces, then n numbers. The second row has 1 space, then n - 1 numbers. The third row has 2 spaces, then n - 2 numbers. Etc.

For diagram 3, the last row has 0 spaces. The second to last row has 1 space. The third to last row has 2 spaces. Etc.
Topic archived. No new replies allowed.