简体   繁体   中英

How to Separate 4 numbers in 2 parts in C language

I want to get 4 numbers from user like 1234(printf("enter 4 numbers: "); and separate them like 12 and 34 the out put I mean:

year: 12

month: 34

You did not explain what you can read from users very clearly. Say you get the input from the following code:

int a;
scanf("%d", &a);

and you are very sure that the input is definitely a 4-digit integer. You can do:

int a1 = a / 100;
int a2 = a % 100;
printf("%d, %d", a1, a2);

The below operations yeilds the result that are expected.

    #include <stdio.h>

int main()
{
    int num, year, month;
    scanf("%d",&num);
    year = num%100;
    month = num/100;
    printf("month : %d\t",year);
    printf(" year: %d",month);
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM