當前位置:首頁 » 編程語言 » c語言輸入一個整數求平方根
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言輸入一個整數求平方根

發布時間: 2022-05-10 12:29:23

❶ 用c語言求平方根

#include<stdio.h>
intmain()
{doublea,x;
intn;
scanf("%lf%d",&a,&n);
for(x=a/2;n--;)x=(x+a/x)/2;
printf("%lf",x);
return0;
}

❷ 用C語言編程:輸入一個整數,求它的平方,立方,平方根(結果保留2位小數 )

#include<stdio.h>
#include<math.h>
intmain(void)
{
intn;
scanf("%d",&n);
printf("%d%d%f",n*n,n*n*n,sqrt(n));
return0;
}

❸ C語言編程 求平方根

#include <stdio.h>
#include <math.h>
int main( )
{
double x, root;

scanf("%lf", &x);
/*---------*/
root=sqrt(x);
printf("The square root of %0.1f is %0.1f\n", x, root);
return 0;
}

❹ C語言 編寫程序,從鍵盤輸入一個正數,計算該數的平方根。

# include <stdio.h>
#include<math.h>
int main()
{
double x;
scanf("%lf",&x);
printf("%lf\n",sqrt(x));
return 0;
}

❺ C語言,輸入一個實數a計算並輸出其平方根

#include <stdio.h>
#include <math.h>
int main()
{
double a;//
scanf("%lf", &a);輸入一個實數a
if (a < 0)
{
printf("The number is error!");
}
else
{
printf("%lf", sqrt(a));//求平方根並列印結果。
}
return 0;
}

❻ C語言,下面程序從鍵盤任意輸入一個整數,先計算其絕對值,然後在該值的基礎上計算平方根

void main()
{
int a,s;
scanf("%d",&a);
if (a<0)
{
a=a*(-1);
}
s=a*a;
printf("%d %d",a,s);
system("pause");
}

❼ 輸入一個整數,求它的平方、平方根C++

要C++源碼嗎?
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
int m=0;
int n=0;
int s=0;
cin >> m;
n=sqrt(m);//sqrt是求平方根的函數
s=m*m; //*是運算符「乘」

cout << "平方根是 " << n << endl;
cout << "平方是 " << s << endl;

system("pause");
return 0;
}

樓主指的中文字是這樣的?:D

❽ c語言求平方根

平方直接兩個數相乘即可。
平方根可以通過這個數本身和0之間進行折半查找的方法,對數據進行計算。
當兩數相乘大於這個數時,重置最大值;小於這個數時,重置最小值。由於存在除不盡的數,可以加上一個精度進行判斷。
#include<stdio.h>
#include<math.h>
doublesquareFun(doublex)//平方
{
returnx*x;
}
doublesqrtFun(doublex,intprecision)//開方
{
doublea=0,b=x,mid=x/2,val=pow(10,-precision);
if(x<0)
return-1;
while(true)//循環折半查找
{
if(fabs(mid*mid-x)<=val)//達到進度則退出
break;
if(mid*mid>x)
b=mid;//重置最大值
else
a=mid;//重置最小值
mid=(a+b)/2;
}
returnmid;
}
intmain()
{
doubled=sqrtFun(2,15);
printf("%.15lf ",d);
printf("%.15lf ",squareFun(d));
}

❾ c語言編程 從鍵盤輸入一個數值,直到該數為正數為止,並求其平方根

1 循環輸入整數。

2 判斷整數值,如為正數,則退出循環,否則繼續輸入;

3 利用sqrt函數求平方根

4 輸出。

代碼如下:

intmain()
{
intn;
doubler;
while(1)
{
scanf("%d",&n);
if(n>0)break;
}
r=sqrt(n);
printf("%lf",r);
}