19/5/2011, 21:59
Bai 2 trang 44
Bai 5 trang 45
- Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class BAC1
{
int a,b;
public:
void nhap();
void tinh();
};
void BAC1::nhap()
{
cout<<" Moi nhap cac he so cua pt bac nhat"<<endl;
cout<<" a = ";cin>>a;
cout<<" b = ";cin>>b;
}
void BAC1::tinh()
{
if(a==0)
{
if (b==0) cout<<" pt co vo so nghiem";
else cout<<" pt vo nghiem ";
}
else
cout<<" pt co nghiem la "<<(float)-b/a;
}
void main()
{
clrscr();
BAC1 pt1;
pt1.nhap();
pt1.tinh();
getch();
}
Bai 5 trang 45
- Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class SNTDW
{
int n,i,j,tong,dem;
public:
void nhap();
void tinh();
};
void SNTDW::nhap()
{
cout<<"nhap 1 so >3"<<endl;
cout<<"vi 1,2,3 la so nguyen to roi ma";
cin>>n;
}
void SNTDW::tinh()
{
tong=6;dem=3;
//tong=6 vi 1+2+3=6 va 1,2,3 la 3 so nguyen to
for( i=4;i<=n;i++)
{
j=2;
while ((j<i)&&(i%j!=0)) j++;
if(j==i)
{
tong+=i;
dem++;
}
}
cout<<"co "<<dem<<" so nguyen to thuoc doan [1..n]";
cout<<" tong cac so do la:"<<tong;
}
void main()
{
clrscr();
SNTDW a;
a.nhap();
a.tinh();
getch();
}