Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.9 kB
3
Indexable
Never
#include <iostream>
#include <cmath>
using namespace std;

long long reverse_number(long long n);

int main()
{
    int side_length;
    cout << "Enter Number: ";
    cin >> side_length;
    
    long long ascending_number = 0;
    long long descending_number = 0;
    for (int i = 1; i <= side_length; i++)
    {
        ascending_number += i;
        ascending_number = (ascending_number * 10);
    }
    ascending_number = (ascending_number / 10);
    descending_number = reverse_number(ascending_number);
    
    // First line
    cout << (long long) (ascending_number/10);
    cout << descending_number << "\n";
    
    int spaces = 0;
    int controlled_spaces = 0;
    for (int i = 0; i < (side_length * 2) - 3; i++)
    {
        int first_number = (long long) (ascending_number / pow(10, controlled_spaces+1));
        cout << first_number;
        for (int i = 0; i < ((controlled_spaces*2) + 1); i++)
        {
            cout << " ";
        }
        long long second_number = reverse_number(first_number);
        cout << second_number;
        
        cout << "\n";
        spaces++;
        int most_spaces = side_length - 2;
        if (spaces > most_spaces)
        {
            controlled_spaces = (2 * most_spaces) - spaces;
        }
        else
        {
            controlled_spaces = spaces;
        }
    }
    
    // Last line 
    cout << (long long) (ascending_number/10);
    cout << descending_number << "\n";
    
    
}

long long reverse_number(long long n)
{
    int numbers_added = 0;
    int arr[10];
    while (n >= 10)
    {
        arr[numbers_added] = (n % 10);
        n = n / 10;
        numbers_added++;
    }
    arr[numbers_added] = n;
    numbers_added++;
    
    long reversed = 0;
    
    for (int i = 0; i < numbers_added; i++)
    {
        reversed += arr[i];
        reversed = reversed * 10;
    }
    reversed = reversed / 10;
    return reversed;
}