c语言程序设计例题

1。编写一个程序输出5!10!的结果。
2。编写一个程序,输入2个学生的姓名。学号。英语。数学。计算机成绩,输出这两个学生的姓名,学号和平均分。
我是菜鸟,希望高手帮帮我

题目1
#include "stdio.h"
void main(void)
{
int n=5,m=10,i=1;
long sum=1;
for(;i<=n;i++)
{
sum*=i;
}
printf("\n5!=%d",sum);
for(i=1;i<=10;i++)
{
sum*=i;
}
printf("\n10!=%d",sum);
}
题目2
#include "stdio.h"
#include "string.h"
struct Student
{
char s_Name[25];
long n_Code;
int n_English;
int n_Math;
int n_Computer;
}student_1,student_2;
void main(void)
{
printf("\nStudent1:\nName:");
scanf("%s",&student_1.s_Name);
printf("StudentNum:");
scanf("%d",&student_1.n_Code);
printf("English Score:");
scanf("%d",&student_1.n_English);
printf("Math Score:");
scanf("%d",&student_1.n_Math);
printf("Computer Score:");
scanf("%d",&student_1.n_Computer);

printf("\nStudent2:\nName:");
scanf("%s",&student_2.s_Name);
printf("StudentNum:");
scanf("%d",&student_2.n_Code);
printf("English Score:");
scanf("%d",&student_2.n_English);
printf("Math Score:");
scanf("%d",&student_2.n_Math);
printf("Computer Score:");
scanf("%d",&student_2.n_Computer);

printf("\nStudent1:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_1.s_Name,student_1.n_Code,student_1.n_English,student_1.n_Math,student_1.n_Computer);
printf("\nStudent2:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_2.s_Name,student_2.n_Code,student_2.n_English,student_2.n_Math,student_2.n_Computer);

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-08-05
第一题:
该题可以使用迭代的方法进行计算!
#include<stdio.h>
int fun(int i)
{
if(i == 1){
return i;
}else{
return i * fun(i - 1);
}
}

int main()
{
int n;
printf("请输入一个整数,以0结束:");
scanf("%d", &n);
while(n!=0){
printf("%d!=%d\n", n, fun(n));
printf("请输入一个整数,以0结束:");
scanf("%d", &n);
}
return 0;
}
第二题:
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;

struct Student{
string stuId;
string stuName;
float engScore;
float mathScore;
float comScore;
};

int main(){
while(1){
Student stu1;
int i = 0;
cout << "请输入学号:";
cin >> stu1.stuId;
cout << "请输入学生姓名:";
cin >> stu1.stuName;
cout << "请输入英语成绩:";
cin >> stu1.engScore;
cout << "请输入数学成绩:";
cin >> stu1.mathScore;
cout << "请输入计算机成绩:";
cin >> stu1.comScore;
cout << "学号:" << stu1.stuId << "\n学生姓名:" << stu1.stuName;
cout << "平均分:" << (stu1.engScore + stu1.mathScore + stu1.comScore) / 3 << endl;
}
return 0;
}
第2个回答  2009-08-04
第一个问题答案为
#include<stdio.h>
int f(int i)
{
int j;
int m=1;

for(j=i;j>1;j--)
{
m=m*j;
}
return m;
}
main()
{
printf("5!的结果为%d\n",f(5));
printf("10!的结果为%d\n",f(10));
}
第二个问题答案为:
#include<stdio.h>
struct stu
{
char n[80];
char x[80];
float avg;
}s[2];
main()
{
float i,j;
int k;
for(k=0;k<2;k++)
{
printf("输入第%d个学生姓名 学号 英语成绩 数学成绩(用空格隔开)\n",k+1);
scanf("%s %s %f %f",s[k].n,s[k].x,&i,&j);
s[k].avg=.5*(i+j);

}

for(k=0;k<2;k++)
printf("第%d个学生的姓名:%s 学号:%s 平均成绩:%f\n",k+1,s[k].n,s[k].x,s[k].avg);
}
ls ms比较繁琐
第3个回答  2009-08-16
1 简单的迭代
2 模拟