1. Output the following pattern on the screen:
*
***
*****
******
*********
***********
***** ********
************
*********
*******
*****
***
*
*
***
*****
******
*********
***********
***** ********
************
*********
*******
*****
***
*
int i = 0, j = 0; for (i = 0; i <= 6; i++) { for (j = 0; j <= 2 * i; j++) printf("*"); printf("\n"); } for (i = 0; i <= 5; i++) { for (j = 0; j <= 10 - 2 * i; j++) printf("*"); printf("\n"); }
2. Find all "daffodils" between 0 and 999 and output.
"Daffodil number" refers to a three-digit number whose cubic sum of each number is exactly equal to the number itself, such as; 153=1^3+5^3+3^3,
Then 153 is a "daffodil number".
#include <stdio.h> #include <Windows.h> #include <math.h> int main() { for (int i = 100; i <= 999; i++) { //Intercept the ones and tens, hundreds int a = i % 10; int b = i / 10 % 10; int c = i / 100 % 10; if (pow(a, 3) + pow(b, 3) + pow(c, 3) == a * 100 + b * 10 + c) { printf("%d%d%d \n", a, b, c); } } system("pause"); return 0; }
3. Find the sum of the first 5 terms of Sn = a + aa + aaa + aaaa + aaaaa, where a is a number, for example: 2 + 22 + 222 + 2222 + 22222
int a, n, t = 0, Sn = 0; printf("Enter a and n:\n"); scanf_s("%d%d", &a, &n); for (int i = 1; i <= n; i++) { t = t * 10 + a; Sn += t; } printf("Sn=%d\n", Sn);
4. Write a program that reads C source code from standard input and verifies that all curly braces are paired correctly.
int a, account = 0; printf("Please enter the code:\n"); while ((a = getchar()) != EOF) { if (a == '{') { account++; } else if (a == '}' && account == 0) { printf("The curly braces are not paired\n"); return 0; } else if (a == '}' && account != 0) { account--; } } if (account > 0) { printf("The curly braces are not paired\n"); } else { printf("Paired curly braces\n"); }