Indentation
The indentation of your post is very bad, mainly because it is very inconsistent. It doesn't really matter what indentation style you choose, as long as you consistently follow the style. However I strongly recommend the following things:
- Use spaces around binary operators, after keywords, after comma's.
- Use a single empty line around functions and
if
, case
, do
and while
blocks.
- Prefer
foo += bar
instead of foo = foo + bar
to avoid repeating yourself.
So for example:
int find_out_the_number_of_digit(int number) {
int count = 0;
int value = 1;
// Getting total digits of a given integer number
while (number != 0) {
number /= 10;
count++;
if (count == 1) {
value = 1;
} else {
value *= 10;
}
}
return value;
}
Try to write correctly formatted code at all times, it helps both yourself and others reading your code. If possible, configure your editor to automatically indent code for you. Be aware though that editors are usually a bit dumb and might not always produce correct indentation, in which case you should correct it yourself. You can also run your code through a program that indents the code for you, like indent
or ClangFormat.
Comments
Use comments to clarify things that are not already clear from the code itself. Most of the comments you wrote are superfluous, because they repeat exactly what the code below does, or because it is already clear from the context. Let's look at your comments:
//put variables for further proceed
int number=get_input_validation();
int value=find_out_the_number_of_digit(number);
The sentence is not completely correct English, but it's clear that you mean you are storing information in variables for further processing. But that's just something that is done all the time when programming, it doesn't clarify anything here.
int get_input_validation(){
...
//Taking input from the user
printf("Enter an integer number: ");
scanf("%d",&number);
It is already clear from both the name of the function and the two lines below that you are indeed reading input from the user. So in this case I would say the comment is superfluous. A comment like this might be useful however if you have a longer function that does multiple things, and where the comments then separate the various parts of the function, although in that case it is probably better to split such a function up into several smaller ones, which you already did here.
int find_out_the_number_of_digit(int number){
...
//Getting total digits of a given integer number
Here the comment is just repeating what is already in the name of the function.
//get the last digit
last_digit=number%10;
//get the quotient
number=number/10;
Here each comment basically repeats exactly what the line below does. What would be better is to write a comment that explains what is happening at a higher level:
// Pop the last digit from number
last_digit = number % 10;
number /= 10;
Simplifying the solution
There is a simpler solution that is also more efficient, by avoiding the first pass over all the digits to find the length of the number. The solution could be a single loop that does this:
while (number != 0) {
// Step 1: pop last digit from number
...
// Step 2: push the digit to the back of complete_integer
...
}
Doing this will result in the order of the digits to be reversed. You already implemented popping the last digit from a number, now try to implement the reverse operation of pushing something to the back of a number, without having to know the exact length of that number.
About input validation
I wonder why you don't allow the number 0
to be reversed. It seems that this has a clear answer. You should be able to handle that case without having to write any additional code.
For negative numbers, you could indeed disallow them, or instead define how a negative number will be reversed. For example, you could say that the reverse of the number -123
will be -321
, and then ensure your code implements this case. If the requirements for this project don't say anything about negative numbers, it is up to you.
Naming things
It is good to have descriptive names for functions and variables. One-letter names are usually bad, unless it's for very commonly used things such as i
for a loop iterator, or x
and y
for coordinates. But too long names can be bad as well; they require more typing, and they are distracting. Not only try to be clear, also be concise. For example:
int find_out_the_reverse_order_and_transform_digits_as_a_complete_integer(int number,int value){
...
}
That's a lot of text! Now compare the following:
int reverse_digits(int number) {
...
}
The name of this function, along with the type of the input parameters and return value, are still very descriptive; it tells you that if you give it a number as a parameter, the function will reverse the digits, and returns the result as an interger. The name is much more concise though.
Similarly, even though you shouldn't need this function, find_out_the_number_of_digit()
could be shortened to count_digits()
.
On the other hand, also be aware of using too similar names. For example:
int number=get_input_validation();
int value=find_out_the_number_of_digit(number);
Now you have two variables, number
and value
. Now I have to look at what function was called to find out that value
is actually the number of digits in number
. Here is a suggestion for renaming it:
int input = get_input();
int digits_in_input = count_digits(input);