在這里,我正在列印陣列的元素,它們是任意數字的平方。
#include <iostream>
#include<math.h>
using namespace std;
// 這個函式被宣告用來檢查 n 是否是任意數的平方。
int squared(int n)
{
int p = sqrt(n);
// here I need to check if p is an integer or not so that I can print n, whose square root would be p.
if(p==int)
return n;
}
int main()
{
int arr[50];
cout<<"enter array elements"<<endl;
for(int i=0; i<5; i )
{
cin>>arr[i];
}
cout<<"print array elements which are squared"<<endl;
for(int j=0; j<5; j )
{
squared(arr[j]); //Also, here each element of the array arr is checked to see if it is a square of any number using the above-declared function.
}
}
// 請檢查所有錯誤 // 還有,如何檢查一個數字是自然數還是 C 中的分數/小數
uj5u.com熱心網友回復:
檢查 var 型別的簡單方法如下
if (p == (int)p) {
std::cout<<"integer"<<std::endl;
}else if (p ==(float)p){
std::cout<<"float"<<std::endl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/423982.html
