0% found this document useful (0 votes)
44 views11 pages

BT th1

The document contains C++ code snippets demonstrating various programming concepts: 1) Printing a greeting message 2) Looping through the alphabet and printing ASCII values 3) User input, arithmetic operations, and conditional statements on two numbers 4) Summing numbers from 1 to 10 with a for loop 3) Calculating powers with a for loop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views11 pages

BT th1

The document contains C++ code snippets demonstrating various programming concepts: 1) Printing a greeting message 2) Looping through the alphabet and printing ASCII values 3) User input, arithmetic operations, and conditional statements on two numbers 4) Summing numbers from 1 to 10 with a for loop 3) Calculating powers with a for loop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

#include <iostream>
using namespace std;

int main()
{
cout << "xin chao cac ban !";
}
2.
#include <iostream>
using namespace std;

int main()
{
for (char i = 'a'; i <= 'z'; i++)
{
cout << "Ma ASCII cua " << i << " la : " << (int)i << "\n";
}
}
3.
#include <iostream>
using namespace std;

int main()
{
int x, y;
cout << "nhap so x: ";
cin >> x ;
cout << "nhap so y: ";
cin >> y ;
if (y == 0)
{
do {
cout << "nhap lai y ";
cin >> y;
} while (y <= 0);
}

cout << "tong x va y la : " << x + y;


cout << "\nhieu x va y la : " << x - y;
cout << "\ntich x va y la : " << x * y;
if (x > y)
{
cout << "\nx lon hon y";
}
else if(x < y)
{
cout << "\nx nho hon y";
}
if (x == y)
{
cout << "\nx bang y";
}
}
4.
#include <iostream>
using namespace std;
int main()
{
int t = 0;
for (int i = 1; i <= 10; i++)
{
t += i;
}
cout << "tong 1 den 10 = " << t;
}
5.
#include <iostream>
using namespace std;

int main()
{
int x, y , tich = 1;
cout << "nhap x : ";
cin >> x ;
cout << "nhap y : ";
cin >> y ;
for (int i = 1; i <= y; i++)
{
tich *= x;
}
cout << x << "^" << y << " = " << tich;
}
6.
#include <iostream>
#include <string>;
using namespace std;

int sotutrongxau(string s)
{
int len = s.length();
int dem = (s[0] != ' ');
for (int i = 0; i < len - 1; i++) {
if (s[i] == ' ' && s[i + 1] != ' ') {
dem++;
}
}
return dem;
}

bool doixung(string s)
{
int len = s.length();
for (int i = 0; i < len / 2; i++)
{
if (s[i] != s[len - i - 1])
return false;
}
return true;
}

int main()
{
string s;
cout << "nhap chuoi ::";
getline(cin, s);
cout << "so tu trong xau la :: " << sotutrongxau(s);
if (doixung(s))
{
cout << "\nxau doi xung ";
}
else
{
cout << "\nxau ko doi xung";
}
}
7.
#include <iostream>
using namespace std;

int kiemtrafibolaci(int n) {
int a1 = 1, a2 = 1, a = 2 ;
while (a < n) {
a = a1 + a2;
a1 = a2;
a2 = a;
}
return a;
}

int main() {
int n;
cout << "nhap 1 so :: ";
cin >> n;
if (n == 1) {
cout << "1 la so fibolaci";
}
if (kiemtrafibolaci(n) == n) {
cout << n << " la so fibolaci";
}
else if(n>1){
cout << n << " ko la so fibolaci";
}
}
10.
#include <iostream>
using namespace std;

void vuonggocphaiduoi(int n , char a)


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i + 1; j++)
{
cout << a;
}
cout << "\n";
}
}

void vuonggocphaitren(int n , char a)


{
for (int i = 0; i < n; i++)
{
for (int j = n; j > i; j--)
{
cout << a;
}
cout << "\n";
}
}

void vuonggoctraitren(int n, char a)


{
for (int i = 0; i < n; i++)
{
for (int y = 0; y < i+1; y++)
{

cout << " ";


}
for (int j = n; j > i; j--) {
cout << a;
}
cout << "\n";
}
}

void vuonggoctraiduoi(int n, char a) {


for (int i = 0; i < n ; i++)
{
for (int y = n; y > i; y--)
{
cout << " ";
}
for (int j = 0; j < i+1; j++)
{
cout << a;
}
cout << "\n";
}
}

int main() {
int n;
cout << "nhap n : ";
cin >> n;
char a = '*';
vuonggocphaiduoi(n,a);
cout << "\n";
vuonggocphaitren(n,a);
cout << "\n";
vuonggoctraitren(n,a);
cout << "\n";
vuonggoctraiduoi(n,a);
}
11.
#include <iostream>
using namespace std;

void nhap(int* a, int n)


{
cout << "nhap gia tri cho mang : \n";
for (int i = 0; i < n; i++)
{
cout << "a[" <<i<<"] = ";
cin >> a[i];
}
}

int max(int* a, int n)


{
int max = a[0];
for (int i = 0; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
return max;
}

void min(int* a, int n)


{
int min = a[0];
for (int i = 0; i < n; i++)
{
if (a[i] < min)
{
min = a[i];
}
}
cout << "min = " << min;
}

int tong(int* a, int n)


{
int tong = 0;
for (int i = 0; i < n; i++)
{
tong += a[i];
}
return tong;
}

int main()
{
int n;
cout << "nhap so phan tu cua mang : ";
cin >> n;

int* a = new int[n];


nhap(a, n);
cout << "max = " << max(a, n)<<endl;
min(a, n);
cout << "\n tong = " << tong(a, n);
}

You might also like