Tải bản đầy đủ (.docx) (24 trang)

các bài tập có lời giải trong c++

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (112.47 KB, 24 trang )

Bài 1 Viết chương trình C++ nhập 10 giá trị nguyên, sau đó tìm giá trị
lớn nhất, nhỏ nhất, tìm giá trị có tần suất xuất hiện nhiều nhất, sắp
xếp mảng theo thứ tự tăng dần, giảm dần và hiển thị kết quả.
lời giải
#include<iostream>
#include<conio.h>
using namespace std;
int *findMax(int arr[],int n);
int main(){
int n,i,*p;
cout<<"Nhap so du lieu: ";
cin>>n;
int arr[n];
for(i=0;i
{

cout<<"Nhap gia tri thu "<cin>>arr[i];
}
p=findMax(arr,n);
cout<<"Gia tri lon nhat la: "<<*p;
getch();
return 0;
}

int *findMax(int data[],int n){
int *max=data;
int i;
for(i=1;iif(*max<*(max+i)) *max=*(max+i);


}
return max;
}


Bài 2 Viết một chương trình C++ để nhận 5 giá trị nguyên từ bàn phím. 5
giá trị này sẽ được lưu trữ trong một mảng bởi sử dụng một con trỏ. Sau
đó, in các phần tử của mảng trên màn hình.
lời giải
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int arr[5],i;
int *p=arr;
cout<<"Nhap 5 so:";
cin>>*p>>*(p+1)>>*(p+2)>>*(p+3)>>*(p+4);
cout<<"Cac so ban vua nhap la:\n";
for(i=0;i<5;i++)
cout<
return 0;

}


Bài 3 Viết chương trình C++ để in ra màn hình số kế tiếp của số nguyên
bạn vừa nhập.

#include <iostream>
#include <conio.h>
using namespace std;
void value(int);

int main()
{

int x;
cout << "Nhap mot so nguyen : ";
cin>>x;
cout << "So ngay sau cua " << x << " la ";
value(x);
return 0;
}
void value(int x)
{
x++;
cout << x << "." << endl;
}


Bài 4 Viết chương trình C++ để nhắc người dùng chọn một tùy chọn. Sau
đó nhắc họ nhập hai số nguyên và in kết quả tương ứng với tùy chọn đó.
#include <cstdlib>
#include <iostream>
#include<iomanip>

using namespace std;


void displaymenu(){
cout<<"==================================================="<<"\n";
cout<<"

MENU

"<<"\n";

cout<<"==================================================="<<"\n";
cout<<"

1. Cong"<<"\n";

cout<<"

2. Tru"<<"\n";

cout<<"

3. Nhan"<<"\n";

cout<<"

4. Chia"<<"\n";

cout<<"

5. Chia lay phan du"<<"\n";

}

int Cong(int a,int b){
return(a+b);
}

int Tru(int a, int b){
return(a-b);
}

int Nhan(int a, int b){
return(a*b);
}
float Chia(int a,int b){
return(a/b);
}


int Phandu(int a, int b){
return(a%b);
}

int main(int argc, char *argv[])
{
//hien thi menu
displaymenu();
int luachon;
int a;
int b;
char confirm;
do
{

cout<<"Nhap lua chon cua ban (1-5):";
cin>>luachon;
cout<<"Nhap hai so nguyen:";
cin>>a>>b;
cout<<"\n";
switch(luachon){
case 1:cout<<"Ket qua:"<case 2:cout<<"Ket qua:"<case 3:cout<<"Ket qua:"<case 4:cout<<"Ket qua:"<case 5:cout<<"Ket qua:"<default:cout<<"Khong hop le";
}

cout<<"\nNhan y hoac Y de tiep tuc:";
cin>>confirm;
}while(confirm=='y'||confirm=='Y');

return 0;


}

Bài 5 Viết chương trình C++ để in một bảng nhân như sau:
1*1=1
1+2=2
…….
1*9=9

2*1=2

2*2=4
…….
2*8=18

3*1=3
3*2=6
…….
3*9=27

lời giải
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

void bangnhan(){
int i,j;
for(i=1;i<10;i++){
for(j=1;j<10;j++)
cout<cout<<"\n";}
}
int main(int argc, char *argv[])
{
bangnhan();

return 0;
}


