Lucas number

Here given code implementation process.

//C Program
//Print lucas numbers list
#include <stdio.h>

//function which are display  initial N lucas number
void lucas_no(int n) 
{ 
  //Variable which are control execution
  //set initial values
  int a = 2;
  int b = 1;
  int c = 0;

  printf("\n%d initial lucas number is \n",n);
  //loop which are printing N lucas numbers
  for (int i = 0; i < n; i++)  
  { 
    printf("%d ",a );
    c = a + b; 
    //next lucas no
    a = b; 
    b = c; 
  } 
} 

int main() {

  lucas_no(10);
  return 0;
}

Output

10 initial lucas number is
2 1 3 4 7 11 18 29 47 76
/*
 C++ Program
 Print lucas numbers list
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Function which are display  initial N lucas number
		void lucas_no(int n) {
			//Variable which are control execution
			//set initial values
			int a = 2;
			int b = 1;
			int c = 0;
			cout << "\n" << n << " initial lucas number is \n";
			//loop which are printing N lucas numbers

			for (int i = 0; i < n; i++) {
				cout << " " << a;
				c = a + b;
				//next lucas no
				a = b;
				b = c;
			}
		}
};
int main() {
	MyNumber obj;
	// Test Case
	obj.lucas_no(10);
	return 0;
}

Output

10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76
/*
  Java Program
  Print lucas numbers list
*/

public class MyNumber {

  //Function which are display  initial N lucas number
  public void lucas_no(int n) 
  { 
    //Variable which are control execution
    //set initial values
    int a = 2;
    int b = 1;
    int c = 0;

    System.out.print("\n"+n+" initial lucas number is \n");
    //loop which are printing N lucas numbers
    for (int i = 0; i < n; i++)  
    { 
      System.out.print(" "+a );
      c = a + b; 
      //next lucas no
      a = b; 
      b = c; 
    } 
      
  } 


  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
      // Test Case
      obj.lucas_no(10);
  }
}

Output

10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76
/*
  C# Program
  Print lucas numbers list
*/
using System;
public class MyNumber {

	//Function which are display  initial N lucas number
	public void lucas_no(int n) {
		//Variable which are control execution
		//set initial values
		int a = 2;
		int b = 1;
		int c = 0;

		Console.Write("\n" + n + " initial lucas number is \n");
		//loop which are printing N lucas numbers
		for (int i = 0; i < n; i++) {
			Console.Write(" " + a);
			c = a + b;
			//next lucas no
			a = b;
			b = c;
		}

	}


	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		// Test Case
		obj.lucas_no(10);
	}
}

Output

10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76
# Python 3 Program
# Print lucas numbers list
class MyNumber :
	#Function which are display  initial N lucas number
	def lucas_no(self, n) :
		#set initial values
		#Variable which are control execution
		a = 2
		b = 1
		c = 0
		print("\n", n ," initial lucas number is ")
		#loop which are printing N lucas numbers
		i = 0
		while (i < n) :
			print(" ", a)
			c = a + b
			#next lucas no
			a = b
			b = c
			i += 1
		
	

def main() :
	obj = MyNumber()
	obj.lucas_no(10)


if __name__ == "__main__":
	main()

Output

10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76
# Ruby Program 
# Print lucas numbers list
class MyNumber 
	#Function which are display  initial N lucas number
	def lucas_no(n) 

		#Variable which are control execution
        #Set initial values
		a = 2
		b = 1
		c = 0
		print("\n", n ," initial lucas number is \n")
		#loop which are printing N lucas numbers
		i = 0
		while (i < n) 
			print(" ", a)
			c = a + b
			#next lucas no
			a = b
			b = c
			i += 1
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.lucas_no(10)
end
main()

Output

10 initial lucas number is 
 2 1 3 4 7 11 18 29 47 76
/*
 Scala Program
 Print lucas numbers list
*/
class MyNumber {
	//Function which are display  initial N lucas number
	def lucas_no(n: Int): Unit = {
		//Variable which are control execution
		//set initial values
		var a: Int = 2;
		var b: Int = 1;
		var c: Int = 0;
        print(s"\n $n initial lucas number is \n");
		//loop which are printing N lucas numbers
		var i: Int = 0;
		while (i < n) {
			print(s" $a");
			c = a + b;
			//next lucas no
			a = b;
			b = c;
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
        obj.lucas_no(10);
	}
}

Output

 10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76
/*
  Swift 4 Program
  Print lucas numbers list
*/
class MyNumber {
	//Function which are display  initial N lucas number
	func lucas_no(_ n: Int) {
		//Variable which are control execution
		//set initial values
		var a: Int = 2;
		var b: Int = 1;
		var c: Int = 0;
		print("\n", n ," initial lucas number is ");
		//loop which are printing N lucas numbers
		var i: Int = 0;
		while (i < n) {
			print(" ", a,terminator:"");
			c = a + b;
			//next lucas no
			a = b;
			b = c;
			i += 1;
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.lucas_no(10);
}
main();

Output

 10  initial lucas number is
  2  1  3  4  7  11  18  29  47  76
<?php
/*
  Php Program
  Print lucas numbers list
*/
class MyNumber {
	//Function which are display  initial N lucas number

	public 	function lucas_no($n) {
		//Variable which are control execution
		//set initial values
		$a = 2;
		$b = 1;
		$c = 0;
		echo("\n". $n ." initial lucas number is \n");
		//loop which are printing N lucas numbers

		for ($i = 0; $i < $n; $i++) {
			echo(" ". $a);
			$c = $a + $b;
			//next lucas no
			$a = $b;
			$b = $c;
		}
	}
};

function main() {
	$obj = new MyNumber();
	// Test Case

	$obj->lucas_no(10);
}
main();

Output

10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76
/*
 Node Js Program
 Print lucas numbers list
*/
class MyNumber {
	//Function which are display  initial N lucas number
	lucas_no(n) {
		//Variable which are control execution
		//set initial values
		var a = 2;
		var b = 1;
		var c = 0;
		process.stdout.write("\n" + n + " initial lucas number is \n");
		//loop which are printing N lucas numbers

		for (var i = 0; i < n; i++) {
			process.stdout.write(" " + a);
			c = a + b;
			//next lucas no
			a = b;
			b = c;
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	// Test Case
	obj.lucas_no(10)
}
main();

Output

10 initial lucas number is
 2 1 3 4 7 11 18 29 47 76


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved