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)

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]
An Văn Minh

2020-09-15 08:36:33



//Baitap4
//=============================================
#include<stdio.h>
#include<math.h>
int main()
{
	float i=1;
	float n;
	float s=0;
	printf("\nNhap n=");
	scanf("%f", &n);
		//dung while
	while(i<=n){
		s=s+1/(2*i);
		i++; 
	}
	//dung do...while
	do{
		s=s+1/(2*i);
		i++;
	}
	while(i<=n);
//	dung for
	for(float i=1;i<=n;i++){
		s=s+1/(2*i);
	}
	printf("\nTong S(n)=%f", s);
	return 0;
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2020-09-15 08:26:07



#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[]) {
	//bai 2
	int i = 1, n, s = 0;
	printf("\nNhap so n:");
	scanf("%d", n);
	//For
	for(i = 1; i <=n ; i++)
	s += i*i;
	printf("\nTong s:%d", s);
	//While
	while(i <= n)
	{
		s += i*i;
		printf("\nTong s:%d", s);
		i++;
	}
	//Do...While
	do
	{
		s += i*i;
		i++;
	}
	while(i <= n);
	{
	printf("\nTong s:%d", s);
	}
	
	return 0;
}



An Văn Minh [T2008A]
An Văn Minh

2020-09-15 08:07:34



//Baitap3
//=============================================
#include<stdio.h>
#include<math.h>
int main()
{
	float i=1;
	float n;
	float s=0;
	printf("\nNhap n=");
	scanf("%f", &n);
		//dung while
	while(i<=n){
		s=s+(1/i);
		i++; 
	}
	//dung do...while
	do{
		s=s+1/i;
		i++;
	}
	while(i<=n);
	dung for
	for(float i=1;i<=n;i++){
		s=s+1/i;
	}
	printf("\nTong S(n)=%f", s);
	return 0;
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2020-09-15 07:47:24



#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[]) {
	//bai 1

	int i = 1, n, s = 0;
	printf("\nNhap n=");
	scanf("%d", n);
	//While
	while(i <= n){
		s = s + i;
		printf("\nTong s la:%d", s);
	}
	//Do...Whlie
	do{
		s = s + i;
		i++;
	}while(i <= n);printf("\nTong s la:%d", s);

	///For
	for(i = 1; i <= n; i++){
		s+=i;
		printf("\nTong s la:%d", s);
	}
	return 0;

	



Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2020-09-15 05:11:50

bài 1


#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 S1=0, S2=0, S3=0, n;
	
	
	printf("\nnhap n: "); scanf("%d",&n);
	//dung for
	printf("\n==========cach 1");
	int t;
	for(t=0;t<=n;t++) {
		 S1+=t;
		 printf("\nket qua: %d",S1);}
   
    	
	//dung while
	printf("\n==========cach 2");
	int i=1;
	while(i<=n) {
		S2=S2+i;
		i++;
		printf("\nket qua: %d",S2);}
		
		
	//dung do..while
	printf("\n==========cach 3");
	int k=1;
	do {
	    S3=S3+k;
		k++;} while(k<=n); printf("\nket qua: %d",S3);
		
	return 0;
}



vuong huu phu [T2008A]
vuong huu phu

2020-09-15 04:26: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[]) {
//bai 5
//cach 1 for
int n,i;
float t;
i=1;
t=0;
printf("\n nhap so n la ");
scanf("%d",&n);

for (i=1;i<=n;i++){
	t=t+(1/(2*i+1));
	printf("\n so n la %f",t);
}
//cach 2 while
int n,i;
float t;
i=1;
t=0;
printf("\n nhap so n la ");
scanf("%d",&n);
while (i<=n) {
		t=t+(1/(2*i+1));
		i++;
		printf("\n so n la: %d",t);
		}
//cach 3 do while		
int n,i;
float t;
i=1;
t=0;
printf("\n nhap so n la ");
scanf("%d",&n);
		do{
     	t=t+(1/(2*i+1));
		i++;
	}
		while (i<=n);
			printf("\n so n la: %d",t);
		
	return 0;
}



Nguyễn Hữu Hiếu [T2008A]
Nguyễn Hữu Hiếu

2020-09-15 04:18:18



#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[]) {
	
//Bai 5	
	
	float i,n,tong;
	tong = 0;
	printf("Nhap n = ");
	scanf("%f", &n);
	for(i=0;i<=n;i++) {
		tong = tong + 1/(2*i+1);
	}
	printf("Sn = %f", tong);
	
	
	
	return 0;
}



Nguyễn Hữu Hiếu [T2008A]
Nguyễn Hữu Hiếu

2020-09-15 04:11:21



#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[]) {
	
//Bai 4
	
	float i,n,tong;
	printf("Nhap n= ");
	scanf("%f", &n);
	i = 1;
	for(i=1;i<=n;i++) {
		tong = tong + 1/(2*i);
		}
		printf("Sn = %f", tong);
	return 0;
}



An Văn Minh [T2008A]
An Văn Minh

2020-09-15 03:46:50



//Baitap2:
#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);
		i++;
	}
	//dung do...while
	do{
		s=s+i*i;
		i++;
	}
	while(i<=n);
	//dung for
	for(int i=1;i<=n;i++){
		s=s+i*i;
	}
	printf("\nTong S(n)=%d", s);
	return 0;
}



vuong huu phu [T2008A]
vuong huu phu

2020-09-15 03:38:09



#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[]) {
    //S(n) = ½ + ¼ + … + 1/2n
  
    //cach 1 for
	int n,i;
	float t;
	t=0 ;
	i=1;
	printf(" nhap so n = ");
	scanf("%d",&n);
	
	for (i=1;i<=n;i++) {
     t=t+1.0/2*i;
	printf("\n so n la: %f",t);	
	}
	
		//cach 2 while
   int n,i;
float t;
t=0;
i=1;
printf (" nhap so n ");
scanf("%d",&n);

while (i<=n){
	t=t+1,0/2*i;
	i++;
	printf("\n so n la %f",t);
}
	
	//cach 3 do while

int n,i;
float t;
i=1;
t=0;
printf (" nhap so n ");
scanf("%d",&n);

do{
	t=t+1.0/2*i;
	i++;
	} while (i<=n);
    printf("\n so n la %f",t);
    
	return 0;	
}