ex.1 解一元二次方程
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
float a, b, c, x1, x2;
float delta, real, imag;
printf("Enter a, b, c: ");
while(scanf("%f%f%f", &a, &b, &c)) {
if(a == 0)
printf("not quadratic equation.\n");
else {
delta = b*b - 4*a*c;
if(delta >= 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("x1 = %f, x2 = %f\n", x1, x2);
}
else {
real = -b/(2*a);
imag = sqrt(-delta) / (2*a);
printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag);
}
}
printf("Enter a, b, c:\n");
}
system("pause");
return 0;
}
ex.2 猜数游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guessNumber;
int ans;
srand((time(0)));
guessNumber = 1 + rand()%100;
do {
printf("your guess number is(1~100): ");
scanf("%d", &ans);
if(ans < guessNumber)
printf("%d is lower than the number.\n", ans);
else if(ans > guessNumber)
printf("%d higher then the number.\n", ans);
}while(ans != guessNumber);
printf("Congratulations. you hit it~\n");
system("pause");
return 0;
}
ex.3 找出最大数和最小数
#include <stdio.h>
#include <stdlib.h>
int main() {
int number, max, min, n;
n=1;
printf("输入第%d个数: ", n);
scanf("%d", &number);
max = number;
min = number;
while(n<5) {
n++;
printf("输入第%d个数: ", n);
scanf("%d", &number);
if(max<number)
max = number;
else if(min>number)
min = number;
}
printf("最大数为: %d\n", max);
printf("最小数为: %d\n", min);
system("pause");
return 0;
}
ex.4 求100到200中的素数
#include<stdio.h>
#include<math.h>
int isprime(int n);
int main()
{
int i;
for(i=100;i<=200;i++)
{
if(isprime(i))
printf("%4d",i);
}
return 0;
}
int isprime(int n)
{
int k;
for(k=2;k<=sqrt(n);k++)
if(n%k==0)
return 0;
return 1;
}

ex.5 从一个数中找出奇数并组成新的数
#include<stdio.h>
#include<stdlib.h>
int main()
{
long i,a=0;
int b,c=1;
printf("number:");
scanf("%ld",&i);
while(i)
{
b=i%10;
if(b%2!=0)
{
a=a+b*c;
c=c*10;
i=i/10;
}
else
i=i/10;
}
printf("new number:%ld\n",a);
return 0;
}

实验小结:实验中的错误:part2中填空第一个写成n<=5
part3中第二个编程练习个位数若是奇数一开始没有输出
原文:https://www.cnblogs.com/pyp2001/p/11886375.html