Reputation: 83
void main()
{
int i, j = 0, p, q, N; // N is the integer and p is the position needed
printf("Enter two positive integers: ");
scanf("%d%d", &N, &p); // Taking the values of N and p
for(i = 1; i <= p; i++) {
j = N / 10;
q = j % 10;
i++;
j = q;
}
printf("The digit of %d in the position %d is %d", N, p, j);
}
Example Input:
Enter two positive integers:
123456789
3
Output:
The digit of 123456789 in the position 3 is 8
I actually have 2 problems with the above lines of code:
j
to work with the second time it runs Upvotes: 1
Views: 11283
Reputation: 79
#include <stdio.h>
int getDgtLen (int n);
int main(void)
{
// n is the integer and p is the position needed
int i, q, n, p;
// Prompt for taking the values of n and p
do
{
printf("Enter two positive integers for number, position: ");
scanf("%d%d", &n, &p);
}
while (n < 0 || p <= 0);
int nOrigin = n;
int len = getDgtLen(n);
if (p <= len)
{
for (i = 0; i < p; i++)
{
q = n % 10;
n /= 10;
}
printf("The digit of %d in the position %d is %d\n", nOrigin, p, len - q + 1);
}
else
{
printf("position not found!\n");
}
}
int getDgtLen (int n)
{
int i = 0;
while (n > 0)
{
n /= 10;
i++;
}
return i;
}
Upvotes: 0
Reputation: 2683
There are a few ways you can do this. As some of the comment suggests, you can do either of the following:
Method 1:
Convert the number into a string array, which can be accomplished by sprintf
.
char str[256];
int len = sprintf(str,"%d",N);
//traverse str using len
Method 2:
Since a number N with n digits in base b you can express any number up to pow(b,n)-1
. To get the digit you can use the inverse function of pow
, namely base-b logarithm. You may not get integer values, so you can use the floor
and cast the result to int. The full program looks like this:
#include <stdio.h>
#include <math.h>
int getDigit(int num, int p)
{
return (num / (int)pow(10, floor(log10(num)) - p)) % 10;
}
int main()
{
int i,j=0,p,q,N;// N is the integer and p is the position needed
printf("Enter two positive integers: ");
scanf("%d%d",&N,&p);// Taking the values of N and p
j = getDigit(N,p);
//The result is j if you count from 0, j+1 if you count from 1
printf("The digit of %d in the position %d is %d\n",N,p,j+1);
return 0;
}
Upvotes: 0
Reputation: 2817
#include <stdio.h>
int main()
{
int i=0;
int j=0;
int p=5;
int n = 123456;
int digits[12]={};
j=n;
while(j)
{
digits[i]=j%10;
j = j/10;
i++;
}
printf("The digit of %d in the position %d is %d",n,p,digits[i-p]);
return 0;
}
The easiest way for me to design is to convert the integer to digits and look up.
Why this way:
To avoid pow
, log10
because they need floating point support to run, and hardware floating point to run fast.
And to avoid two loops because those two loops does a lot of repeated calculation.
Upvotes: 0
Reputation: 526
Without using 2 loops :
int main()
{
int i,ans,p,N,no_of_digits=0,temp;
printf("Enter two positive integers: ");
scanf("%d%d",&N,&p);
temp=N;
no_of_digits=(int)log10(N) + 1;
while(i<=(no_of_digits-p))
{
ans=N%10;
N=N/10;
i++;
}
printf("The digit of %d in the position %d is %d\n",temp,p,ans);
}
Upvotes: 0
Reputation: 3332
First you count the number of digits in the integer,let say n
.Then you can get the first digit by dividing the number by 10n-1.Then put this in a loop and decrease n by 1 & number N by
N%=10n-1
int main()
{
int i,j=0,p,N;
printf("Enter two positive integers: ");
scanf("%d%d",&N,&p);
int pow=1,tmp=N;
//counting power of 10 to divide
while(tmp>10)
{
pow*=10;
tmp/=10;
}
tmp=N;
for(i=1; i<=p;i++){
j = tmp/pow;
tmp%=pow;
pow/=10;
}
printf("The digit of %d in the position %d is %d\n",N,p,j);
}
Upvotes: 1