You are on page 1of 3

#include <iostream>

#include <cmath>
#include <string>

using namespace std;

// Tinh giai thua:


void factorial() {
cout << "Nhap so can tinh giai thua: ";
int n;
cin >> n;

int fact = 1;
if (n < 2) {
fact = 1;
} else {
for (int i = 2; i <= n; i++) {
fact *= i;
}
}
cout << n << "! = " << fact;
}

// In day fibonaci:
void fibonacci() {
cout << "Nhap so luong so fibonacci can in: ";
int num;
cin >> num;

cout << endl;

long arr[50];
arr[0] = 0;
arr[1] = 1;

for (int i = 2; i < num; i++) {


arr[i] = arr[i - 2] + arr[i - 1];
}

for (int i = 0; i < num; i++) {


cout << "So thu " << i + 1 << ": " << arr[i] << endl;
}
}

// In cac so nguyen to nho hon n:


void primeNum() {
cout << "Nhap n: ";
int n;
cin >> n;

if (n <= 2) {
cout << "Khong ton tai so nao!";
} else {
cout << "Cac so nguyen to nho hon " << n << ": ";
for (int i = 2; i < n; i++) {
bool isPrime = true;
for (int j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
}
}
if (isPrime == true) {
cout << i << " ";
}
}
}
}

// Tim so thuan nghich:


void reversibleNum() {
cout << "Nhap so: ";
long n;
cin >> n;

string strNum = to_string(n);


int length = strNum.length();

char reverseStrNum[length];
int reverseIndex = length - 1;
for (int i = 0; i < length; i++) {
reverseStrNum[i] = strNum[reverseIndex];
reverseIndex--;
}

cout << "So dao nguoc: ";


for (int i = 0; i < length; i++) {
cout << reverseStrNum[i];
}
}

// Giai phuong trinh bac 2:


void giaiPtBac2() {
cout << "Nhap cac he so cua phuong trinh ax*x + bx + c = 0:" << endl;
cout << "Nhap a: ";
float a;
cin >> a;

cout << "Nhap b: ";


float b;
cin >> b;

cout << "Nhap c: ";


float c;
cin >> c;

float delta = b*b - 4*a*c;


if (delta < 0) {
cout << "Phuong trinh vo nghiem!";
} else if (delta == 0) {
cout << "Phuong trinh co nghiem kep x = " << -b/(2*a);
} else {
cout << "Phuong trinh co 2 nghiem phan biet:" << endl;
cout << "x1 = " << -b + (sqrt(delta)/(2*a)) << endl;
cout << "x2 = " << -b - (sqrt(delta)/(2*a)) << endl;
}
}

// Tinh tong cac chu so cua mot so nguyen:


void tongCacChuSo() {
cout << "Nhap so can tinh: ";
int n;
cin >> n;

string strNum = to_string(n);


int length = strNum.length();
int arr[length];
for (int i = 0; i < length; i++) {
int num = strNum[i] - '0';
arr[i] = num;
}

int sum = 0;
for (int i = 0; i < length; i++) {
sum += arr[i];
}

cout << "Tong cac chu so cua so " << n << ": ";
for (int i = 0; i < length - 1; i++) {
cout << arr[i] << "+";
}
cout << arr[length - 1] << " = " << sum;
}

int main()
{
tongCacChuSo();
return 0;
}

You might also like