Home > Software design >  How to Separate 4 numbers in 2 parts in C language
How to Separate 4 numbers in 2 parts in C language

Time:12-10

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

CodePudding user response:

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);

CodePudding user response:

The below operations yeilds the result that are expected.

    #include <stdio.h>

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

enter image description here

  • Related