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)
![An Văn Minh [T2008A]](https://www.gravatar.com/avatar/e0f14efe4b11f7d9d5901e8802319c92.jpg?s=80&d=mm&r=g)
An Văn Minh
2020-09-15 03:13:09
#include<stdio.h>
#include<math.h>
int main()
{
int i=1;
int n;
int s=0;
printf("\nNhap n=");
scanf("%d", &n);
//dung while
while(i<=n){
s=s+i;
i++;
}
//dung do...while
do{
s=s+i;
i++;
}
while(i<=n);
//dung for
for(int i=1;i<=n;i++){
s=s+i;
}
printf("\nTong S(n)=%d", s);
return 0;
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2020-09-15 03:11:26
#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[]) {
//Bài 3: Tính S(n) = 1 + ½ + 1/3 + … + 1/n
//cah 1 for
float n , t ,i;
t=0 ;
i=1;
printf(" nhap so n = ");
scanf("%f",&n);
for (i=1;i<=n;i++) {
t=t+(1/i);
printf("\n so n la: %f",t);
}
//cach 2 while
float n , t ,i;
t=0 ;
i=1;
printf(" nhap so n = ");
scanf("%f",&n);
while (i<=n) {
t=t+(1/i);
i++;
printf("\n so n la: %f",t);
}
//cach 3 do while
float n , t ,i;
t=0 ;
i=1;
printf(" nhap so n = ");
scanf("%f",&n);
do {
t=t+(1/i);
i++;
} while (i<+n);
printf("\n so n la:%f ",t);
return 0;
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2020-09-15 03:11:01
#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[]) {
//Bài 2: Tính S(n) = 1^2 + 2^2 + … + n^2
// cach 1 for
int n , t ,i;
t=0 ;
i=1;
printf(" nhap so n = ");
scanf("%d",&n);
for(i=1;i<=n;i++){
t=t+i*i;
printf(" so n la: %d",t);
}
// cach 2 wile
int n , t ,i;
t=0 ;
i=0;
printf(" nhap so n = ");
scanf("%d",&n);
while (i<=n) {
t=t+i*i;
i++;
printf("\n so n la: %d",t);}
//cach 3 do while
int n , t ,i;
t=0 ;
i=0;
printf(" nhap so n = ");
scanf("%d",&n);
do{
t=t+i*i;
i++;
}
while (i<=n);
printf("\n so n la: %d",t);
return 0;
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2020-09-15 03:10:33
#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[]) {
//Bài 1: Tính S(n) = 1 + 2 + 3 + … + n
//cach 1 for
int n , t ,i;
t=0 ;
i=1;
printf(" nhap so n = ");
scanf("%d",&n);
for (i=1;i<=n;i++) {
t=t+i;
printf("\n so n la: %d",t);
}
//cach 2 while
int n , t ,i;
t=0 ;
i=0;
printf(" nhap so n = ");
scanf("%d",&n);
while (i<=n) {
t=t+i;
i++;
printf("\n so n la: %d",t);
}
//cach 3 do while
int n , t ,i;
t=0 ;
i=0;
printf(" nhap so n = ");
scanf("%d",&n);
do{
t=t+i;
i++;
}
while (i<=n);
printf("\n so n la: %d",t);
return 0;
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-09-14 16:04:50
#include "bits/stdc++.h"
int main() {
//BAI 5
float n; scanf("%f", &n);
float n2 = n, n3 = n;
if (n < 0) printf("MATH ERROR!"), exit(0);
//Cach 1:
float s1 = 0;
for (float i = 0; i <= n; i++) s1 += (1 / (2 * i + 1));
printf("S1 = %.3f\n", s1);
//Cach 2:
float s2 = 0;
while (n2 >= 0) s2 += (1 / (n2 * 2 + 1)), n2--;
printf("S2 = %.3f\n", s2);
//Cach 3:
float s3 = 0;
do s3 += (1 / (n3 * 2 + 1)), n3--;
while (n3 >= 0);
printf("S3 = %.3f\n", s3);
return 0;
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-09-14 16:01:10
#include "bits/stdc++.h"
int main() {
//BAI 4
float n; scanf("%f", &n);
float n2 = n, n3 = n;
if (n <= 0) printf("MATH ERROR!"), exit(0);
//Cach 1:
float s1 = 0;
for (float i = 1; i <= n; i++) s1 += (1 / (2 * i));
printf("S1 = %.3f\n", s1);
//Cach 2:
float s2 = 0;
while (n2 > 0) s2 += (1 / (n2 * 2)), n2--;
printf("S2 = %.3f\n", s2);
//Cach 3:
float s3 = 0;
do s3 += (1 / (n3 * 2)), n3--;
while (n3 > 0);
printf("S3 = %.3f\n", s3);
return 0;
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-09-14 15:54:39
#include "bits/stdc++.h"
int main() {
//BAI 3
float n; scanf("%f", &n);
float n2 = n, n3 = n;
if (n <= 0) printf("MATH ERROR!"), exit(0);
//Cach 1:
float s1 = 0;
for (float i = 1; i <= n; i++) s1 += (1 / i);
printf("S1 = %.3f\n", s1);
//Cach 2:
float s2 = 0;
while (n2 > 0) s2 += (1 / n2), n2--;
printf("S2 = %.3f\n", s2);
//Cach 3:
float s3 = 0;
do s3 += (1 / n3), n3--;
while (n3 > 0);
printf("S3 = %.3f\n", s3);
return 0;
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-09-14 15:38:27
#include "bits/stdc++.h"
int main() {
//BAI 2
int n; scanf("%d", &n);
int n2 = n, n3 = n;
//Cach 1:
int s1 = 0;
for (int i = 1; i <= n; i++) s1 += pow(i, 2);
printf("S1 = %d\n", s1);
//Cach 2:
int s2 = 0;
while (n2 > 0) s2 += pow(n2, 2), n2--;
printf("S2 = %d\n", s2);
//Cach 3:
int s3 = 0;
do s3 += pow(n3, 2);
while (n3--);
printf("S3 = %d\n", s3);
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2020-09-14 14:16:14
#include<stdio.h>
#include<math.h>
int main()
//Cach for
{
int n;
printf("\nNhap n:");
scanf("%d",&n);
if (n<=0)
printf("Khong tinh duoc");
else{
float tong=0;
for (int i=1;i<=n;++i)
tong= tong + (1.0/(2*i));
printf("Tong day so do la %f",tong);
}
return 0;
}
//Cach while
{
{
int n;
printf("\nNhap n:");
scanf("%d",&n);
if (n<=0)
printf("Khong tinh duoc");
else{
float tong=0;
int i=1;
while(i<=n){
tong= tong + (1.0/(2*i));
i++;
}
printf("Tong day so do la %f",tong);
}
return 0;
}
}
//Cach do..while
{
int n;
printf("\nNhap n:");
scanf("%d",&n);
if (n<=0)
printf("Khong tinh duoc");
else{
float tong=0;
int i=1;
do{
tong= tong + (1.0/(2*i));
i++;
}while(i<=n);
printf("Tong day so do la %f",tong);
}
return 0;
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2020-09-14 14:06:58
#include<stdio.h>
int main()
//Cach while
{
int n;
printf("Nhap n: ");
scanf("%d",&n);
if (n<=0){
printf("Khong tinh duoc");
}
else{
int i=1, tong=0;
while(i<=n){
tong=tong + i*i;
i++;
}
printf("\nTong day so bang %d",tong);
}
}
//Cach do..while
{
int n;
printf("Nhap n: ");
scanf("%d",&n);
if (n<=0){
printf("Khong tinh duoc");
}
else{
int i=1, tong=0;
do{
tong=tong + i*i;
i++;
}while(i<=n);
printf("\nTong day so bang %d",tong);
}
}
//Cach for
{
int n;
printf("Nhap n: ");
scanf("%d",&n);
if (n<=0){
printf("Khong tinh duoc");
}
else{
int i,tong=0;
for(i=1;i<=n;i++)
tong=tong + i*i;
printf("\nTong day so bang %d",tong);
}
}