简体   繁体   English

如何在 C 语言中分隔 2 个部分中的 4 个数字

[英]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:我想从用户那里得到 4 个数字,比如 1234(printf("enter 4 numbers: "); 并将它们分开,比如输出 12 和 34 我的意思是:

year: 12年:12

month: 34月: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.并且您非常确定输入肯定是 4 位 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);
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c - 如何将字符串分成多个部分并存储到C中的数组中? - How to separate a string into parts and store into an array in C? C语言数组如何提供随机数? - How C language array provides a random numbers? 如何用“,”符号分隔字符串的各个部分 - How to separate parts of strings by “,” sign 如何在 C 语言中不指定数组大小的情况下将数字读入数组 - How to read numbers into an array without specifying the array size in C language C语言-&gt;将段落中的单词分开 - C Language -> Separate words from a paragraph 使用子例程分隔偶数和素数的汇编语言程序 - Assembly language program to separate even and prime numbers using subroutines 如何将文本文档中的字符串转换为列表,并将列表分隔为 python 中的部分 - How to convert strings in a text doc into list, and separate list into parts in python 如何将我的数组分成 discord.js 中的一些部分? - How to separate my array into some parts in discord.js? 如何从字符串中分隔数字和算术运算符? - How to separate numbers and arithmetic operators from a string? 如何从数组中分离负数和正数? - How to separate negative numbers and positive numbers from an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM