# Transformed and stored reversed order digits of an integer to a complete integer number

Input:

Enter an integer number: 123456 [Any number of digits are allowed]


Output:

Completed integer number : 654321


I have solved this problem by following this strategy:

#include <stdio.h>
int main(void){
//put variables for further proceed
int number=get_input_validation();
int value=find_out_the_number_of_digit(number);
find_out_the_reverse_order_and_transform_digits_as_a_complete_integer(number,value);
}
int get_input_validation(){

int number=0;

while(1){
//Taking input from the user
printf("Enter an integer number: ");
scanf("%d",&number);
if(number<=0){
printf("Zero and negative numbers are not allowed here\n");
}
else{
break;
}

}
return number;
}
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=number/10;
count++;
if(count==1){
value=1;
}
else{
value=value*10;
}

}
return value;
}

int find_out_the_reverse_order_and_transform_digits_as_a_complete_integer(int number,int value){
int complete_integer=0;

while(number!=0){
int last_digit=0;

//get the last digit
last_digit=number%10;

//get the quotient
number=number/10;

complete_integer=complete_integer+last_digit*value;

value=value/10;

}

printf("Completed integer number : %d",complete_integer);
}


I am here for: What about my indentation and comments? Will I face any problem for some conditions? How can I simplify my solution by reducing too much line of codes in an efficient way?

• "What about my indentation" - What indentation? I almost don't see any. Sep 28 '20 at 13:47
• Thank you for your response. How can I improve on this segment? Sep 28 '20 at 15:04

# 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.

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.

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);

• "Run your code through a program that indents the code for you" I disagree. Adopt a consistent coding style and minimum of discipline when writing code, don't rely on some tool to do that for you. Discipline is perhaps the most single-most important programmer trait of all, more so than brains and tech skills even. Sep 29 '20 at 7:37
• I agree with you. Please give me some tips about how can I improve my coding format and styles as well. Sep 29 '20 at 9:23
• Sir, I checked and I learned a lot from your information. Thank you, sir. May allah bless you. Keep up the good work. Oct 2 '20 at 10:01
• Thank you, @AshifulIslamPrince, and I wish you well in your future coding projects! Consider marking the answer that was most helpful to you as the accepted answer. Note that you can always change the accepted answer if a better answer is added later. Oct 2 '20 at 21:25

You say "Any number of digits are allowed" but then try to read into an int, which can hold only very few digits. So at that point you already lost. Also, even if the entered number fits into an int, the reverse might not:

Enter an integer number: 1000000003
Completed integer number : -1294967295


Better use a string instead of an int.

• That's a good point and very helpful. Thank you. Sep 28 '20 at 15:01

How can I simplify my solution by reducing too much line of codes in an efficient way?

Simplify and drop value. Not needed and as used may overflow. Perhaps use a wider type to reverse int values like 1000000009.

 long long complete_integer = 0;
while(number) {
complete_integer = complete_integer*10 + number%10;
number=number/10;
}