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 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:34:48
//bai 4
float n, i, s4;
printf("Nhap n: ");
scanf("%f",&n);
if (n<=0){
printf("Khong co ket qua");
}
//sd for
else{
s4=0;
for(i=1; i<=n; i++){
s4 += (1/(i*2));
}
printf("s4 = %f",s4);
}
//sd while
else {
s4=0;
i=1;
while(i<=n){
s4+= (1/(i*2));
i++;
}
printf("s4 = %f",s4);
}
//sd do...while
else{
s4=0;
i=1;
do{
s4+=(1/(i*2));
i++;
}while(i<=n);
printf("s4 = %f",s4);
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-09-15 09:30:13
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//Tính S(n) = 1 + 1/3 + 1/5 + … + 1/(2n + 1)
//Ham while
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
i = 1;
S = 1;
while(i<=n){
S = S + 1/(2*i+1);
i++;
}
printf("Tong S = %f", S);
return 0;
}
//Ham do while
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
i = 1;
S = 1;
do{
S = S + 1/(2*i+1);
i++;
}while(i<=n);
printf("Tong S = %f", S);
return 0;
}
//Ham for
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
i = 1;
S = 1;
for(i=1;i<=n;i++){
S = S + 1/(2*i+1);
}
printf("Tong 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:30:09
//bai 3
float n, i, s3;
printf("Nhap n: ");
scanf("%f",&n);
if (n<=0){
printf("Khong co ket qua");
}
//sd for
else{
s3=0;
for(i=1; i<=n; i++){
s3 += (1/i);
}
printf("s3 = %f",s3);
}
//sd while
else {
s3=0;
i=1;
while(i<=n){
s3+= (1/i);
i++;
}
printf("s3 = %f",s3);
}
//sd do...while
else{
s3=0;
i=1;
do{
s3+=1/i;
i++;
}while(i<=n);
printf("s3 = %f",s3);
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-09-15 09:29:14
//Bài 4: Tính S(n) = ½ + ¼ + … + 1/2n
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float S,n,i;
printf("Nhap so n = "); scanf("%f", &n);
S = 0;
for(i=1;i<=n;i++){
S = S + 1/(2*i);
}
printf("Tong S = %f", S);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float S,n,i;
printf("Nhap so n = "); scanf("%f", &n);
S = 0;
i = 1;
while(i<=n){
S = S + 1/(2*i);
i++;
}
printf("Tong S = %f", S);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float S,n,i;
printf("Nhap so n = "); scanf("%f", &n);
S = 0;
i = 1;
do{
S = S + 1/(2*i);
i++;
}while(i<=n);
printf("Tong S = %f", S);
return 0;
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-09-15 09:27:28
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//Tính S(n) = 1 + ½ + 1/3 + … + 1/n
//Ham while
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
i = 1;
while(i<=n){
S = S + 1/i;
i++;
}
printf("Tong S = %f", S);
return 0;
}
//Ham do while
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
i = 1;
do{
S = S + 1/i;
i++;
} while(i<=n);
printf("Tong S = %f", S);
return 0;
}
//Ham for
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
for(i=1; i<=n;i++){
S = S + 1/i;
}
printf("Tong S = %f", S);
return 0;
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-09-15 09:25:54
//Tính S(n) = 1^2 + 2^2 + … + n^2
//Ham while
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
i = 1;
while(i<=n){
S = S + pow(i,2);
i++;
}
printf("Tong S = %f", S);
return 0;
}
//Ham do while
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
i = 1;
do{
S = S + pow(i,2);
i++;
} while(i<=n);
printf("Tong S = %f", S);
return 0;
}
//Ham for
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
for(i=1;i<=n;i++){
S = S + pow(i,2);
}
printf("Tong S = %f", S);
return 0;
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-09-15 09:24:45
//Tính S(n) = 1 + 2 + 3 + … + n
//Ham while
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
i = 1;
while(i<=n){
S = S + i;
i++;
}
printf("Tong S = %f", S);
return 0;
}
//Ham do while
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
i = 1;
do{
S = S + i;
i++;
} while(i<=n);
printf("Tong S = %f", S);
return 0;
}
//Ham for
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float i,S,n;
printf("Nhap n =");
scanf("%f", &n);
S = 0;
for(i=1; i<=n;i++){
S = S + i;
}
printf("Tong 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:19:10
//bai 2
int n, i, s1;
printf("Nhap n: ");
scanf("%d",&n);
if (n<=0){
printf("Khong co ket qua");
}
//sd for
else{
s1=0;
for(i=0; i<=n; i++){
s1 += pow(i,2);
}
printf("s1 = %d",s1);
}
//sd while
else {
s1=0;
while(i<=n){
s1+=pow(i,2);
i++;
}
printf("s1 = %d",s1);
}
//sd do...while
else{
s1=0;
do{
s1+=pow(i,2);
i++;
}while(i<=n);
printf("s1 = %d",s1);
}
![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:13:56
//bai 1
int n, i, s1;
printf("Nhap n: ");
scanf("%d",&n);
if (n<=0){
printf("Khong co ket qua");
}
//sd for
else{
s1=0;
for(i=0; i<=n; i++){
s1 += i;
}
printf("s1 = %d",s1);
}
//sd while
else {
s1=0;
while(i<=n){
s1+=i;
i++;
}
printf("s1 = %d",s1);
}
sd do...while
else{
s1=0;
do{
s1+=i;
i++;
}while(i<=n);
printf("s1 = %d",s1);
}
![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 08:40:36
//Baitap5
//=============================================
#include<stdio.h>
#include<math.h>
int main()
{
float i=0;
float n;
float s=0;
printf("\nNhap n=");
scanf("%f", &n);
//dung while
while(i<=n){
s=s+1/(2*i+1);
i++;
}
//dung do...while
do{
s=s+1/(2*i+1);
i++;
}
while(i<=n);
// dung for
for(float i=1;i<=n;i++){
s=s+1/(2*i+1);
}
printf("\nTong S(n)=%f", s);
return 0;
}