# Chapter V assignment 2

I

1. Define int, n = 0, i;

2. Assign 0 to i;

3. Judge I < 3;

4. True: n + +; Steering 5; False: end;

5. Judge the value of n + +;

6.n=0:

7. True: n=1? False: n=1?;

8. True: n=2; False: n=2;

9. True: n=3; False: n=3;

10. True: output the value of n + +; Steering 11; False: steering 11;

11.i++； Turn back 3.

```#include<stdio.h>int main()
int main()
{
int n = 0, i;
for (i = 0; i < 3; i++)
switch (n++)
{
case 0:
case 1:
case 2:
case 3:printf("%2d", n++);
}
return 0;
}```

II

1. Define long, N, I, K, J, P, sum;

2. Assign 2 to n;

3. Judge n < = 10;

4. True: steering 5; False: end;

5. Calculate k = n * n * n;

6. Assign 1 to i;

7. Judge I < K / 2;

8. True: turn 9; False: steering 25;

9. Assign i to j and 0 to sum; Steering 10;

10. True: turn 11; False: steering 24;

11. Judge sum < K;

12. True: turn 13; False: steering 23;

13. Calculate sum += j; Steering 14;

14； Judge sum == k;

15. True: turn 16; False:; Steering 22;

16. Output the value of n*n*n=sun =; Steering 17;

17. Assign i to p;

18. Judgment P < J-2;

19. True: turn to 20; False: output the value of p; Steering 22;

20. Output the value of p +; Steering 21;

21.p+=2; Turn back 18;

22.j+=2; Turn back 11;

24.i+=2； Turn back 7;

25: judgment I > = K / 2;

26. True: steering 27; False: steering 28;

27. Output \ n error!; Steering 28;

28. Calculate n + +; Turn back 3.

```#include<stdio.h>
int main()
{
long n, i, k, j, p, sum;
for (n = 2; n <= 10; n++)
{
k = n * n * n;
for (i = 1; i < k / 2; i += 2)
{
for (j = i, sum = 0; sum<k; j += 2)
sum += j;
if (sum == k)
{
printf("\n%ld*%ld*%ld=%ld=", n, n, n, sum);
for (p = i; p < j - 2; p += 2)
printf("%ld+", p);
printf("%ld", p);
break;
}
}
if (i >= k / 2)
printf("\n error!");
}
return 0;
}```

III

1.long m,n,k,s,flag=0;

2. Output input k:

3. Enter the value of k;

4.n=k；

5.m=n;

6.s=n*(n-m)-m*m;

7. Judge s*s==1;

8. True: turn 9; False: steering 10;

9.flag=1, steering 11;

10.m--；

11. Judge m > 0 & &! flag；

12. True: Turn 6, false: turn 13;

13. Judge m==0;

14. True: turn to 15, otherwise turn to 16;

15.n--；

16. Judge n > 0 & &! flag；

17. True: turn back 5, false: turn back 18;

18. Output m, n, end.

```#include<stdio.h>
int main()
{
long m, n, k, s, flag = 0;
printf("input k:");
scanf_s("%ld", &k);
n = k;
do
{
m = n;
do
{
s = n * (n - m) - m * m;
if (s * s == 1)
{
flag = 1;
}
else
{
m--;
}
}while (m > 0 && !flag);
if (m == 0)
n--;
}while (n > 0 && !flag);
printf("m=%ld,n=%ld", m, n);
return 0;
}```

IV

1.int i,j,line=0;long int n;

2. Enter Please enter n:

3.long int n;

4. Output Please enter n:

5. Enter the value of n;

6. Judge n < = 1;

7. True: steering 8; False: steering 10;

8. Output Enter error,enter again

9. Enter n;

10.i=2；

11. Judgment I < = n

12. True: turn 13; False: end;

13.j=2；

14. Judge J < I;

15. True: turn 16; False: steering 25;

16. Judge I% J = = 0;

17. True: turn 24; False: steering 18;

18. Judge J = = I & & I% 10= 9；

19. True: value of output i; Steering 20; False: steering 24;

20.line++；

21. Judgment line==10;

22. True: output \ n; Steering 23; False: steering 24;

23.line=0； Turn back 18;

24.j++； Turn back 14;

25i++； Turn back 11.

```#include<stdio.h>
int main()
{
int i, j, line = 0;
long int n;
printf("Please enter n:");
scanf_s("%ld", &n);
while (n <= 1)
{
printf("Enter error,enter again.");
scanf_s("%d", &n);
}
for (i = 2; i <= n; i++)
{
for(j=2;j<i;j++)
if (i % j == 0)
break;
if (j == i && i % 10 != 9)
{
printf("%d\t", i);
line++;
if(line==10)
{
printf("\n");
line = 0;
}
}
}
return 0;
}```

V

1.int i,j,n;long temp,sum=0;

2. Enter Please input terms numbers

3. Assign the value to n;

4. If n < 1|n > 10, turn to step 5, otherwise turn to step 7;