…… 9*1=1
…… 9*2=18
…… …….
…… 9*9=81


Bài 6 Tìm số lớn nhất nhỏ nhất
Viết chương trình C++ nhắc người dùng nhập 3 số nguyên và sau đó in
các số nhỏ nhất và lớn nhất bởi sử dụng hàm trong C++.
lời giải
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int giatrimax(int a,int b,int c){
if(a>b){
if(a>c) return(a);
else return(c);}
else if(b>c) return(b);
else return(c);

}
int giatrimin(int a,int b,int c){
if(aif(aelse return(c);}

else if(belse return(c);

}
int main(int argc, char *argv[])
{
int a,b,c;
cout<<"Nhap ba so nguyen:";


cin>>a>>b>>c;
cout<<"Gia tri lon nhat="<cout<<"Gia tri nho nhat="<cout<<"\n";

return 0;
}

Bài 7 Viết chương trình C++ để tính giai thừa của một số nguyên dương
bởi sử dụng hàm trong C++.
lời giải
#include<iostream>
#include<conio.h>

using namespace std;

//ham
long giaithua(int);

int main()

{

// Khai bao bien
int counter, n;

// Nhap gia tri
cout<<"Nhap so :";
cin>>n;

// Goi ham giaithua
cout<<"Gia tri giai thua cua " <

return 0;
}

// Ham giaithua
long giaithua(int n)
{
int counter;
long fact = 1;

//vong lap for
for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}

return fact;
}


Bài 8 Sử dụng nạp chồng hàm để xếp thứ tự 10 số nguyên, hoặc 10 giá trị
long hoặc 10 giá trị double trong cùng một chương trình C++.
lời giải
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

void hamsapxep(int danhsach[]){

int i,j;
int temp;


for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(danhsach[j]>danhsach[j+1]){
temp=danhsach[j];
danhsach[j]=danhsach[j+1];
danhsach[j+1]=temp;
}

for(i=0;i<10;i++)cout<
}

void hamsapxep(float danhsach[]){


int i,j;
float temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(danhsach[j]>danhsach[j+1]){
temp=danhsach[j];
danhsach[j]=danhsach[j+1];
danhsach[j+1]=temp;
}

for(i=0;i<10;i++)cout<
}
void hamsapxep(double danhsach[]){

int i,j;
double temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)


if(danhsach[j]>danhsach[j+1]){
temp=danhsach[j];
danhsach[j]=danhsach[j+1];
danhsach[j+1]=temp;
}

for(i=0;i<10;i++)cout<

}

int main(int argc, char *argv[])
{
int danhsach[]={11,2,15,14,6,22,26,32,9,20};
hamsapxep(danhsach);

return 0;
}

Bài 9 Sử dụng khái niệm truyền mảng tới hàm để tính giá trị trung bình của
các giá trị nguyên được nhập từ bàn phím. Các giá trị nguyên được lưu trữ
trong mảng một chiều và được truyền tới hàm có tên là avg.
lời giải
#include <cstdlib>
#include <iostream>
using namespace std;

