0% found this document useful (0 votes)
8 views9 pages

BTVN

Uploaded by

khanhtqn123
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)
8 views9 pages

BTVN

Uploaded by

khanhtqn123
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/ 9

Bài 12.

#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"1. Tong cac so nguyen tu 1 den 100."<<endl;
cout<<"2. Tong cac so nguyen tu 1 den n, voi n duoc nhao vao."<<endl;
cout<<"3. Xuat cac so chan nam trong [m,n], voi m, n duoc nhap vao."<<endl;
cout<<"Nhap so ban muon tinh: ";
cin>>a;
switch(a)
{
case 1:
{
int t;
for(int i=1;i<=100;i++)
{
t+=i;
}
cout<<"Tong cac so nguyen tu 1 den 100 la: "<<t;
break;
}
case 2:
{
int n, t=0;
cout<<"Nhap n: ";
cin>>n;
if (n<1)
{
cout<<"N phai lon hon 1";
}
else
{
for(int i=1;i<=n;i++)
{
t+=i;
}
cout<<"Tong cac so tu 1 den n: "<<t;
}
break;
}
case 3:
{
int m,n;
cout<<"Nhap m, n: ";
cin>>m>>n;
if(m>n)
{
cout<<"m phai nho hon n";
}
else
{
cout<<"Cac so chan tu m den n la: ";
for(int i=m;i<=n;i++)
{
if(i%2==0)
{
cout<<i<<" ";
}
}
}
break;
}
default:
cout<<"Lua chon khong hop le";
}
return 0;
}

Bài 14.
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"1. In tam giac trai"<<endl;
cout<<"2. In tam giac can"<<endl;
cout<<"3. In tam giac phai"<<endl;
cout<<"Chon tam giac muon in: ";
cin>>a;
switch (a)
{
case 1:
{
int h;
cout<<"Nhap so nguyen duong: ";
cin>>h;
if(h<0)
{
cout<<"Khong phai so nguyen duong";
}
else
{
for(int i=1;i<=h;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
}
break;
}
case 2:
{
int h;
cout<<"Nhap so nguyen duong: ";
cin>>h;
if(h<0)
{
cout<<"Khong phai so nguyen duong";
}
else
{
for (int i = 1; i <= h; i++)
{
for (int j = 1; j <= h - i; j++)
{
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++)
{
cout << "*";
}
cout << endl;
}
}
break;
}
case 3:
{
int h;
cout<<"Nhap so nguyen duong: ";
cin>>h;
if(h<0)
{
cout<<"Khong phai so nguyen duong";
}
else
{
for(int i=1;i<=h;i++)
{
for(int j=i;j<=h;j++)
{
cout<<"*";
}
cout<<endl;
}
}
break;
}
default:
cout<<"Khong co lua chon nay";

}
return 0;
}

Bài 21.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a;
cout<<"1. Tinh bieu thuc 1"<<endl;
cout<<"2. Tinh bieu thuc 2"<<endl;
cout<<"Chon bieu thuc muon tinh: ";
cin>>a;
switch(a)
{
case 1:
{
int n;
double s=0;
cout<<"Nhap n: ";
cin>>n;
if(n<0)
{
cout<<"Khong hop le";
}
else
{
for(int i=1;i<=n;i++)
{
s=sqrt(i+s);
}
cout<<"Ket qua cua bieu thuc: "<<s;
}
break;
}
case 2:
{
int n;
double s=0;
cout<<"Nhap n: ";
cin>>n;
if(n<0)
{
cout<<"Khong hop le";
}
else
{
for(int i=n;i>=1;i--)
{
s=sqrt(i+s);
}
cout<<"Ket qua cua bieu thuc: "<<s;
}
break;

}
default:
cout<<"Lua chon khong hop le";
}
return 0;
}

Bài 22.
#include <iostream>
using namespace std;
int main()
{
int t=0;
for(int i=1;i<=20;i++)
{
t+=i;
}
cout<<"Tong tu 1 den 20 la: "<<t;
return 0;
}

Bài 23.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Nhap n: ";
cin>>n;
if(n>0)
{
int t=1;
for(int i=1;i<=n;i++)
{
t*=i;
}
cout<<"Tich cac so tu 1 den n la: "<<t;
}
else
{
cout<<"Khong hop le";
}
return 0;
}

Bài 24.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Nhap n: ";
cin>>n;
if(n>0)
{
int t=0;
for(int i=2;i<=n;i+=2)
{
t+=i;
}
cout<<"Tong t: "<<t;
}
else
{
cout<<"Khong hop le";
}
return 0;
}

Bài 25.
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"1. Tinh bieu thuc 1"<<endl;
cout<<"2. Tinh bieu thuc 2"<<endl;
cout<<"Chon bieu thuc: ";
cin>>a;
switch (a)
{
case 1:
{
int n;
cout<<"Nhap n: ";
cin>>n;
if(n>0)
{
double t;
for(double i=2;i<=n;i++)
{
t+=1.0/i;
}
cout<<"Tong t: "<<t;
}
else
{
cout<<"Khong phai so nguyen duong";
}
break;
}
case 2:
{
int n;
cout<<"Nhap n: ";
cin>>n;
if(n>0)
{
double t;
for(double i=1;i<=n;i++)
{
t+=(i/(i+1));
}
cout<<"Ket qua bieu thuc: "<<t;
}
}
break;
default:
cout<<"Lua chon khong hop le";
}
return 0;
}

You might also like