Hi everybody

Anyone knows how to verify the ordinance of numbers but without using vectors!..

example:
n=3456;
the order is ascendent

or

n=9874321
the order is descendent

it seems we need to create a fuction but I´ve no idea how!

plese help...:eek:

Dani AI

Generated

A compact, arithmetic-only way to check digit order (no lists, arrays or "vectors") is to peel off digits with % and // and inspect adjacent pairs. As noted, turning the number into a string is simple; the code below shows a purely integer approach that satisfies @Soul’s constraint.

This single-pass routine reads digits right-to-left (least significant first). When a left digit is smaller than the right one it signals an ascending relationship (left-to-right increase); when it is larger it signals descending. The function returns one of 'ascending', 'descending', 'equal', or 'mixed'. An optional strict flag makes equality between adjacent digits disqualify strict monotonicity.

def digit_order(n, strict=True):
    """
    Return 'ascending', 'descending', 'equal', or 'mixed'.
    Operates with integer arithmetic only (no lists/strings).
    strict=True: equal adjacent digits make the result 'mixed' 
    unless all digits are the same.
    """
    n = abs(int(n))
    if n < 10:
        return 'equal'
    prev = n % 10
    n //= 10
    has_asc = has_desc = equals = False
    while n:
        d = n % 10
        if d < prev:
            has_asc = True
        elif d > prev:
            has_desc = True
        else:
            equals = True
        prev = d
        n //= 10
        if has_asc and has_desc:
            return 'mixed'
    if not has_asc and not has_desc:
        return 'equal'
    if strict and equals:
        return 'mixed'
    return 'ascending' if has_asc else 'descending'

Notes: the function treats negative numbers by absolute value. Leading zeros are not represented in integers. Time is O(number of digits) and extra space is constant. Use strict=False if you want non-decreasing / non-increasing to count as ascending/descending.

Recommended Answers

All 2 Replies

I'm not sure how to do it in C#, but in C or C++ I would format the integer as a string then test each character of the string.

What will you do when the digits are in random order? such as:
n=129438623;

well what I need to do is test if the numbers are incresing or decreasing
just that!

it seems to be easy but i don´t know how to do it not using vectors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.