Cè¯è¨æºç¨åºï¼
#include "stdio.h"
void main()
{
int i, n;
int f = 1; /* ç¬¦å· */
double sum; /* å项ä¹å */
printf("input value of n : ");
scanf("%d", &n);
sum = 0.0;
for(i=1; i<=n; i++)
{
sum += 1.0 * f / i;
f *= -1; /* t å¨æ£è´1ä¹é´åæ¢ */
}
printf("1-1/2+1/3-1/4+ ... +-1/n = %lf", sum);
}
è¿è¡æµè¯ï¼
C语言 编程计算1-1\/2+1\/3-1\/4+……±1\/n
double sum; \/* 各项之和 *\/ printf("input value of n : ");scanf("%d", &n);sum = 0.0;for(i=1; i<=n; i++){ sum += 1.0 * f \/ i;f *= -1; \/* t 在正负1之间切换 *\/ } printf("1-1\/2+1\/3-1\/4+ ... +-1\/n = %lf", sum);} 运行测试:...
c语言 计算1-1\/2+1\/3-1\/4…+1\/n=? (保留三位小数) 我的哪里错了
void main(){ int i=1,n;double a,sum=0;scanf("%d",&n); \/\/把n改成了&n while (i<=n){ a=(pow((-1),(i+1)))*(1.0)\/i; \/\/ 把你写的那一行移到这里, 你放在while循环外面a的值就 \/\/成了一个常量了。sum = sum +a;i++;} printf("%.3f\\n",sum);} ...
用C语言如何编写1-1\/2+1\/3-1\/4+…+(-1)(n+1次方)\/n
include <math.h> long double fun(int n);main(){ int n;printf("请输入n的值:\\n");scanf("%d",&n);printf("表达式的值为:%lf\\n",fun(n));} long double fun(int n){ long double result;if(n == 1){ return 1;} else { result = (long double)pow(-1,n+1)\/n;retu...
用C语言求s=1-1\/2!+1\/3!-1\/4!+……+1\/n!其中n的值由键盘输入(急!!)
double f = 1 ;for (int t=1 ; t<= n ; t++){ f = f*t ;} return f;} void main(){ int n, k=1;double s = 0 ;printf("Input n\\nn=") ;scanf("%d", &n) ;for (int t=1; t<=n; t++){ s = s + ((double)k)\/factorial(t) ;k = -k ;} printf("s...
C语言编写函数求:1-1\/2+1\/3-1\/4+1\/5-1\/6+...1\/n的值怎么做?结果是什么...
1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。2、在test.cpp文件中,输入C语言代码:。int n = 100;double s;for (int i = 1; i <= n; i++)if (i % 2 == 0)s -= 1.0 \/ i;else s += 1.0 \/ i;printf("%lf", s);3、编译器运行test.cpp文件,...
c语言1\/1-1\/2+1\/3-1\/4+.
include"stdio.h"intmain(){ doublesum=0;\/\/用double的目的是提高精度 doubleitem=0;inti;for(i=1;i<=99;i+=2)\/\/步长2,各项头数是相接的,{ item=i*(i+1)*(i+2)*1.0;sum+=item;} printf("1*2*3+3*4*5+...+99*100*101=%.0lf\\n",sum);return0;} ...
c语言计算 1-1\/2+1\/3-1\/4+1\/5-1\/6...…±1\/n 的值
因为term的值改变了 不再是1 也可以在循环里赋值1 include<stdio.h> main(){ int n;float sum=0,term;for(n=1;n<=30;n++){ if(n%2==1){ term=1.0\/n;} else { term=-(1.0)\/n;} sum=sum+term;} printf("%f",sum);} ...
C语言编程题,计算S=1-1\/2+1\/3-1\/4...+1\/m
由于你的n是int型,则1\/n是整数,程序中的1\/n或(-1)\/n一直是0,所以s一直都是0;将t=1\/n和t=(-1)\/n改为t=1\/(float)n和t=-1\/(float)n即可,即将n强制转换为float型,这样1\/n就可以表示为小数了。
C语言编写函数求:1-1\/2+1\/3-1\/4+1\/5-1\/6+...1\/n的值怎么做?结果是什么...
include<stdio.h>int main(){int i=1,n;float s=0;scanf("%d",&n);while(i<=n){if(i%2)s+=1.0\/i; else s-=1.0\/i; i++; } printf("%f\\n",s); return 0;}
c语言 1-1\/2+1\/3-1\/4+1\/5-...+1\/n
修改后的C语言程序:include <stdio.h>void main(){int n; float i; float sum = 1.0;int t = -1;printf("Please input a integer number n:"); scanf("%d", &n); for(i=2; i<=n; i++){ sum += 1.0 * t \/ i;t *= -1;} printf("The sum 1-1\/2+1\/3+...+...