Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Print numbers from 1 to an given integer(N) in lexicographic order

C Programming Mathematics: Exercise-16 with Solution

Lexicographical order:
From Wikipedia,
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters. This generalization consists primarily in defining a total order on the sequences (often called strings in computer science) of elements of a finite totally ordered set, often called an alphabet.
Write a C programming to print numbers from 1 to an given integer(N) in lexicographic order.

Example:
Input: 10
Output:
Print numbers from 1 to 10 in lexicographic order-
1 10 2 3 4 5 6 7 8 9
Input: 25
Output:
Print numbers from 1 to 25 in lexicographic order-
1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 3 4 5 6 7 8 9

Pictorial Presentation:

C Exercises: Print numbers from 1 to an given integer(N) in lexicographic order

Sample Solution:

C Code:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void print_lexicographic(int n)
{
	int m, j, i = 1; 
	printf("\n\nPrint numbers from 1 to %d in lexicographic order-\n",n);
    while(i<= 9){
        j = 1;
        while( j <= n){
            m = 0;
			while(m < j) {
                if((m + j * i)<= n){
                    printf("%d ", m + j * i);
                }
                m=m+1;          
			  }
            j= j*10;
        }
        i=i+1;
    }
}

int main(void)
{
    print_lexicographic(10);
    print_lexicographic(25);
    print_lexicographic(40);
    print_lexicographic(100);
    return 0;   
}

Sample Output:

Print numbers from 1 to 10 in lexicographic order-
1 10 2 3 4 5 6 7 8 9 

Print numbers from 1 to 25 in lexicographic order-
1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 3 4 5 6 7 8 9 

Print numbers from 1 to 40 in lexicographic order-
1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 5 6 7 8 9 

Print numbers from 1 to 100 in lexicographic order-
1 10 11 12 13 14 15 16 17 18 19 100 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 47 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 61 62 63 64 65 66 67 68 69 7 70 71 72 73 74 75 76 77 78 79 8 80 81 82 83 84 85 86 87 88 89 9 90 91 92 93 94 95 96 97 98 99 

Flowchart:

Flowchart: Print numbers from 1 to an given integer(N) in lexicographic order.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a C programming to get the maximum product from a given integer after breaking the integer into the sum of at least two positive integers.
Next: Write a C programming to find the nth digit of number 1 to n?.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP