哦,题目要求些一个函数。下面代码写了一个函数,而且不限于4位数。
#include <stdio.h>
char* digits(int number, char* buffer, int size)
{
char* p = buffer + (size - 1);
*p = 0; // end of string
if (number == 0)
{
*(--p) = '0';
}
else
{
while (number > 0)
{
int d = number % 10;
number /= 10;
if (*p != 0)
*(--p) = ' ';
*(--p) = '0' + d;
}
}
return p;
}
int main()
{
int n;
char s[100];
printf("Please enter a 4-digit number:");
scanf("%d", &n);
char* p = digits(n, s, 100);
printf("The digits of the number are: %s\n", p);
}
// Output
Please enter a 4-digit number:1990
The digits of the number are: 1 9 9 0
Please enter a 4-digit number:31415926
The digits of the number are: 3 1 4 1 5 9 2 6
附注:求赞助积分和C币。加入CSDN将近20年了。最近几年忙小孩没登录。刚才搜索到一本电子书想下载,需要20积分/C币。已经收到8元了,还查12元。赞助多少都可以。多谢。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
int n;
char s[10];
printf("Please enter a 4-digit number:");
scanf("%4d", &n);
itoa(n, s, 10);
printf("The digits of the number are:\n");
for (int i = 0; i < strlen(s); ++i)
printf("%d ", s[i]);
printf("\n");
}
附注:求赞助积分和C币。加入CSDN将近20年了。最近几年忙小孩没登录。刚才搜索到一本电子书想下载,需要20积分/C币。已经收到8元了,还查12元。赞助多少都可以。多谢。
if(j==1||3||5)改为 if((j==1) || (j==3) || (j==5)) 或者 if(j%2==1)也行
void change(int m[4])
{
for(int i=0; i<7;i++)
{
if(i%2==0)
printf("%c",m[i/2]);
else
printf(" ");
}
也可以
}