Write a function in C++ that takes an integer argument D and returns the index and value of the first D-digit Lucas number. Turn in a print-out of your code, as well as the index and value your function returns when D = 15. Example) Let D = 3. The first 3-digit Lucas Number is L10 = 123, so your function should return something like firstDDigitLucas(3) = (10, 123).
------------------------------------------------------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
void lucas(long long); //function to get lucas
numbers
int noOfDigit(long long); // function number of digits in a
number
int main()
{
long long n;
cout<<"Enter No. of digits = ";
cin>>n;
lucas(n);
return 0;
}
void lucas(long long n)
{
long long a=2;
long long b=1;
long long temp;
int index=1;
while(1)
{
temp=a+b;
a=b;
b=temp;
index++;
int d=noOfDigit(temp);
if(n==d)
{
cout<<"firstDDigitLucas("<<n<<") =
"<<"("<<index<<","<<temp<<")"<<endl;
return;
}
}
}
int noOfDigit(long long temp)
{
int digits = 0;
while (temp != 0)
{
temp /= 10;
digits++;
}
return digits;
}
------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------
Get Answers For Free
Most questions answered within 1 hours.