4

My program asks the user for a year like 2056. So in main, I use scanf.

However, I have a function which requires the first 2 digits of this value only.

I've tried using another scanf inside the function itself scanf("%2d"...) but then I can't have the other scanf in main for my other functions.

So how can I do this?

Sam
  • 147
  • 2
  • 4
  • 7

6 Answers6

10
int year = 2056;
int firstPart = year / 100;
juergen d
  • 201,996
  • 37
  • 293
  • 362
3

How about just divide the year number by 100?

int year, first_two_digits;
scanf("%d",&year);
first_two_digits = year / 100;

I wonder how your program should behave after year 10000.

wks
  • 1,158
  • 1
  • 7
  • 12
3

In this case, I would just read the year and extract the first two digits:

int year;
scanf("%4d", &year); // assuming a four digit year
int first_two_digits = year / 100;
Michael Koval
  • 8,207
  • 5
  • 42
  • 53
3

I had a very similar question - https://math.stackexchange.com/questions/120459/how-to-find-the-first-and-last-n-2-digits-from-an-n-digit-number

In your case, if it is always a 4-digit number and you always need the first 2-digits, then as others suggested X/100 would be the best thing to do.

But if you need n/2-digits from an n-digit integer, say X, then you may have to use the following:

first n/2 digit (for even value of n) = X / 10^(n/2)  
first n/2 digit (for odd value of n)  = X / 10^((n+1)/2) 

last n/2 digit (for even value of n) = X - [first n/2 digit * pow(10, n/2)] 
last n/2 digit (for odd value of n)  = X - [first n/2 digit * pow(10, (n+1)/2)]

The following may be an overkill, but you may be looking for something like this!

int getn(int num)
{
        return (int) log10(num) + 1;
}

void gethalfs(int num, int * first, int * last)
{
        int n, po;

        if (num == 0) return;

        n = getn(num);
        /* Here you can calculate the value of po based on whether n is even or odd */
        po = (int) pow(10, n/2);

        *first = num / po;
        *last  = num - *first * po;

        return;
}

In order to get the number of digits in a given integer i.e. n in the above case, you may look at the following possible methods - count number of digits - which method is most efficient?

Hope this helps!

Community
  • 1
  • 1
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
2

using separate variable to get the first two digits that is first2digits=year/100;

karthik gorijavolu
  • 860
  • 3
  • 14
  • 28
1

You can get the first 2 digits of any number by using the flowing :

INT(X*10^(-INT(LOG(X))+1))

for Example :

  • X = 0.005432 then Log(X) = -2.265 and Int(Log(x)) = -3 . So, 10^(-int(log(x))+1) = 10^4 = 10000. Finally , int(X * 10000 ) = 54 which are the first 2 digits.

  • X = 3.45 then Log(X) =0.53 and Int(Log(x)) = 0 So, 10^(-int(log(x))+1) = 10^1 = 10. Finally, int(X * 10 ) = 34 which are the first 2 digits.

  • X = 3543.45then Log(X) =3.459 and Int(Log(x)) = 3 So, 10^(-int(log(x))+1) = 10^-2 = 0.01. Finally , int(X * 10 ) = 35 which are the first 2 digits.

Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48
magdi
  • 11
  • 1