c语言编程,考试,急!!!!
发布网友
发布时间:2022-04-22 06:38
我来回答
共1个回答
热心网友
时间:2024-02-28 07:25
%%%%%%%%%
%% 1
%%%%%%%%%
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
int sum=0;
long n;
printf("input n=:\n");
scanf("%ld",&n);
printf("n=%ld\n",n);
while(n!=0)
{
sum+=n%10;
n/=10;
}
printf("sum=%d\n",sum);
getch();
}
%%%%%%
%% 2
%%%%%%
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 100
int main(void)
{
int i,n;
float salary[N]={100,200,400,500,600};
float average;
float pross(float s[],int n);
n=5;
printf("Befor pross:\n");
for (i=0;i<n;i++)
printf("%f\t",salary[i]);
printf("\n");
average=pross(salary,n);
printf("After pross:\n");
for (i=0;i<n;i++)
printf("%f\t",salary[i]);
printf("\n");
printf("average=%f\n",average);
getch();
}
float pross(float s[],int n)
{
int i;
float sum=0;
for (i=0;i<n;i++)
{
if (s[i]<500)
s[i]+=100;
sum+=s[i];
}
return sum/n;
}
%%%%%%
%% 3
%%%%%%
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 20
int main(void)
{
char str1[N]="ZhiDaoBaiDu";
char str2[N]="HiEverybody";
char str3[2*N];
int newstring(char s1[],char s2[],char s3[]);
newstring(str1,str2,str3);
printf("str1=%s\n",str1);
printf("str2=%s\n",str2);
printf("str3=%s\n",str3);
getch();
}
int newstring(char s1[],char s2[],char s3[])
{
int i,n=5;
for (i=0;i<n;i++)
s3[i]=s1[i];
for (i=0;i<n;i++)
s3[n+i]=s2[i];
s3[n+n]='\0';
return 1;
}