Skip to document

Tổng hợp code C về Hình - vẽ hình

vẽ hình
Course

Programming Fundamentals (PRF192)

999+ Documents
Students shared 2755 documents in this course
Academic year: 2022/2023
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Trường Đại học FPT

Comments

Please sign in or register to post comments.

Preview text

Tổng hợp code C về Hình

Example 1

*

* *

* * *

* * * *

* * * * *

#include <stdio> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("\n"); } return 0; }

Example 2

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

#include <stdio> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("%d ", j); } printf("\n"); } return 0; }

Example 3

A

B B

C C C

D D D D

E E E E E

#include <stdio> int main() { int i, j; char input, alphabet = 'A';

printf("Enter an uppercase character you want to print in the last row: "); scanf("%c", &input); for (i = 1; i <= (input - 'A' + 1); ++i) { for (j = 1; j <= i; ++j) { printf("%c ", alphabet); } ++alphabet; printf("\n"); } return 0; }

Example 4

* * * * *

* * * *

* * *

* *

*

#include <stdio> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("\n"); } return 0; }

Example 5

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

#include <stdio> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) { printf("%d ", j); } printf("\n"); } return 0; }

Example 6

*

* * *

* * * * *

* * *

*

#include <stdio> int main() { int rows, i, j, space; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (space = 0; space < rows - i; ++space) printf(" "); for (j = i; j <= 2 * i - 1; ++j) printf("* "); for (j = 0; j < i - 1; ++j) printf("* "); printf("\n"); } return 0; }

Example 8

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

//Pascal's Triangle #include <stdio> int main() { int rows, coef = 1, space, i, j; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 0; i < rows; i++) { for (space = 1; space <= rows - i; space++) printf(" "); for (j = 0; j <= i; j++) { if (j == 0 || i == 0) coef = 1; else coef = coef * (i - j + 1) / j; printf("%4d", coef); } printf("\n"); } return 0; }

Example 9

1

2 3

4 5 6

7 8 9 10

#include <stdio> int main() { int rows, i, j, number = 1; printf("Enter the number of rows: ");

scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; ++j) { printf("%d ", number); ++number; } printf("\n"); } return 0; }

Example 10

* * * * * * * * *

* * * *

* * * *

* * * *

* * *

* * * *

* * * *

* * * *

* * * * * * * * *

#include <stdio> int main() { int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == 1 || i == j || j == 1 || j == n || i == n || i + j == n + 1) printf("# "); else printf(" "); } printf("\n"); } }

Example 11

*****

* *

* *

* *

*****

#include <stdio> int main() { int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j < n - i + 1; j++) { printf(" "); } for (j = 1; j <= n; j++)

}

}

return 0; }

Example 12

input n: 5 *








#include <stdio>

int main() { int n; printf("input n: "); scanf("%d", &n);

//thoi for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { printf(" "); }

for (int j = 1; j <= 2 * i - 1; j++) { printf(" * "); } printf("\n"); }

for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { printf(" "); }

for (int j = 1; j <= 2 * i - 1; j++) { printf(" * "); } printf("\n"); } }

Example 13

* * * * * * * * * *

* * * * * * * *

* * * * * *

* * * *

* *

* * * *

* * * * * *

* * * * * * * *

* * * * * * * * * *

#include <stdio>

int main() { int n; scanf("%d", &n);

for (int i = 1; i <= n; i++) { for (int j = 1; j <= 2 * n; j++) { if (j <= n - i + 1 || j >= n + i) { printf(" * "); } else { printf(" "); } } printf("\n"); }

for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= 2 * n; j++) { if (j <= n - i + 1 || j >= n + i) { printf(" * "); } else { printf(" "); } } printf("\n"); } }

Example 14

Enter the number of columns *****



** * ** *** **** *****

else { for (int j = 1; j <= n / 2; j++) { printf(" "); } printf(" + "); } printf("\n"); } return 0; }

Example 16

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

#include <stdio> #include <math>

void tamGiacThuong(int h) { for (int i = 1; i <= h; i++) { for (int j = 1; j < 2 * h; j++) { if (abs(h - j) <= (i - 1)) { printf("%3d", i - abs(h - j)); } else { printf(" "); } } printf("\n"); } }

int main() { int h; scanf("%d", &h); tamGiacThuong(h); return 0; }

Example 17

Enter the number of rows 1 2 3 4 5 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1

#include <stdio> #include <stdlib> int main() {

int i,j,rows,space=0; printf("Enter the number of rows"); scanf("%d",&rows);//taking numer of rows from user

for(i=rows; i>=1; i--){ //outer for loop for(j=1; j<=space; j++) printf(" "); for(j=1; j<=i; j++) printf("%d ",j);

for(j=i-1; j>=1; j--) printf("%d ",j); printf("\n"); space++; } getch(); return 0; }

Example 18

*

**

* *

* *

* *

* *

* *

********

#include <stdio> int main() { int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { if (j == 1 || i == n || i == j) printf("*"); else printf(" "); } printf("\n"); } }

Example 19

Enter the number of rows: 5 * * * *




#include <stdio> int main() { int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == 1 || i == n || i == j) { printf("* "); } else printf(" "); } printf("\n"); } }

Example 21

#include <stdio> int main() { int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == 1 || i == n || i == j || i + j == n + 1) { printf("* "); } else printf(" ");

}

printf("\n"); } }

Was this document helpful?

Tổng hợp code C về Hình - vẽ hình

Course: Programming Fundamentals (PRF192)

999+ Documents
Students shared 2755 documents in this course
Was this document helpful?
Tổng hợp code C về Hình
Example 1
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Example 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Example 3
A
B B
C C C
D D D D
E E E E E
#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';