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)
![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 10:17:28
#include<stdio.h>
int main()
//Cach for
{
int n;
printf("\nNhap n:");
scanf("%d",&n);
if (n<=0){
printf("Ban da lam sai");
}
else{
int tong=0;
for(int i=0;i<=n;++i)
tong = tong + i;
printf("Tong n so la %d",tong);
}
getch();
return 0;
}
//Cach while
{
int n;
printf("\nNhap n:");
scanf("%d",&n);
if (n<=0){
printf("Ban da lam sai");
}
else{
int i=1,tong=0;
while(i<=n){
tong=tong + i;
i++;
}
printf("\nTong day so = %d ",tong);
}
}
//Cach do...while
{
int n;
printf("\nNhap n:");
scanf("%d",&n);
if (n<=0){
printf("Ban da lam sai");
}
else{
int i=1,tong=0;
do{
tong=tong+i;
i++;
}while(i<=n);
printf("\nTong day so = %d",tong);
}
}
![Đặ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 10:12:52
#include "bits/stdc++.h"
//BAI 1
int main() {
int s1 = 0, s2 = 0, s3 = 0;
int n;
printf("Nhap n: "); scanf("%d", &n);
int n1 = n, n2 = n;
//Cach 1:
for (int i = 0; i <= n; i++) s1 += i;
//Cach 2:
while (n1 > 0) s2 += n1, n1--;
//Cach 3:
do s3 += n2;
while(--n2);
printf("s = %d, s1 = %d, s2 = %d", s1, s2, s3);
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-09-14 10:10: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[]) {
int S = 0;
int i = 1;
int n;
printf("\nNhap n =");
scanf("%d", &n);
do {
S = S + i;
i = i + 1;
} while(i<=n);
printf("\n%d", S);
return 0;
}
![Nguyên Phấn Đông [community]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2020-09-14 10:08:02
#include<stdio.h>
#include<stdlib.h>
int main(){
float s=0, c=0, n;
printf("\nBai 2:");
printf("\nNhap n: "); scanf("%f",&n);
printf("\n======== Cach1:");
while (c <= n ){
s= s +c*c;
c++;
}
printf("\nS(n)= %f",s);
printf("\n======== Cach2:");
s=0;
c=0;
do{
s+=c*c;
c++;
}while(c<= (n) );
printf("\nS(n)= %f",s);
printf("\n======== Cach3:");
s=0;
for (int d=0; d <= n; d++){
s+=d*d;
}
printf("\nS(n)= %f",s);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(){
float s=0, c=1, n;
printf("\nBai 3:");
printf("\nNhap n: "); scanf("%f",&n);
printf("\n======== Cach1:");
while (c <= n ){
s+=1/c;
c++;
}
printf("\nS(n)= %f",s);
printf("\n======== Cach2:");
s=0;
c=1;
do{
s+=1/c;
c++;
}while(c<= (n) );
printf("\nS(n)= %f",s);
printf("\n======== Cach3:");
s=0;
for (float d=1; d <= n; d++){
s+=1/d;
}
printf("\nS(n)= %f",s);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(){
float s=0, c=1, n, e;
printf("\nBai 4:");
printf("\nNhap n: "); scanf("%f",&n);
printf("\n======== Cach1:");
while (c <= n ){
e= 2*c;
s+=1/e;
c++;
}
printf("\nS(n)= %f",s);
printf("\n======== Cach2:");
s=0;
c=1;
do{
e= 2*c;
s+= 1/e;
c++;
}while(c<= n );
printf("\nS(n)= %f",s);
printf("\n======== Cach3:");
s=0;
e=0;
for (int d=1; d <= n; d++){
e=d*2;
s+=1/e;
}
printf("\nS(n)= %f",s);
return 0;
}
![Nguyên Phấn Đông [community]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2020-09-14 10:07:26
#include<stdio.h>
#include<stdlib.h>
int main(){
float s=0, c=0, n;
printf("\nBai 1:");
printf("\nNhap n: "); scanf("%f",&n);
printf("\n======== Cach1:");
while (c <= n ){
s+=c;
c++;
}
printf("\nS(n)= %f",s);
printf("\n======== Cach2:");
s=0;
c=0;
do{
s+=c;
c++;
}while(c<= (n) );
printf("\nS(n)= %f",s);
printf("\n======== Cach3:");
s=0;
for (int d=0; d <= n; d++){
s+=d;
}
printf("\nS(n)= %f",s);
return 0;
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2020-09-14 09:46:40
#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[]) {
float i,n,tong;
tong = 0;
i = 1;
printf("Nhap n = ");
scanf("%f", &n);
//===========================Cach 1
for(i=1;i<=n;i++) {
tong = tong +1/i;
}
printf("Sn = %f", tong);
//===========================Cach 2
while (i<=n) {
tong = tong +1/i;
i++;
}
printf("Sn = %f", tong);
//===========================Cach 3
do {
tong = tong +1/i;
i++;
} while (i<=n);
printf("Sn = %f", tong);
return 0;
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2020-09-14 09:33:23
#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 tong, i, n;
tong = 0;
i = 1;
printf("Nhap n = ");
scanf("%d", &n);
for(i=1;i<=n;i++) {
tong = tong + i*i;
}
printf("Sn = %d", tong);
//
// while (i<=n) {
// tong = tong + i*i;
// i++;
// }
// printf("Sn = %d", tong);
// do {
// tong = tong +i*i;
// i++;
// } while (i<=n);
// printf("Sn = %d", tong);
return 0;
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2020-09-14 09:19:31
#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[]) {
//Cach 1: for
int tong, n, i;
tong = 0;
i = 1;
printf("\nNhap n =");
scanf("%d", &n);
/* for(i=1;i<=n;i++) {
tong = tong+i;
}
printf("\nSn = %d", tong);
*/
//Cach 2: while
/*
while (i<=n) {
tong = tong + i;
i++;
}
printf("\nSn = %d", tong);
*/
//Cach 3: do...while
do {
tong = tong + i;
i++;
} while (i<=n);
printf("\nSn = %d", tong);
return 0;
}