/**
* 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
/** * Write a Java program to find and print the Happy numbers between m and n. Happy number: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1, or it loops endlessly in a cycle which does not include 1. Example: 19 is a happy number 1^2 + 9^2=82 8^2 + 2^2=68 6^2 + 8^2=100 1^2 + 0^2 + 02=1 The first few happy numbers are 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, ... */ import java.util.*; public class HappyNum { public static int digitSq( int x) // recursive function to find square of digits of the number { if(x<10) return x*x; else return (x%10)*(x%10) + digitSq(x/10); } // end of digitSq() public static boolean isHappy(int num, int i) ...
Comments
Post a Comment