Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my output:
clang-7 -pthread -lm -o main main.c
 ./main

Enter a number:
123456789
Enter the nth position:
5
1987654321 \\I reversed the input number so i can store num as inputed in array when i use %
The no. in 5 position is: 1

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>

int length (long int num)
{
  int len=0;
  while(num>0)
  {
    num = num/10;
    len++;
  }
  return len;
}
long int reverse(long int num)
{
  int len = length(num);
  int i,rev;
  long int str = 10,mlt;
  while(i<len)
  {
    rev = num%10;
    num = num/10;
    str = (str+rev)*10;
    i++;
  }
  long int num1 = str/10;
  printf("%ld\n", num1);
    return num1;
}
int position(long int num, int n)
{
  int len = length(num);
  int a;
  int *ptr = (int*) malloc(len*sizeof(int));
  long int num1 = reverse(num);
  for(int i=0; i<len; i++)
  {
    ptr[i] = num1%10;
  }
  return ptr[n];
}
int main()
{
  long int num;
  int n;
  printf("Enter a number: \n");
  scanf("%ld", &num);
  printf("Enter the nth position: \n");
  scanf("%d", &n);
  printf("The no. in %d position is: %d", n, position(num, n));
}
Posted
Updated 4-Nov-20 1:57am
v2
Comments
Rick York 4-Nov-20 3:36am    
Why didn't you leave it as a string and then use the array index to get the character? That seems considerably easier to me because I'm not sure exactly what you are trying to do. If you want the fourth character of a string that is string[3]. What else is there to this?

We have no idea what "nth number for input number" actually means - and your simngle example isn't helpful, it's not at all obvious why the 5th number would be "1".

As a result, we can't really help you other than in generics. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
C++
long int reverse(long int num)
{
  int len = length(num);
  int i,rev;
  long int str = 10,mlt;
  while(i<len) // What is the value of i when you reach this point ?
  {
    rev = num%10;
    num = num/10;
    str = (str+rev)*10;
    i++;
  }
  long int num1 = str/10;
  printf("%ld\n", num1);
    return num1;
}

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
You don't have to reverse the original number nto get its ith digit.
Just divide it by 10^(L-i) and then take apply the modulus (% 10)
Where L is the 'length' of the number (the integer part of its decimal logarithm).

Now, back at your code. You don't need the length function to reverse the number and, moreover, your reverse function is incorrect (you didn't initialize the variable i).
In position function, you dynamically allocate memory that then you never release (that's a sloppy approach at programming). By the way you really don't need at all that piece of dynamic memory.
Try
C
#include <stdio.h>
#include <stdlib.h>

long reverse(long int num)
{
  long rev = 0;
  while ( num )
  {
    rev *=  10;
    rev += num % 10;
    num /=  10;
  }
  printf("%ld\n", rev);
    return rev;
}
int position(long int num, int n)
{
  long rev = reverse(num);
  for(int i=1; i<n; i++) // assuming 1-based position
  {
    rev /= 10;
  }
  return (rev % 10);
}
int main()
{
  long int num;
  int n;
  printf("Enter a number: \n");
  scanf("%ld", &num);
  printf("Enter the nth position: \n");
  scanf("%d", &n);
  printf("The no. in %d position is: %d", n, position(num, n));
}


Please note, such algorithm doesn't work with numbers containing trailing zero(es).
 
Share this answer
 
v3

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