❶ c語言:輸入一個正整數n,用3種方法求e=1+1/1!+1/2!+。。。。
#include<stdio.h>
int main(void)
{
int i,item,n;
double e;
item=1;
e=1;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++){
item=item*i;
e=e+1.0/item;
}
printf("e=%.4f\n",e);
return 0;
}
(2)
#include<stdio.h>
double fact(int n);
int main(void)
{
int i,n;
double e;
e=1;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++){
e=e+1.0/fact(i);
}
printf("e=%.4f\n",e);
return 0;
}
double fact(int n)
{
int i;
double result;
result =1;
for(i=1;i<=n;i++)
result=result*i;
return result;
}
(3)
#include<stdio.h>
int main(void)
{
int i,j,n;
double e,item;
e=1;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++){
item=1;
for(j=1;j<=i;j++)
item=item*j;
e=e+1.0/item;
}
printf("e=%.4f\n",e);
return 0;
}
運行一下,
❷ 用C語言編程 :輸入一個正整數n 用3種方法分別計算下式的和 保留4位小數
/
printf(", total).4f\ i <
for (j = i;= n;/
total += temp;= n;
double total = 0.h"
double total = 0:%:%;;= n;嵌套循環, temp = 1.0, total); j >.0; i <.0;
total = total + temp.0; i;;
/%d" j, &n); j--)
temp = temp / i++)
{
temp = temp /
}
return total;
double fact(int n)
{
int i.0;= 1;n"
}
int main(void)
{
int i; i++)
{
temp = 1, temp = 1; i <;
for (i = 1;調用函數
total = 0.4f\.0;嵌套循環
total = 0;
scanf(", fact(n));/
// i, n;調用函數;n".4f\一重循環
for (i = 1:%; i++)
{
temp = temp /一重循環;, j;
total = total + temp;
for (i = 1#include "
}
printf("
}
printf("n"
return 0
/
total);嵌套循環;調用函數; i++)
total += temp /.0;一重循環
&n).4f\= n.4f\ j--)
total = total + temp /
}
int main(void)
{
int i;%d"
/= j;
printf("
return 0
j;= n;n"= 1
n; i++)
{
temp = 1;n" i <.4f\.0; i++)
total = total + temp /
scanf("
for (i = 1;/
for (i = 1;
double total = 0.0;iostream>= i.0;
for (j = i;
temp = 1;
using namespace std;;
fact(n))
total); j >
/
double total = 0; i < i <;
printf("調用函數
total = 0;n":%:%:%;一重循環
for (i = 1;/.0;嵌套循環
total = 0;= n;
return total
❸ c語言,輸入一個正整數n,求1+2+3+......+n的和
main(){ int k,flag=1,n;float s=0;scanf("%d",&n);for(k=1;k<=n;k++){ s=s+__flag*(k/k+1)______; _____flag= (-1)*flag_____;}printf("sum=%f\n",s);} 拷貝你提問中的代碼,但是for後邊的分號明顯是手誤,給你改了一下
❹ 用C語言編程:1000位以內的正整數的加法運算
聲明兩個能容納1000位十進制數的char型數組存儲輸入數字字元串,以長的做被加數和結果,短的長度控制加法循環次數。在加法過程中判斷和處理進位。舉例代碼如下:
//#include"stdafx.h"//Ifthevc++6.0,withthisline.
#include"stdio.h"
#include"string.h"
intmain(void){
chara[1002]={'0'},b[1002]={'0'},*pl=a,*ps=b;
inti,j,la,lb;
while(1){//保證輸入是正確的
printf("Inputa&b(length<=1000)... ");
scanf("%[1234567890]%[1234567890]",a+1,b+1);//最前面留1位做進位
if((la=strlen(a))<1002&&(lb=strlen(b))<1002)
break;
printf("Error,redo:");
}
if(la<lb){//找出長的做被加數和結果
ps=a,pl=b;
j=la,la=lb,lb=j;
}
for(i=lb-1,j=la-1;i>0;i--,j--)//從末位向前對應加
if((pl[j]+=ps[i]-'0')>'9')//某位>'9'則處理進位
pl[j]-=10,pl[j-1]++;
for(;j>0;j--)//若被加數有進位則繼續做完
if(pl[j]>'9')
pl[j]-=10,pl[j-1]++;
printf("Theresultis%s ",pl[0]=='1'?pl:pl+1);//有進位則第0位輸出
return0;
}
❺ C語言中 整型數據加法 1+3等於多少
這幾個空這樣填:
第一行:gets()
第二行:puts()
第三行:3
第四行:3.123
第五行:1
第六行:2
第七行:2.5
注意:第三行理由是,兩個整數相加,結果是整數,第四行理由是,一個整數和一個浮點數相加或者兩個浮點數相加,結果一定是一個浮點型的數,第五行理由是,整數%整數,這個是計算兩個數的余數,也就是取余運算,第六行理由是,整數/整數,這個計算的是兩個數的商的整數部分,也就是取整,第七行理由是:浮點型數/整數或浮點數/浮點數,這個就是正常的做除法運算,也就是當/號左右兩邊都是整數的時候取商,這種運算會忽略掉余數,當/號左右有一個或兩個都是浮點型數的時候,就是做除法,這時候有商才是最准確的
❻ C語言求解:輸入一個正整數,求各個位的整數之和
直接上代碼,如下:
#include <stdio.h>
int main()
{
int num = 0;
int s = 0;
int i = 0;
scanf("%d",&num);
while(num)
{
s += num%10;
num = num/10;
i++;
}
printf("數字個數為%d,各個數相加為%d
",i,s );
return 0;
}
❼ C語言用字元串實現整數的加法
這個問題你可以寫兩個小函數來實現 , 一個是atoi()函數,就是把字元串轉化成int型,另一個是itoa(),就是把int型的數字轉化成字元串。剛開始時用atoi()把str1,str2轉化成整形數,然後相加,再把相加後的結果用itoa()函數轉化成字元串,再賦值給result。這是個思路,那兩個函數也不太難,自己嘗試實現一下。
❽ C語言編程 100以內整數加減法
srand(time(0));
inta[20];
for(inti=0;i<20;i++)
{
a[i]=rand()%100;
}
for(inti=10;i<20;i+=2)
{
if(a[i]<a[i+1])
{
intt=a[i];a[i]=a[i+1];a[i+1]=t;
}
}
intscore=0;
for(inti=0;i<20;i+=2)
{
charc=i<10?'+':'-';
printf("%d%c%d=幾? ",a[i],c,a[i+1]);
intx;
scanf("%d",&x);
if((i<10&x=a[i]+a[i+1])|(i>=10&x=a[i]-a[i+1]))
{
score+=10;
cout<"回答正確"<<endl;
}
elsecout<"回答錯誤"<<endl;
}
switch(score)
{
case0:
case10:
case20:
case30:
case40:
case50:cout<<"要加油哦"<<endl;break;
case60:
case70:
case80:cout<<"真不錯哦"<<endl;break;
case90:cout<<"你真棒"<<endl;break;
case100:cout<<"酷斃了"<<endl;break;
default:;
}
❾ 求輸入一個正整數輸出幾個連續整數相加等於這個數,如27等於13+14也等於8.9.10相加都輸出c
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int i,flag,n;
int sum = 0;
queue<int> q;
printf("請輸入一個整數:\n");
scanf("%d",&n);
for (i = 1; i <= n; i++)
{
flag = i;
while (1)
{
sum += flag;
q.push(flag);
flag++;
if (sum == n && q.size() != 1)
{
int temp;
printf("%d = ",n);
temp = q.front();
printf("%d",temp);
q.pop();
while(!q.empty())
{
temp = q.front();
printf(" + %d",temp);
q.pop();
}
printf("\n");
}
if (sum > n) {break;}
}
while(!q.empty()) q.pop();
sum = 0;
}
return 0;
}