实验三 循环
#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;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guessNumber; // 存放程序运行时生成的1~100之间的随机整数
int ans; // 存放用户猜的数字
srand((time(0))); // 以时间函数作为随机种子
guessNumber = 1 + rand()%100; // 生成1~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"); // 在devc中运行时,这一行可以去掉
return 0;
}
#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(number>=max)
max = number;
else if(number<=min)
min = number;
}
printf("最大数为: %d\n", max);
printf("最小数为: %d\n", min);
system("pause");
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
int m,n,x,i;
x=0;
for(n=101;n<=200;n++)
{
m=sqrt(n);
for(i=2;i<=m;i++)
if(n%i==0)
break;
if(i>m)
{
printf("%5d",n);
x++;
if(x%5==0){
printf("\n");}
} }
printf("\n");
printf("101~200之间有%d个素数",x);
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
long int a;
int b;
int sum;
int count;
int number;
b=0;
sum=0;
count=1;
number=0;
printf("Enter a number:");
scanf("%ld",&a);
while(a!=0)
{
b=a%10;
a=a/10;
if(b%2==1){
sum=sum+b*count;
number++;
count=pow(10,number);
}
}
printf("%ld",sum);
return 0;
}


浙公网安备 33010602011771号