By GokiSoft.com|
20:08 09/12/2021|
Lập Trình C
Ôn tập While, Do .. While, For lập trình C - Lập trình C
Sử dụng bằng 3 cách cho bài sau : sử dụng while, do .. while, for
Bài 1: Tính S(n) = 1 + 2 + 3 + … + n
Bài 2: Tính S(n) = 1^2 + 2^2 + … + n^2
Bài 3: Tính S(n) = 1 + ½ + 1/3 + … + 1/n
Bài 4: Tính S(n) = ½ + ¼ + … + 1/2n
Bài 5: Tính S(n) = 1 + 1/3 + 1/5 + … + 1/(2n + 1)
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Âu Duy Quyền [c_online]](https://www.gravatar.com/avatar/90ca555110a54024dd82b80fe75cf3ae.jpg?s=80&d=mm&r=g)
Âu Duy Quyền
2022-10-04 03:30:40
#include <stdio.h>
#include <conio.h>
int main()
{
float n;
printf("Nhap n = ");
scanf("%f", &n);
/*Bai1: Tính S(n) = 1 + 2 + 3 + … + n
int i, S;
i=0;
S=0;
while(i<=n) {
S=S+i;
i++;
}
*/
/*Bai2: Tính S(n) = 1^2 + 2^2 + 3^2 + … + n^2
int i, S;
i=0;
S=0;
do {
S=S+i*i;
i++;
} while(i<=n);
*/
/*Bai3: Tính S(n) = 1 + 1/2 + 1/3 + … + 1/n
float i, S;
S=0;
i=1;
for(;i<=n;i++) {
S= S + 1/i;
}
*/
/*Bai4: Tính S(n) = 1/2 + 1/4 + … + 1/2n
float i, S;
S=0;
i=1;
for(;i<=n;i++) {
S= S + (1/(2*i));
}
*/
/*Bai5: Tính S(n) = 1 + 1/3 + 1/5 + … + 1/(2n+1)*/
float i, S;
S=1;
i=1;
while(i<=n) {
S= S + (1/(2*i+1));
i++;
}
printf("\nS = %0.1f",S);
return 0;
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2020-09-17 12:09:40
//bai 1 while:
/*int n, i, S;
printf("Nhap n = "); scanf("%d", &n);
i=1;
S=0;
while (i<=n){
S=S+i;
i++;}
printf("Tong S = %d",S);*/
//Bai 2 c2
/*int i, n, S;
printf("Nhap n = "); scanf("%d",&n);
S=0;
for (i=0;i<=n;i++)
{S=S+i*i;}
printf("Tong S = %d",S);*/
//Bai 3 c2
/*float i, n, S;
printf("Nhap n = "); scanf("%f",&n);
i=1;
S=0;
do {S=S+1/i;
i++;}
while (i<=n);
printf("Tong S = %f",S);*/
//bai 4 c1
/*float i, n, S;
printf("Nhap n = "); scanf("%f", &n);
i=1;
S=0;
while (i<=n)
{S=S+(1/(2*i));
i++;}
printf("Tong S = %f",S);*/
//Bai 5 for
float i, n, S;
printf("Nhap n = "); scanf("%f",&n);
S=0;
for(i=1;i<=n;i++){
S=S+(1/(2*i+1));}
printf ("Tong S = %f",S);
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-09-16 07:22:43
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n, i ,s = 0;
printf("\nNhap n: ");
scanf("%d", &n);
while(i<=n){
s = s + i;
printf("\nTong s: %d", s);
}
do {
s = s + 1;
printf("\nTong s: %d", s);
i++;
}
for(i = 1; i <= n; i++){
s+ = i;
printf("\nTong s: %d", s);
}
return 0;
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2020-09-15 14:50:39
bai 2
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n;
printf("\nnhap n: "); scanf("%d",&n);
printf("\n==============cach 1");
int S1;
for(S1=0;S1<2;S1++){
S1=(n*(n+1)*(2*n+1))/6;
printf("\n tong la %d",S1);
}
printf("\n==============cach 2");
int S2=0;
do {
S2=(n*(n+1)*(2*n+1))/6;
} while(S2<2);
printf("\ntong la: %d",S2);
printf("\n==============cach 3");
int S3=0;
while(S3<2) {
S3=(n*(n+1)*(2*n+1))/6;
printf("\ntong la: %d",S3);
}
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-15 13:56:06
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//bat dau code;
//bai 1;
int n;
int i;
int s = 0;
printf("\nNhap n: ");
scanf("%d", &n);
while(i<=n) {
s = s + i;
printf("\nTong s: %d", s);
}
do {
s = s + 1;
printf("\nTong s: %d", s);
i++;
}
for(i = 1; i <= n; i++){
s+=i;
printf("nTong s: %d", s);
}
//ket thuc code;
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-15 13:35:37
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//bat dau code;
//bai 3;
int S = 0;
int i = 1;
int n;
printf("\nNhap n =");
scanf("%d", &n);
for(i=1;i<=n;i++) {
tong = tong + 1 / i;
}
printf("Sn = %f", tong);
while(i<=n) {
tong = tong + 1 / i;
i++;
}
printf("Sn = %f", tong);
do {
tong = tong + 1 / i;
i++;
}
printf("Sn = %f", tong);
//ket thuc code;
return 0;
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-09-15 13:27:31
#include<stdio.h>
#include<stdlib.h>
int main(){
//bat dau code;
//bai 2;
float s=0, c=0, n;
printf("\nNhap n: ");
scanf("%f",&n);
while (c <= n ){
s= s +c*c;
c++;
}
printf("\nS(n)= %f",s);
s=0;
c=0;
do{
s+=c*c;
c++;
}while(c<= (n) );
printf("\nS(n)= %f",s);
s=0;
for (int d=0; d <= n; d++){
s+=d*d;
}
printf("\nS(n)= %f",s);
//ket thuc code;
return 0;
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2020-09-15 12:21:02
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bai4
int main(int argc, char *argv[]) {
int i = 1, n;
float s;
s = 0;
printf("\nnhap so n:");
scanf("%d", &n);
//for
for(i = 1; i <= n; i++);
{
s = s + 1.0 / (2 * i);
i++;
}
printf("Tong s:%f", s);
//do...while
do
{
s = s + 1.0 / (2 * i);
i++;
}
while(i <= 1);
{
printf("Tong s:%f", s);
}
return 0;
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2020-09-15 10:10:49
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bai3
int main(int argc, char *argv[]) {
int i, n;
float s;
i = 1;
s = 0;
printf("\nnhap so n:");
scanf("%d", &n);
///while
while(i <= n)
{
s = s + (1/i);
i++;
}
printf("\nTong s:%f", &s);
//Do..while
do
{
s = s + 1/i;
i++;
}
while(i <= n);
{
printf("\nTong s:%f", &s);
}
for(i = 1; i <= 1; i++);
{
s = s + 1/i;
i++;
}
printf("\nTong s:%f", &s);
return 0;
}
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
2020-09-15 09:42:54
//bai 5
float n, i, s5;
printf("Nhap n: ");
scanf("%f",&n);
//sd for
s5=0;
for(i=0; i<=n; i++){
s5 += (1/(i*2+1));
}
printf("s5 = %f",s5);
//sd while
s5=0;
i=0;
while(i<=n){
s5+= (1/(i*2+1));
i++;
}
printf("s5 = %f",s5);
//sd do...while
s5=0;
i=0;
do{
s5+=(1/(i*2+1));
i++;
}while(i<=n);
printf("s5 = %f",s5);