从键盘上输入n(n<50)个任意位的正整数m(m<32767),将每个整数的各位数字之和存放在a数组中。要求:求每个整数k的各位数字之和用自定义函数
从键盘上输入n(n<50)个任意位的正整数m(m<32767),将每个整数的各位数字之和存放在a数组中。要求:求每个整数k的各位数字之和用自定义函数来实现。
自己动手 丰衣足食 哪里不会再来请教!
#include <stdio.h> void func(int Array[ ],int a[ ],int n) { for(int i=0;i<n;i++) { int sum=0;//和 if(Array[i]>9999) { sum+=Array[i]/10000; Array[i]-=(Array[i]/10000)*10000; sum+=Array[i]/1000; Array[i]-=(Array[i]/1000)*1000; sum+=Array[i]/100; Array[i]-=(Array[i]/100)*100; sum+=Array[i]/10; Array[i]-=(Array[i]/10)*10; sum+=Array[i]; a[i]=sum; } else if (Array[i]>999) { sum+=Array[i]/1000; Array[i]-=(Array[i]/1000)*1000; sum+=Array[i]/100; Array[i]-=(Array[i]/100)*100; sum+=Array[i]/10; Array[i]-=(Array[i]/10)*10; sum+=Array[i]; a[i]=sum; } else if (Array[i]>99) { sum+=Array[i]/100; Array[i]-=(Array[i]/100)*100; sum+=Array[i]/10; Array[i]-=(Array[i]/10)*10; sum+=Array[i]; a[i]=sum; } else if (Array[i]>9) { sum+=Array[i]/10; Array[i]-=(Array[i]/10)*10; sum+=Array[i]; a[i]=sum; } else sum=Array[i]; } } void main() { int n;//要输入的数的个数 int Array[50]; int a[50]; printf("input how many numbers you want : "); scanf("%d",&n); printf("please input several(<=50) numbers :\n"); for(int i=0;i<n;i++) scanf("%d",&Array[i]); func(Array,a,n);// for(int i=0;i<n;i++) printf("the sum of Array[%d] is %d \n",i,a[i]); }