/**
* Write a Java program to display first N Lucas numbers. ( 3>N>100 )
The Lucas numbers or series are integer sequences named after the mathematician François Édouard Anatole Lucas,
who studied both that sequence and the closely related Fibonacci numbers.
Lucas numbers and Fibonacci numbers form complementary instances of Lucas sequences.
The sequence of Lucas numbers : 2, 1, 3, 4, 7, 11, 18, 29, ….
*/
import java.util.*;
public class Lucas
{
public static void lucas(int x, int a, int b) //recursive function to display lucas series
{
if(x==0)
System.out.println();
else
{
int c=a+b;
a=b;
b=c;
System.out.print(", "+c);
lucas(x-1,a,b);
}
} // end of lucas()
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter number of terms : ");
int N=sc.nextInt();
if(N>3 && N<100)
{
System.out.println("\nThe sequence of "+N+" Lucas numbers are : ");
System.out.print(" 2, 1");
lucas(N-2,2,1);
}
else
{
System.out.println("OUT OF RANGE.");
}
} // end of main
} // end of class
Q.9 WAP in C/ C++/ java to check whether the number is a Special Number or not. Definition: If the sum of the factorial of digits of a number (N) is equal to the number itself, the number (N) is called a special number. INPUT : 145 The digits of the number are: 1, 4, 5 Factorial of digits: 1! = 1 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Sum of factorial of digits = 1 + 24 + 120 = 145 Hence, the given number 145 is a special number. Solution : #include <stdio.h> int factorial ( int x) // function to return factorial of the number { int f = 1 ; for ( int i = 1 ; i <= x; i ++ ) { ...
Comments
Post a Comment