サイクルの小さい例、フィポラチのシーケンス、サイクルは1元の2次方程式とswitchの例のプログラムを解きます
# include <stdio.h>
int main(void)
{
int n;
int i;
int f1, f2, f3;
f1 = 1;
f2 = 1;
printf(" :");
scanf("%d", &n);
for (i=3; i<n; i++)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
printf("%d ", f3);
}
return 0;
}
一元二次方程式の解:
# include <stdio.h>
# include <math.h>
int main(void)
{
double a, b, c;
double delta;
double x1, x2;
char ch;
do {
printf(" :
");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
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
", x1, x2);
}
else if (0 == delta)
{
x1 = x2 = (-b) / (2*a);
printf(" ,x1 = x2 = %f
", x1, x2);
}
else
{
printf(" !");
}
printf(" (Y/N):");
scanf(" %c", &ch); //%c ,
} while ('y'==ch || 'Y'==ch);
return 0;
}
switchの例:
# include <stdio.h>
int main(void)
{
int val;
printf(" :");
scanf("%d", &val);
switch (val)
{
case 1:
printf("1 !
");
break;
case 2:
printf("2 !
");
break;
case 3:
printf("3 !
");
break;
default:
printf(" !
");
break;
}
return 0;
}