How to Write a Program That Prints All the Numbers of Base 16 in Lowercase in C

How to Write a Program That Prints All the Numbers of Base 16 in Lowercase in C

A Step-by-Step Guide

Introduction

Numbers of Base 16 are simply numbers in Hexadecimal, where these numbers range from 0 to 15, but then to differentiate these numbers from the numbers of base 10 (decimal/denary), from 0 to 9 are the normal numerical numbers, while from 10 to 15, they are been represented by normal alphabets starting from A, down to F. This alphabet can either be in uppercase (A, B, C, D, E and F) or lowercase (a, b, c, d, e, f).

The table below shows the entire number of base 16.

Base 10Base 16 (uppercase)Base 16 (lowercase)
000
111
222
333
444
555
666
777
888
999
10Aa
11Bb
12Cc
13Dd
14Ee
15Ff

These are all the numbers of base 16.

Printing Numbers of Base 16 in Lowercase

To pull this off, we will need to make use of our loop, it could be our for loop, while loop or do while loop together with our control statement, which could either be an if-else or a switch statement.

And we could also just ignore all these and used just our loop statement together with the printf function to save ourselves the stress of having to use the control statements.

To use the printf function, we will be using the hexadecimal formatter, which is the %x for lowercase and the %X for uppercase.

So we will loop through numbers from 0 to 15 and print them to the standard output using the printf with the base 16 formatter.

I will still be using the Betty style of coding in C programming.

#include <stdio.h>
/**
  *main - prints numbers of base 16 in lowercase
  *Return: 0
*/
int main(void)
{
    int num;

    for (num = 0; num <= 15; num++)
    {
        printf("%x", num);
    }
    printf("\n");
    return (0);
}

As you can see from the image above, it printed the numbers of base 16 in lowercase, to do the same in uppercase, simply use an uppercase x instead of a lowercase x for the base 16 formatter in the printf function.

Conclusion

You can also use different methods for this, but this is the easiest and straightforward method you can find.

Thank you. You can connect with me on Twitter and LinkedIn.