double avg(int arr[],int size){ //khong xac dinh kich co mang giua hai dau [ va ]
int sum=0;
double r;
for(int i=0;isum+=arr[i];
r=(double)sum/size;
return(r);


}

int main(){

int n;
cout<<"Nhap so cac so nguyen:";
cin>>n;
int arr[n];
for(int i=0;icout<<"Gia tri "<cin>>arr[i];
}

cout<<"Gia tri trung binh="<
return 0;
}

Bài 10 Viết chương trình C++ sử dụng khái niệm hàm đệ qui để tính tích
sau:
Sn= 11 * 22 * 33 * 44 *...* n
Lời giải
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
double bieuthuc(int);

int main(int argc, char *argv[])
{


int n;
cout<<"Nhap n bang: ";

cin>>n;
cout<<"Ket qua: "<
return 0;
}
double bieuthuc(int n){
if(n==1) return 1; //co so co so
else return(pow((float)n,n)* bieuthuc(n-1)); //co so chung
}

Bài 11: Viết một chương trình C++ để nhập hai giá trị và tính tích và
lập phương của chúng bởi sử dụng khái niệm inline function trong
C++.
lời giải
#include<iostream>
#include<conio.h>
using namespace std;

class line
{
public:
inline float phepnhan(float x,float y)
{
return(x*y);
}
inline float lapphuong(float x)
{
return(x*x*x);
}
};



int main()
{
line obj;
float giatri1,giatri2;

cout<<"Nhap hai gia tri:";
cin>>giatri1>>giatri2;
cout<<"\nGia tri cua phep tinh tich la: "<cout<<"\n\nGia tri cua phep tinh lap phuong la:
"<return 0;
}

Bài 12 in hang dọc trong c++
#include<iostream>
using namespace std;
int main(){
int n,a;
cout<<"Nhap so luong phan tu ";
cin>>n;
int mangSL[n];
for(int i=0;iint m;
cout<<"Gia tri phan tu thu "<cin>>m;
mangSL[i]=m;
}
int max=mangSL[0];

for(int y=0;yif(max<=mangSL[y]){
max=mangSL[y];
}
}
a=max;
int k=1;
for(int i=0;iif(k<=a){
for(int y=0;yif(k<=mangSL[y]){
cout<<"*";
}else{
cout<<" ";


}
}
k=k+1;
}
cout<<"\n";
}
return 0;
}

Bài 12 in hình chữ nhật trong c++

#include<iostream>
using namespace std;
int main(){

int n,m;
do{
cout<<"Nhap chieu dai ";
cin>>n;
if(n<0){
cout<<"Nhap sai !\n";
}
}while(n<0);
do{
cout<<"Nhap chieu rong ";
cin>>m;
if((m<0)||(m>n)){
cout<<"Nhap sai !\n";
}
}while((m<0)||(m>n));
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if((i==1)||(i==m)){
cout<<"* ";
}else{
if((j==1)||(j==n))
{
cout<<"* ";
}else{
cout<<" ";
}
}
}
cout<<"\n";
}



return 0;
}


Bài 13 in ra các nguyên tố trong c++
#include<iostream>
using namespace std;
int

soNT(int

a);

int main(){
int n;
do{
cout<<"nhap n ";
cin>>n;
if(n<0){
cout<<"Nhap n > 0 ";
}
}while(n<0);
int
do{

a=0,b=n,dem=0;

b=b/10;

dem=dem+1;
}while(b>0);
for(int i=1;i<=dem;i++){
a=n%10;
n=n/10;
if(soNT(a)==1){
cout<}
}
return 0;
}
int soNT(int a){
int
bien=0;
if(a!=1){
for(int j=2;
jif(a%j==0){
bien=bien+1;
}
}
if(bien!=0){
return 0;
}else{
return 1;
}
}else{
return 0;
}
}



Bài tập 14 : tính diện tích và thể tích trong c ++
lời giải
#include<iostream>
using namespace std;
class

Box{
public:
static int
bienDemDT;
Box(double
dai,
double rong, double cao):
chieuDai(dai), chieuRong(rong),
chieuCao(cao){
cout<<"Khoi tao ham dung! \n";
bienDemDT++;
}
void
}
void
}
void

