Write a program that prints all possible combinations of single-digit numbers
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
The question is about writing a C program that produces the outcome above.
There are several ways we can have a program to print all the possible combinations of single-digit numbers. In this tutorial, we will look at how to use:
putchar
printf
Also, for each of them, we can look at multiple ways of achieving the expected outcome. In this tutorial we will take a look at how to use two different types of loops to achieve the result;
For loop
While loop
Using putchar
to print all combinations of single-digit numbers
First we will look at how to do that with a for
loop and afterwards implement the same code using a while
loop.
By default, the putchar
function is used for displaying single characters to the screen, but we need to make it print integers this time round.
What can help us here is the fact that the putchar
function recognizes ASCII codes and is able to print the corresponding integer value of any ASCII code. This means we would also need to know the ASCII codes for the digits (0, 1, 2...9).
ASCII Code | Interger Value |
048 | 0 |
049 | 1 |
050 | 2 |
051 | 3 |
052 | 4 |
053 | 5 |
054 | 6 |
055 | 7 |
056 | 8 |
057 | 9 |
With a for
loop
#include <stdio.h>
/**
* main - main block
* Description: Print all possible combinations of single-digit numbers.
* Numbers must be separated by commas and a space.
* You can only use `putchar` to print to the console.
* You can only use `putchar` up to four times.
* You are not allowed to use any variable of type `char`.
* Return: 0
*/
int main(void)
{
int x;
for (x = 48; x < 58; x++)
{
putchar(x);
if (x < 57)
{
putchar(44);
putchar(32);
}
}
putchar('\n');
return (0);
}
If you don't know the ASCII codes and would still want to use the putchar
function, there is a way around it.
You can forcefully convert an integer to a character inside the putchar
function. This can be done by adding '0'
to the integer.
The above code can therefore be re-written as:
#include <stdio.h>
int main(void)
{
int x;
for (x = 0; x < 10; x++)
{
putchar(x + '0');
if (x < 9)
{
putchar(',');
putchar(' ');
}
}
putchar('\n');
return (0);
}
With a while
loop
Let's use the while
loop to answer the tasks in the same way we did for the for
loop.
- Using the ASCII codes
#include <stdio.h>
int main(void)
{
int x = 48;
while (x < 58)
{
putchar(x);
if (x < 57)
{
putchar(44);
putchar(32);
}
x++;
}
putchar('\n');
return (0);
}
- Converting integers to characters with the
putchar
#include <stdio.h>
int main(void)
{
int x = 0;
while (x < 10)
{
putchar(x + '0');
if (x < 9)
{
putchar(',');
putchar(' ');
}
x++;
}
putchar('\n');
return (0);
}
Using printf
to print all combinations of single-digit numbers
- With a while loop
#include <stdio.h>
int main(void)
{
int x = 0;
while (x < 10)
{
printf("%d", x);
if (x < 9)
{
printf(", ");
}
x++;
}
printf("\n");
return (0);
}
- With a
for loop
#include <stdio.h>
int main(void)
{
int x;
for (x = 0; x < 10; x++)
{
printf("%d", x);
if (x < 9)
{
printf(", ");
}
}
printf("\n");
return (0);
}
The outcome for all the above codes are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Subscribe to my newsletter
Read articles from Dr. Ehoneah Obed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Dr. Ehoneah Obed
Dr. Ehoneah Obed
Heya! One thing I love doing is helping people and I believe one of the key ways I do this is through sharing of my knowledge and personal experiences. I have therefore decided to use this platform to share my knowledge and experience to people who may decide to take the path that I have taken. So, the question is, what is this path? I am professionally trained as a pharmacist but I have been an ardent lover and user of various technologies. During my practice at a tertiary hospital in the Northern Region of Ghana, I discovered a number of pertinent health problems and how technology can be used to solve those problems. In my quest for finding ways to solve some of these health problems with technology, I discovered the field of digital health and how that is transforming the way healthcare is being delivered. I am so much interested in the field and look forward to building a career in digital health and hopefully starting a company (startup) in that field. To make this career transition, I have to pursue further education and I have come across several paths that can lead to that career goal of mine. I have therefore decided to explore almost all the options that I have. For further studies, some of the courses which I came across that can help me achieve that career goal include a masters program or PHD in any of the following: Digital Health, Health technology, Health Informatics, Information Technology, Computer Science and Computational Science. I have therefore started applying to various schools offering any of the programs above. In the time being, I decided to start learning more about emerging technologies and that led me to take a three months course in Amazon Web Services (AWS). I am currently an AWS Certified Cloud Practitioner. During the 3 months training that I undertook at Ghana Tech Lab, Accra, I was introduced to a lot of things which excited me the more. I came to realize the potential of things I could do if I had the right knowledge especially in software development. Prior to this time, I had taught myself a number programming languages, libraries and frameworks for web development. I could consider myself as a fullstack developer with knowledge in HTML, CSS, Javascript, PHP, MySQL, WordPress, React, Laravel, and Codeigniter. However, I practiced less and didn't feel confident enough about my knowledge. I just knew I needed more. More learning, more practice, more projects and more connections with people in the industry. I started taking the Odin project to become a better web developer and a few days into it, I came across the ALX Software Engineering programme for Africans. Once I realized it was free to join, I was going to do everything in my capacity to qualify for it. I discovered the programme about 3 days to the deadline for the May 2022 Cohort. I therefore, sat through that night to finish my application to ALX. Fortunately for me, I got accepted into the program and a new phase of my life has started. I am now a Software Engineering Student looking forward to building a career in digital health. I know that this is just the beginning. As part of this programme, we spend about 70 hours a week learning, practicing, completing projects and building new relationships with our peers. As part of this journey, I am going to use this platform to document what I learn and share my journey to becoming a Software Engineer with you. Therefore, anyone at all looking to transition from their current career to software engineering will find what I post here relevant. Also, if you are a self-taught developer or you are thinking of teaching yourself software development, then you will also enjoy the content I publish here. Throughout my short journey of life, I have realized that we tend to give up on things easily when there is no form of accountability. I am therefore by this platform signing a deal to become accountable to you through sharing with you whatever I learn. Do share your thoughts and comments with me whenever you come across any of my post so I can truly remain accountable to you and never give up on this new found dream of mine.