5. Output Enter error, enter again

6. Assign the value to n and go to step 4;

7. i=0；

8. If I < n, turn to step 9, otherwise turn to step 15;

9.j=0；

10. If J < = I, turn to step 11, otherwise turn to step 13;

11.temp+=(long int)pow(10,j)*(i+1);

12.j + +, go to step 10;

13.sum+=temp;

14.i + +, go to step 8;

15. Output sum.

```#include<stdio.h>
int main()
{
int i, j, n;
long temp, sum = 0;
printf("Please input terms numbers.");
scanf_s("%d", &n);
while (n < 1 || n>10)
{
printf("Enter error,enter again.");
scanf_s("%d", &n);
}
for (i = 0; i <= n; i++)
{
temp = 0;
for (j = 0; j <= i; j++)
temp += (long int)pow(10, j) * (i + 1);
sum += temp;
}
printf("sum=%ld\n", sum);
return 0;
}```

Vi

1. Define int, I, J, n = 0, sum;

2.i=3;

3. Judge I < = 1000,

4. True: turn to 5, false: output the value of n; end;

5.sum=0；

6.j=1；

7. Judge J < = n;

8. True: turn 9; False: steering 14;

9. Judge n%j==0,

10. True: turn 11; False: steering 12;

11..sum=sum+j；

12.n++；

13.j++； Turn back 7;

14. Judgment i == sum

15. True: turn 16; False: steering 21;

16； The value of output i;

17.n++；

18. Judge n%5==0;

19. True: turn to 20; False: turn back 14;

20. Output the value of n; Turn back 14;

21: i++； Turn back 4.

```#include<stdio.h>
int main()
{
int i, j, n = 0, sum;
for (i = 3; i <= 1000; i++)
{
sum = 0;
for (j = 1;j < n - 1; j++)
if (n % j == 0)sum = sum + j;
if (i == sum)
{
printf("%d", i);
n++;
if (n % 5 == 0)
printf("\n");
}
}
printf("\n%d\n", n);
return 0;
}```

VII

1.int i，n=0；

2.i=1900；

3. If I < = 2000, turn to step 4, otherwise turn to step 10;

4. If I% 4 = = 0 & & I% 100= 0|i%400 = = 0, turn to step 5, otherwise turn to step 9;

5. Output i;

6.n++；

7. If n%3==0, turn to step 8, otherwise turn to step 9;

8. Output line feed;

9.i + +, go to step 3;

10. End of output

```int main()
{
int i, n = 0;
for (i = 1900; i <= 2000; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
printf("%d", i);
n++;
if (n % 3 == 0)
printf("\n");
}
}
return 0;
}```

VIII

1. int i,a,b,c,n=0;

2.i=100；

3. If I < = 999, turn to step 4, otherwise turn to step 13;

4.a=i/100；

5.b=(i-a*100)/10;

6.c=i%10;

7. If a * a * a + b * b * B + C * C = = I, turn to step 8, otherwise turn to step 12;

8. Output i;

9.n++；

10. If n%2==0, turn to step 11, otherwise turn to step 12;

11. Output line feed;

12.i + +, go to step 3;

13. End of output

```#include<stdio.h>
int main()
{
int i, a, b, c, n = 0;
for (i = 100; i <= 999; i++)
{
a = i / 100;
b = (i - a * 100) / 10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
printf("%d", i);
n++;
if (n % 2 == 0)
printf("\n");
}
}
return 0;
}```

IX

1.int k=1,i,n,t;

2.float e=1，s=1；

3. Enter a value to assign to n;

4.t=1；

5.i=1；

6. If I < = k, turn to step 7, otherwise turn to step 11;

7.t=t*i；

8.e=e+1.0/t；

9.k++；

10.i + +, go to step 6;

11. If K < = n, turn to step 4, otherwise turn to step 12;

12. Output e.

```#include<stdio.h>
int main()
{
int k = 1, i, n, t;
float e = 1, s = 1;
scanf_s("%d", &n);
do
{
t = 1;
for (i = 1; i <= k; i++)
t = t * i;
e = e + 1.0 / t;
k++;
} while (k <= n);
printf("%.2f", e);
return 0;
}```

X

1.float y,s=1,x,xx=1,a=1,i=1;

2. Enter a value to assign to x;

3.a=-a;

4.i++;

5.xx=xx*x;

6.y=i/xx;

7.s=s+a*y;

8. If Y > 0.00001, turn to step 3, otherwise turn to step 9;

9. Output s

```#include<stdio.h>
int main()
{
float y, s = 1, x, xx = 1, a = 1, i = 1;
scanf_s("%f", &x);
do
{
a = -a;
i++;
xx = xx * x;
y = i / xx;
s = s + a * y;
} while (y > 0.00001);
printf("%f", s);
return 0;
}```

Tags: C++ C R Language

Posted by justgrafx on Sat, 11 Dec 2021 02:03:12 +1030