1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; // Hàm chính. int main() { int n, i; do{ cout << "\n Nhap n = "; cin >> n; }while(n <0 && printf("\n Error: n >= 0")); int m = n; do{ i = n % 10; n = n / 10; }while(n != 0); cout << "\n Chu so dau tien cua " << m << " la: " << i << endl; system("pause"); return 0; } |
Thứ Ba, 8 tháng 12, 2015
Bài 49: Cho số nguyên dương n. Hãy tìm chữ số đầu tiên của n
Nhãn:
Xi-plus-plus
Bài 45: Hãy tính tích các chữ số của số nguyên dương n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; // Hàm chính. int main() { int n; do{ cout << "\n Nhap n = "; cin >> n; if(n < 0) { cout << "\n Vui long nhap n >= 0..."; } }while(n < 0); int T = 1; while(n) { T = T * (n % 10); n = n / 10; } cout << "\n T = " << T << endl; system("pause"); return 0; } |
Nhãn:
Xi-plus-plus
Thứ Tư, 2 tháng 12, 2015
03 Example Programs(Các chương trình ví dụ)
Program to convert A to B:
-
Program to add two numbers:
-
Program to swap two numbers:
Cách 1:
-
Cách 2:
-
Cách 3:
-
Cách 4:
-
Cách 5:
-
Program printing the name and age:
-
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> using namespace std; int main() { float A; float B; cout<<"Enter A = "; cin>> A; B = A * 3.14; cout<<"Result B = "<< B; return 0; } |
Program to add two numbers:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main() { int a, b, c; cout<<endl<<"Enter a and b: "; cin>> a>> b; c = a + b; // -, * , / with type float cout<<"Result c = "<< c; return 0; } |
Program to swap two numbers:
Cách 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int i, j, g; i = 4; j = 7; cout<<"values before swapping: "<<i<<" "<<j<<endl; g = i; i = j; j = g; cout<<"values after swapping: "<<i<<" "<<j; return 0; } |
Cách 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int i, j, g; i = 4; j = 7; cout<<"values before swapping: "<<i<<" "<<j<<endl; g = i * j; i = g / i; j = g / i; cout<<"values after swapping: "<<i<<" "<<j; return 0; } |
Cách 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int i, j, g; i = 4; j = 7; cout<<"values before swapping: "<<i<<" "<<j<<endl; i = i * j; // 4*7 j = i / j; // 4*7 / 7 = 4 i = i / j; // 4*7 / 4 = 7 cout<<"values after swapping: "<<i<<" "<<j; return 0; } |
Cách 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int i, j, g; i = 4; j = 7; cout<<"values before swapping: "<<i<<" "<<j<<endl; g = i + j; i = g - i; j = g - i; cout<<"values after swapping: "<<i<<" "<<j; return 0; } |
Cách 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int i, j, g; i = 4; j = 7; cout<<"values before swapping: "<<i<<" "<<j<<endl; i = i + j; //4+7 = 11 j = i - j; // 11 - 7 = 4 i = i - j; cout<<"values after swapping: "<<i<<" "<<j; return 0; } |
Program printing the name and age:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; int main() { char name[10]; int age; cout<<"Enter the name: "; cin>>name; cout<<"Enter the age: "; cin>>age; cout<<"The name of the person is "<<name<<" and the age is "<<age; return 0; } |
Nhãn:
Xi-plus-plus
Thứ Ba, 1 tháng 12, 2015
02 Data types(Các kiểu dữ liệu)
#include <iostream> using namespace std; int main() { //data integer int a; //data char char grade; //data float float b; cout<<"Input a: "; cin>> a; cout<<"Input the grade: "; cin>> grade; cout<<"Input b: "; cin>> b; cout<<"The value inside is: "<< a<<endl; cout<<"The grade is: "<< grade<<endl; cout<<"The value inside is: "<< b; return 0; }Phân tích:
Theo (Wiki) có 4 kiểu cơ bản của các biến trong C; đó là:
char
, int
, double
và float
.Tên kiểu | Ý nghĩa |
---|---|
char | Đơn vị cơ bản nhất có thể địa chỉ hóa được; nó là một byte. Đây là một kiểu nguyên. |
int | Loại số nguyên theo kích cỡ tự nhiên nhất của các máy tính. Thông thường nó có thể lấy trọn một khoảng có thể địa chỉ hoá được của một word với độ lớn biến thiên từ 16, 32, hay 64 bit tùy theo kiến trúc của CPU và hệ điều hành. |
float | Một giá trị dấu chấm động có độ chính xác đơn. |
double | Một giá trị dấu chấm động có độ chính xác kép. |
-----cin>> //nhập vào giống scanf("")
Data types:
1.char is 1 bytes
2.double is 8 bytes
3.short int is 2 bytes
4.{ } Is using the beginning and end of block command({ } là sử dụng mở đầu và kết thúc của khối lệnh)
5. ; ends most lines of C++ code(; kết thúc hầu hết các dòng code C++)
Phần xem thêm:
The following table tells you the basic data types in C++
Type | Keyword |
---|---|
Boolean | bool |
Character | char |
Integer | int |
Floating point | float |
Double floating point | double |
Valueless | void |
Wide character | wchar_t |
This modifiers can be used on data types for the size differentiation of particular data type
- signed
- unsigned
- short
- long
The following table shows the data type size.
Type | Typical Bit Width | Typical Range |
---|---|---|
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | Range | 0 to 65,535 |
signed short int | Range | -32768 to 32767 |
long int | 4bytes | -2,147,483,647 to 2,147,483,647 |
signed long int | 4bytes | same as long int |
unsigned long int | 4bytes | 0 to 4,294,967,295 |
float | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 bytes | 1 wide character |
This sizes may be different based upon your compiler.
Nhãn:
Xi-plus-plus
01 Introduction (Lời nói đầu)
Phân tích:#include <iostream> using namespace std; int main() { cout<<"Hello into the World!"<<endl; cout<<"-:- CNTTLAPTRINH -:-"; return 0; }
#include <iostream> //Khai báo thư viện using namespace std; //Sử dụng không gian tên std main() //Hàm chính cout<<"Hello into the World!"<<endl; //Trong đó cout<< lệnh xuất, endl dùng xuống dòng return //Trả về giá trị 0------cout<<"" như printf("") trong C
------endl như \n trong C
------ // Description 1 <== là miêu tả 1 dòng
------ /*Description 1
Description 2
*/ <== là miêu tả nhiều dòng
1.Any C++ program starts with main() function and it is mandatory to have it in your program(Bất kỳ chương trình C++ bắt đầu với chức năng main() và nó là bắt buộc phải có nó trong chương trình của bạn)
2.#include is a preprocessor directive(#include là một chỉ thị tiền xử lý)
3.The exact value to return to the operating system after successful completion of the program is 0(Các giá trị chính xác để trở về hệ điều hành sau khi hoàn thành thành công của chương trình là 0)
4.The only function all C programs must have as main()(Chức năng duy nhất tất cả C chương trình phải có là main())
3.The exact value to return to the operating system after successful completion of the program is 0(Các giá trị chính xác để trở về hệ điều hành sau khi hoàn thành thành công của chương trình là 0)
4.The only function all C programs must have as main()(Chức năng duy nhất tất cả C chương trình phải có là main())
Nhãn:
Xi-plus-plus