Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Code to take a decimal number as input and prints its Hexadecimal equivalent, the code must have recursive function and no arrays in C language ??

I just wrote the following code, but sure about nothing in this, just tried to write something.

You can tell a new code instead of modifying this one.

What I have tried:

C++
#include<stdio.h>

int convert(int n){
	int x;
	if(n==0) {
		printf("0");
		return;
	}

	else{
		x = convert(n/16)%16;
		if(x>9){
			x = x+55;
			printf("%c", x);
		}
		else{
			printf("%d", x);
		}

		return n;
	}
}



int main(void){
	int n;
	scanf("%d", &n);
	convert(n);

}</stdio.h>
Posted
Updated 8-Feb-16 10:51am
v4
Comments
Patrice T 8-Feb-16 16:15pm    
What is the problem with this code ?
What is going wrong when you run it ?
Dishank Bansal 8-Feb-16 16:53pm    
It's simply not giving the correct answer.
Suppose for input 15, it must give output F, but is giving 00.

There is no such thing as conversion between decimal and hexadecimal. This is because a number cannot be decimal or hexadecimal, it is "binary"; more exactly, calculation is agnostic of the internal representation of the number, except for some special cases.

So, "conversion" function profile cannot be int convert(int n). Note that you don't return anything at all, which is a plain bug. If you call some function "conversion", it should not print anything, but should only return some value. But you have no "another" integer value to return. All numeric values, such as int and other ones, represent mathematical value, which represent the value, amount, not the way of showing this value in a string.

The only relevant "conversion" would be a "conversion" to string. One of the way to do so is to create a function expecting a number and returning a string. But with C-style strings, you should not return such string (because it would have a side effect, allocation of memory, which is bad). Therefore, you should fill in some memory provided by the called. Such functions already exist in the standard library; this is, in particular, sprintf or snprintf:
sprintf — C++ Reference[^],
snprintf — C++ Reference[^].

All your "hexadecimal vs decimal" stuff is in the format specifiers which define how the data is "printed": printf — C++ Reference[^].
See also this tutorial: C Tutorial: printf, Format Specifiers, Format Conversions and Formatted Output | CodingUnit Programming Tutorials[^].

—SA
 
Share this answer
 
v2
Comments
CPallini 8-Feb-16 16:35pm    
5.
Sergey Alexandrovich Kryukov 8-Feb-16 16:36pm    
Thank you, Carlo.
—SA
Sergey already showed you the right way of thinking about it, that is the hexadecimal representation of a number. He also suggested you the proper way to handle it in C.
Now, let me try to go a bit into the actual implementation:

C
  #include <stdio.h>
  #include <string.h>
  #include <assert.h>

void step( int k, int l, char * p)
{
  if ( l )
  {
    *p = (k & 0xF);
    *p += *p < 0xA ? '0' : ('A' - 0xA);
    step(k>>=4, --l, --p);
  }
}

void hexstr(int n, char buf[], int bufsize)
{
  int len = 2 * sizeof(n);
  assert(buf && bufsize > len);
  buf[len] = '\0';
  step(n, len, &buf[len-1]);
}


int main()
{
  int n;
  char buf[2*sizeof(n)+1];
  printf("please enter a number\n");
  if ( scanf("%d", &n) == 1)
  {
    hexstr(n, buf, sizeof(buf));
    printf("the hexadecimal representation of %d is %s\n", n, buf);
  }
  return 0;
}



[update]
This one recursively prints the hexadecimal representation of the number without using arrays.

C++
#include <stdio.h>
#include <string.h>
#include <assert.h>


void hexstep( unsigned int k, int l)
{
  if ( l )
  {
    hexstep (k >> 4, --l);
  }

  char c = (char) (k & 0xF);
  printf("%c", c < 0xA  ? c + '0' : c + 'A' - 0xA);
}

int main()
{
  unsigned int n;
  printf("please enter a number\n");
  if ( scanf("%u", &n) == 1)
  {
    printf("the hexadecimal representation of %u is ", n);
    hexstep(n, sizeof(n)*2-1);
    printf("\n");
  }
  return 0;
}

[/update]
 
Share this answer
 
v3
Comments
nv3 8-Feb-16 18:10pm    
Nice, although strictly speaking you did not conform to the request of not using arrays -- which will be easy enough to change.
CPallini 9-Feb-16 2:37am    
Oooooooooooooops :-)
Dishank Bansal 9-Feb-16 2:10am    
yeah.
I have a problem in which they mentioned to not use arrays.
CPallini 9-Feb-16 3:03am    
See my updated solution.
Dishank Bansal 9-Feb-16 17:24pm    
Thank You very much for the solution.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[

Pay attention to the value returned by convert.
 
Share this answer
 
v2
Comments
Dishank Bansal 9-Feb-16 2:08am    
I know how to use debugger.
But here i'm unable to make the algorithm.
this one is just a random try.
jeron1 9-Feb-16 10:26am    
A 'random' try, really? At least map it out and go through it on paper, if you can't do it there, then you certainly can't write code for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900