Skip to content

Instantly share code, notes, and snippets.

@nhatminhbui
Last active August 1, 2021 11:46
Show Gist options
  • Save nhatminhbui/ee0eb0166a803cf87ed1731641599c1a to your computer and use it in GitHub Desktop.
Save nhatminhbui/ee0eb0166a803cf87ed1731641599c1a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
char Lowercase(char c) {
// A to Z only
char uppercase = (int)c;
if (uppercase >= 65 && uppercase <= 90) uppercase += 32;
return uppercase; //printf("%c")
}
void SolveLinear(float a, float b) {
if (a != 0) printf("x = %f", (-1)*b/a);
else {
if (b == 0) printf("infinite solutions");
else printf("no solution");
}
}
void SolveQuadratic(float a, float b, float c) {
if (a == 0) SolveLinear(b, c);
else {
float delta = b*b - 4*a*c;
if (delta > 0) {
printf("x1 = %f\n", ((-1)*b + sqrt(delta)) / (2*a));
printf("x2 = %f\n", ((-1)*b - sqrt(delta)) / (2*a));
}
else if (delta == 0) printf("x = %f", ((-1)*b) / (2*a));
else printf("no real solution");
}
}
int Min(int a, int b) {
if (a < b) return a;
else return b;
}
int Min4(int a, int b, int c, int d) {
return Min(Min(a, b), Min(c, d));
}
// B6
int ReverseNumber(int n) {
int reversed = 0;
for (; n>0; n/=10) {
reversed *= 10;
reversed += n % 10;
}
return reversed;
}
int SumOddDigit(int n) {
int sum = 0;
for (; n>0; n/=10) {
if ((n % 10) % 2 != 0) sum += n % 10;
}
return sum;
}
bool KTChinhPhuong(int n) {
return sqrt(n) == floor(sqrt(n));
}
bool KTNguyenTo(int n) {
if (n == 1) return false;
if (n == 2) return true;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment