Skip to content

Instantly share code, notes, and snippets.

@nhatminhbui
Last active August 11, 2021 11:07
Show Gist options
  • Save nhatminhbui/0831ede9745d95dc75aa3a947049e859 to your computer and use it in GitHub Desktop.
Save nhatminhbui/0831ede9745d95dc75aa3a947049e859 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void PrintASCII() {
for(int i = 0; i < 256; ++i){
printf("%d %c\n", i, i);
}
}
int SumOdd(int n) {
// 1 3 5 7... 2n+1
// num-in-range * (start + end) / 2
return (n+1) * (1 + 2*n+1) / 2;
}
int SumEven(int n) {
// 2 4 6 8... 2n
// #num-in-range * (start + end) / 2
return n * (2 + 2*n) / 2;
}
int Factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) result *= i;
return result;
}
int UCLN(int a, int b) {
while (b != 0) {
int temp = a;
a = b;
b = temp % b;
}
return a;
}
int BCNN(int a, int b) {
return a*b / UCLN(a, b);
}
void HCN_Dac(int d, int r) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < d; j++) {
printf("*");
}
printf("\n");
}
}
void HCN_Rong(int d, int r) {
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= d; j++) {
if (i == 1 || i == r) {
printf("*");
if (j == d) printf("\n");
continue;
}
if (j == 1) printf("*");
else if (j == d) printf("*\n");
else printf(" ");
}
}
}
void TamGiacDac(int h) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
void TamGiacRong(int h) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= i; j++) {
if (i == h) {
printf("*");
continue;
}
if (j == 1 && i != 1) printf("*");
else if (j == i) printf("*\n");
else printf(" ");
}
}
}
void Bai8(int n) {
int count = 0;
int sum = 0;
printf("Cac uoc so chan cua %d la:\n", n);
for (int i = 1; i <= n/2; i++) {
if (n % i == 0) {
sum += i;
count++;
if (i % 2 == 0) printf("%d ", i);
}
}
printf("\nTong tat ca cac uoc cua %d la: %d\n", n, sum);
printf("So cac uoc so cua %d la: %d", n, count);
}
int Bai9 (int n) {
//1 * 3 * 5 * ... * (2n+1)
int result = 1;
for (int i = 3; i <= 2*n+1; i+=2) result *= i;
return result;
}
void BangCuuChuong() {
for (int i = 2; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
printf("%d x %d = %d\n", i, j, i*j);
}
printf("------------\n");
}
}
int main () {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment