You are on page 1of 3

Bài tập C++ về nhập xuất dữ liệu

Bài 1: Nhập vào 1 ký tự, yêu cầu ra mã ASCII của ký tự đó


VD:

Input Output

A 65

#include <iostream>
using namespace std;
int main()
{
char m;
cin >> m;
cout << int(m);
return 0;
}

Bài 2: Nhập vào 1 số gồm 3 chữ số, yêu cầu xuất số đó theo thứ tự ngược lại
VD:

Input Output

123 321

#include <iostream>
using namespace std;
int main()
{
int a,n,s;
s = 0;
cin >> a;
while (a>0)
{
n = a%10;
a = a/10;
s = s*10+n;
};
cout << s;
return 0;
}

Bài 3: Nhập vào 1 số gồm 2 chữ số, yêu cầu xuất ra tổng chữ số của nó ra màn hình
VD:

Input Output

23 5

#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
cout << a%10 + a/10;
return 0;
}

Bài 4: Nhập vào 1 số thực, xuất số đó ra lấy chính xác 3 chữ số thập phân
VD:

Input Output

1.0123 1.012

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
float m;
cin >> m;
printf("%.3f",m); //để làm tròn số thì dùng lệnh này
return 0;
}

You might also like