setChieuDai(double
chieuDai=dai;

dai){


setChieuRong(double rong){
chieuRong=rong;
setChieuCao(double
chieuCao=cao;

cao){

}
double theTich(void){
return chieuDai*chieuRong*chieuCao;
}
friend void
inChieuRong(Box
box1){
cout<<"Chieu rong cua hinh la
"<}
static double layBienDemDT(void){
return bienDemDT;
}
protected:
double chieuDai;
double chieuRong;
double chieuCao;
};
int

Box::bienDemDT=0;


/*class tinhCP{
public:
double tinhChiPhi(int dienTich){
return dienTich*20;
}


};
class

dienTich:
public tinhCP, public Box{
public:
double tinhDT(void){
return chieuDai*chieuRong;
}

};
*/
int main(){
cout<<"so lan khoi tao ham dung "<Box
box1(1,2,3);
cout<<"so lan khoi tao ham dung "<inChieuRong(box1);
/*

dienTich

dt;


dt.setChieuDai(2);
dt.setChieuRong(3);
dt.setChieuCao(4);
cout<<"the tich cua hinh la "<cout<<"Dien tich cua hinh la "<int dienTich=0;
dienTich=dt.tinhDT();
cout<<"Chi phi son la "<*/
return 0;
}

Bài tập 15 : bài tập về nhập số in time
lời giải
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
int main(){
int hour,n,minute,second;
string a;
cout<<"Nhap n ";


cin>>n;
hour=n/3600;
minute=(n-hour*3600)/60;
second=(n-(minute*60)-(hour*3600));
if(n>=43200){

a=" PM ";
}else{
a=" AM ";
}
cout<return 0;
}

Bài 16 : bài tập tính kế thừa
lời giải
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class

nhapThongTin{
protected:
int
string

diemkt1,
mssv;

public:
void
nhapTTSV(){
cout<<"nhap ma sinh vien ";
getline(cin,mssv);
cout<<"Nhap diem mon 1 ";

cin>>diemkt1;
cout<<"nhap diem mon 2 ";
cin>>diemkt2;
}
};
class

};

nhapDiemTC{
protected:
int diemTC;
public:
void
nhapDiemGDTC()
{
cout<<"nhap diem the chat ";
cin>>diemTC;
}

diemkt2;


class

sinhVien:public
nhapThongTin, public nhapDiemTC{
private:
int
tongDiem;

double diemTB;
public:
void
display(){
tongDiem=diemTC+diemkt1+diemkt2;
diemTB=tongDiem/3;
cout<<"Ma so sinh vien "<cout<<"Tong diem la "<cout<<"Diem trung binh la "<}

};

int main(){
sinhVien

obj;

obj.nhapTTSV();
obj.nhapDiemGDTC();
obj.display();
return 0;

return 0;
}

Bài 17 : bài tập về cặp số hữu nghị
lời giải
#include<iostream>
#include<cmath>

using namespace std;
int

uocSN(int a);

int main(){
int
do{

n,a,b;

cout<<"Nhap so luong cac chu so can xet : ";cin>>n;
if(n<1){
system("cls");
}
}while(n<1);


a=pow(10,(n-1)),

b=pow(10,n)-1;

cout<<"Cac cap so huu nghi la: ";
for(int i=a;i<=b;i++){
for(int j=(i+1);j<=b;j++){
if((uocSN(i)==j)&&(uocSN(j)==i)){
cout<}
}
}

return 0;
}
int

uocSN(int
a){
int
n=0;
for(int i=1;iif(a%i==0){
n=n+i;
}
}
return n;

}

Bài 18 : chuỗi palidrcome
lời giải
#include<string>
#include<iomanip>
#include<iostream>
using namespace std;
int main(){
string str;
getline(cin,str);
int a,b=0,c,d,e=0,f,g,h;
a=str.length();
cout<f=a/2-1;

for(int i=0;iif(str[i]!=' '){
b=b+1;
}
}
cout<h=b/2;


if(a%2==0){
for(int y=0;yd=a-(y+1);
if(str[y]!=' '){
if(str[d]==' '){
d=d-1;
if(str[y]==str[d]){
e=e+1;
}
}
else{
if(str[y]==str[d]){
e=e+1;
}
}
}else{
g=y+1;
if(str[d]==' '){
d=d-1;
if(str[g]==str[d]){
e=e+1;

}
}
else{
if(str[g]==str[d]){
e=e+1;
}
}
}
}
}
else{
for(int y=0;y<=f;y++){
d=a-(y+1);
if(str[y]!=' '){
if(str[d]==' '){
d=d-1;
if(str[y]==str[d]){
e=e+1;
}
}
else{
if(str[y]==str[d]){
e=e+1;
}
}
}else{
g=y+1;
if(str[d]==' '){
d=d-1;
if(str[g]==str[d]){

e=e+1;
}


}
else{
if(str[g]==str[d]){
e=e+1;
}
}
}
}
}
if(b%2==0){
if(e==b/2){
cout<<"Day
}else{
cout<<"Day
}
}
if(b%2!=0){
if(e==h){
cout<<"Day
}else{
cout<<"Day
}
}
return 0;
}


la chuoi Palidrome.\n";
khong phai chuoi Palidrome.\n";

la chuoi Palidrome.\n";
khong phai chuoi Palidrome.\n";



×