/* Viet chuong trinh khai bao van hap vao mot mang gom 5 phan tu là so nguyen: 1.. Dem xem có bao nhieu phan tu có gtri = 3.[r]
(1)// Giai phuong trinh bac nhat mot an: ax + b = #include<iostream.h> int main() { int a,b; float x; cout<<"nhap so a = "; cin>>a; cout<<"nhap so b = "; cin>>b; if(a != 0) { x = (float)-b/a; cout<<"Phuong trinh co mot nghiem nhat x= "<<x<<endl; } else if (b != ) cout<<"Phuong trinh vo nghiem "<<endl; else cout<<"Phuong trinh co vo so nghiem"<<endl; } (2) /* Viet chuong trinh khai bao van hap vao mot mang gom phan tu là so nguyen: Dem xem có bao nhieu phan tu có gtri = Tinh tong cac phan tu co gia tri duong Tinh trung binh cong cua các phan tu chia het cho 5.*/ #include<iostream.h> int main() { int A[5]; int i; for (i=0;i<5;i++) { cout<<"nhap A["<<i<<"]="; cin>>A[i]; } int Dem=0; int Tong = 0; for (i=0;i<5;i++) { if (A[i]==3) Dem++; if(A[i]>0) Tong = Tong+A[i];} int Dem1=0; int Tong1=0; float Tbinh; for (i=0;i<5;i++) { if (A[i]%5==0) {Dem1++; Tong1 = Tong1+A[i]; Tbinh=(float)Tong1/Dem1;} } cout<<"so phan tu cho la: "<<Dem<<endl; cout<<"Tong cac so duong la: "<<Tong<<endl; cout<<"Trung binh cac so chia het cho la: "<<Tbinh<<endl; } (3) Nhap du lieu cho mang mot chieu gom phan tu In cac phan tu man hinh #include<iostream.h> int main() { int A[5]; // Nhap du lieu cho mang int i; for (i = 0; i<5; i++) { cout<<"Nhap A["<<i<<"] = "; cin>>A[i]; } // In cac phan tu cua mang man hinh cout<<"Mang da nhap: "; for (i = 0; i < 5; i++) cout<<A[i]<<" "; cout<<"\n \n"; } (4) Bai giai phuong trinh bac hai: ax^2+bx+c=0 #include<iostream.h> #include<math.h> using namespace std; void main() { float a,b,c,delta,x1,x2; cout<<"Nhap a: ";cin>>a; cout<<"Nhap b: ";cin>>b; cout<<"Nhap c: ";cin>>c; if(a==0) { if(b==0) if(c==0) cout<<"Pt co nghiem y\n"; else cout<<"Pt vo nghiem\n"; else cout<<"Pt co mot nghiem:x="<<-c/b<<"\n"; } else { delta=b*b-4*a*c; if(delta<0) cout<<"Pt vo nghiem\n"; else if(delta==0) cout<<"Pt co nghiem kep: x1=x2= "<<-b/(2*a)<<"\n"; else { x1=(-b-sqrt(delta))/(2*a); x2=(-b+sqrt(delta))/(2*a); cout<<"Pt co nghiem phan biet:\n"; cout<<"x1= "<<x1<<" va x2= "<<x2<<"\n"; } } } (5) Tinh tong hieu tich thuong hai so #include <stdio.h> #include <conio.h> main() { int a,b,tong,hieu,tich=0,thuong=0,i; printf("Nhap so a, b: ");scanf("%d%d",&a,&b); tong=a+b; printf("Tong so a, b: %d",tong); hieu=a-b; printf("\nHieu so a, b: %d",hieu); for(i=1;i<=b;i++) tich=tich+a; printf("\nTich so a, b: %d",tich); while(a>b) { a=a-b; thuong++; } printf("\nThuong so a, b: %d du %d",thuong,a); getch(); return 0; } (6) Cach 2: tinh tong hieu tich thuong cua hai so #include<iostream.h> int main() { int a,b,tong,hieu,tich; float thuong; cout<<"nhap so a = "; cin>>a; cout<<"nhap so b = "; cin>>b; tong = a + b; hieu = a - b; tich = a * b; thuong = (float) a / b; cout<<a<<" + "<<b<<" = "<<tong<<endl; cout<<a<<" - "<<b<<" = "<<hieu<<endl; cout<<a<<" * "<<b<<" = "<<tich<<endl; cout<<a<<" / "<<b<<" = "<<thuong<<endl; } (7) // Tinh tong so chan le C #include"conio.h" #include"iostream.h" #include"math.h" int nhap(int a[20], int n); int xuat(int a[20],int n); int tong(int a[20],int n); int tongle(int a[20],int n); int tongchan(int a[20],int n); int ktnt(int ); int tongnt(int a[20], int n); int tongknt(int a[20], int n); int main() { int a[20],n,h; // clrscr(); cout<<"nhap n phan tu: "; cin>>n; nhap(a,n); cout<<"mang vua nhap la:"<<endl; xuat(a,n); getch(); cout<<endl<<endl<<"tong cac ptu cua mang la :"<<" "<<tong(a,n); getch(); cout<<endl<<endl<<"tong cac ptu le cua mang la :"<<" "<<tongle(a,n); getch(); cout<<endl<<endl<<"tong cac ptu chan cua mang la :"<<" "<<tongchan(a,n); getch(); cout<<endl<<endl<<"tong cac ptu la so nguyen to cua mang la :"<<" "<<tongnt(a,n); getch(); cout<<endl<<endl<<"tong cac ptu ko la so nguyen to cua mang la :"<<" "<<tongknt(a,n); getch(); } int nhap(int a[20], int n) { for(int i=1;i<=n;i++) { cout<<"nhap phan tu: "<<i<<" = "; (8) cin>>a[i]; } } int xuat(int a[20],int n) { for(int i=1;i<=n;i++) { cout<<a[i]<<" "; } } int tong(int a[20],int n) { int s=0,i; for(i=1;i<=n;i++) s=s+a[i]; return s; } int tongle(int a[20],int n) { int s=0,i; for(i=1;i<=n;i++) if(a[i]%2==1) s=s+a[i]; return s; } int tongchan(int a[20],int n) { int s=0,i; for(i=1;i<=n;i++) if(a[i]%2==0) s=s+a[i]; return s; } int ktnt(int n) { int i,text=0,k; if(n>0) { i=1; (9) text=1; k=(int)sqrt(n); { i++; if(n%i==0) { text=0; break; } }while(i<=k); }return text; } int tongnt(int a[20],int n) { int s=0,i; for(i=1;i<=n;i++) if(ktnt(a[i])==1) s=s+a[i]; return s; } int tongknt(int a[20], int n) { int s=0,i; for(i=1;i<=n;i++) if(ktnt(a[i])==0) s=s+a[i]; return s; } Het (10) Hien so chan so le #include<iostream.h> int main() { int x; cin >> x; if (x%2==0) cout<<"la so chan"; else cout<<"so le"; } Bao gio #include <iostream.h> // da chay int main() { int h = 2; cout <<"chao cac em, hien tai la" << h <<"gio"; } Hien thi dong ki tu #include<iostream.h> //da chay #define begin{ int h=2; void writeString(char msg []) { cout<<msg; } int main() { writeString("xin chao cac em!"); } (11) tinh tuoi #include<iostream.h> int main() { int NamSinh, Tuoi; cout<<"Moi ban nhap nam sinh: "; cin>>NamSinh; Tuoi = 2014 - NamSinh; //gan nam sinh cout<<"Nam Ban da duoc"<<Tuoi<<" tuoi.\n"; } // bai thong bao mot so nhap vao la so am hay so duong #include<iostream.h> int main() { int a; cout<<"nhap so a = "; cin>>a; if (a > 0) cout<<a<<" la so duong."<<endl; else cout<<a<<" la so am."<<endl; } (12)