Find ceiling in a sorted array

Here given code implementation process.

// C program
// Find ceiling in a sorted array
#include <stdio.h>
 // Find the ceiling of given number
void findCeiling(int arr[], int n, int num)
{
	int i = 0;
	// Execute loop until array elements are not 
	// Greater than given number
	while (i < n && arr[i] < num)
	{
		i++;
	}
	// Display given number
	printf("\n Given number : %d ", num);
	if (i != n)
	{
		// Display the calculated result
		printf("\n Ceiling : %d", arr[i]);
	}
	else
	{
		// When ceiling of number number not exist
		printf("\n Ceiling not exist\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Display given array
	int arr[] = {
		1 , 9 , 12 , 44 , 45 , 52 , 70 , 230
	};
	// Get the number of element
	int n = sizeof(arr) / sizeof(arr[0]);
	// Test
	findCeiling(arr, n, 64);
	findCeiling(arr, n, 0);
	findCeiling(arr, n, 117);
	findCeiling(arr, n, 240);
	return 0;
}

Output

 Given number : 64
 Ceiling : 70
 Given number : 0
 Ceiling : 1
 Given number : 117
 Ceiling : 230
 Given number : 240
 Ceiling not exist
// Java program
// Find ceiling in a sorted array
public class Ceiling
{
	// Find the ceiling of given number
	public void findCeiling(int[] arr, int n, int num)
	{
		int i = 0;
		// Execute loop until array elements are not 
		// Greater than given number
		while (i < n && arr[i] < num)
		{
			i++;
		}
		// Display given number
		System.out.print("\n Given number : " + num);
		if (i != n)
		{
			// Display the calculated result
			System.out.print("\n Ceiling : " + arr[i]+"\n");
		}
		else
		{
			// When ceiling of number number not exist
			System.out.print("\n Ceiling not exist\n");
		}
	}
	public static void main(String[] args)
	{
		Ceiling task = new Ceiling();
		// Display given array
		int[] arr = {
			1 , 9 , 12 , 44 , 45 , 52 , 70 , 230
		};
		// Get the number of element
		int n = arr.length;
		// Test
		task.findCeiling(arr, n, 64);
		task.findCeiling(arr, n, 0);
		task.findCeiling(arr, n, 117);
		task.findCeiling(arr, n, 240);
	}
}

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
// Include header file
#include <iostream>
using namespace std;

// C++ program
// Find ceiling in a sorted array
class Ceiling
{
	public:
		// Find the ceiling of given number
		void findCeiling(int arr[], int n, int num)
		{
			int i = 0;
			// Execute loop until array elements are not
			// Greater than given number
			while (i < n && arr[i] < num)
			{
				i++;
			}
			// Display given number
			cout << "\n Given number : " << num;
			if (i != n)
			{
				// Display the calculated result
				cout << "\n Ceiling : " << arr[i] << "\n";
			}
			else
			{
				// When ceiling of number number not exist
				cout << "\n Ceiling not exist\n";
			}
		}
};
int main()
{
	Ceiling task = Ceiling();
	// Display given array
	int arr[] = {
		1 , 9 , 12 , 44 , 45 , 52 , 70 , 230
	};
	// Get the number of element
	int n = sizeof(arr) / sizeof(arr[0]);
	// Test
	task.findCeiling(arr, n, 64);
	task.findCeiling(arr, n, 0);
	task.findCeiling(arr, n, 117);
	task.findCeiling(arr, n, 240);
	return 0;
}

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
// Include namespace system
using System;
// C# program
// Find ceiling in a sorted array
public class Ceiling
{
	// Find the ceiling of given number
	public void findCeiling(int[] arr, int n, int num)
	{
		int i = 0;
		// Execute loop until array elements are not
		// Greater than given number
		while (i < n && arr[i] < num)
		{
			i++;
		}
		// Display given number
		Console.Write("\n Given number : " + num);
		if (i != n)
		{
			// Display the calculated result
			Console.Write("\n Ceiling : " + arr[i] + "\n");
		}
		else
		{
			// When ceiling of number number not exist
			Console.Write("\n Ceiling not exist\n");
		}
	}
	public static void Main(String[] args)
	{
		Ceiling task = new Ceiling();
		// Display given array
		int[] arr = {
			1 , 9 , 12 , 44 , 45 , 52 , 70 , 230
		};
		// Get the number of element
		int n = arr.Length;
		// Test
		task.findCeiling(arr, n, 64);
		task.findCeiling(arr, n, 0);
		task.findCeiling(arr, n, 117);
		task.findCeiling(arr, n, 240);
	}
}

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
<?php
// Php program
// Find ceiling in a sorted array
class Ceiling
{
	// Find the ceiling of given number
	public	function findCeiling( & $arr, $n, $num)
	{
		$i = 0;
		// Execute loop until array elements are not
		// Greater than given number
		while ($i < $n && $arr[$i] < $num)
		{
			$i++;
		}
		// Display given number
		echo "\n Given number : ". $num;
		if ($i != $n)
		{
			// Display the calculated result
			echo "\n Ceiling : ". $arr[$i] ."\n";
		}
		else
		{
			// When ceiling of number number not exist
			echo "\n Ceiling not exist\n";
		}
	}
}

function main()
{
	$task = new Ceiling();
	// Display given array
	$arr = array(1, 9, 12, 44, 45, 52, 70, 230);
	// Get the number of element
	$n = count($arr);
	$task->findCeiling($arr, $n, 64);
	$task->findCeiling($arr, $n, 0);
	$task->findCeiling($arr, $n, 117);
	$task->findCeiling($arr, $n, 240);
}
main();

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
// Node Js program
// Find ceiling in a sorted array
class Ceiling
{
	// Find the ceiling of given number
	findCeiling(arr, n, num)
	{
		var i = 0;
		// Execute loop until array elements are not
		// Greater than given number
		while (i < n && arr[i] < num)
		{
			i++;
		}
		// Display given number
		process.stdout.write("\n Given number : " + num);
		if (i != n)
		{
			// Display the calculated result
			process.stdout.write("\n Ceiling : " + arr[i] + "\n");
		}
		else
		{
			// When ceiling of number number not exist
			process.stdout.write("\n Ceiling not exist\n");
		}
	}
}

function main()
{
	var task = new Ceiling();
	// Display given array
	var arr = [1, 9, 12, 44, 45, 52, 70, 230];
	// Get the number of element
	var n = arr.length;
	// Test
	task.findCeiling(arr, n, 64);
	task.findCeiling(arr, n, 0);
	task.findCeiling(arr, n, 117);
	task.findCeiling(arr, n, 240);
}
main();

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
#  Python 3 program
#  Find ceiling in a sorted array
class Ceiling :
	#  Find the ceiling of given number
	def findCeiling(self, arr, n, num) :
		i = 0
		#  Execute loop until list elements are not 
		#  Greater than given number
		while (i < n and arr[i] < num) :
			i += 1
		
		#  Display given number
		print("\n Given number : ", num, end = "")
		if (i != n) :
			#  Display the calculated result
			print("\n Ceiling : ", arr[i] )
		else :
			#  When ceiling of number number not exist
			print("\n Ceiling not exist")
		
def main() :
	task = Ceiling()
	#  Display given list
	arr = [1, 9, 12, 44, 45, 52, 70, 230]
	#  Get the number of element
	n = len(arr)
	#  Test
	task.findCeiling(arr, n, 64)
	task.findCeiling(arr, n, 0)
	task.findCeiling(arr, n, 117)
	task.findCeiling(arr, n, 240)

if __name__ == "__main__": main()

Output

 Given number :  64
 Ceiling :  70

 Given number :  0
 Ceiling :  1

 Given number :  117
 Ceiling :  230

 Given number :  240
 Ceiling not exist
#  Ruby program
#  Find ceiling in a sorted array
class Ceiling 
	#  Find the ceiling of given number
	def findCeiling(arr, n, num) 
		i = 0
		#  Execute loop until array elements are not 
		#  Greater than given number
		while (i < n && arr[i] < num) 
			i += 1
		end

		#  Display given number
		print("\n Given number : ", num)
		if (i != n) 
			#  Display the calculated result
			print("\n Ceiling : ", arr[i] ,"\n")
		else 
			#  When ceiling of number number not exist
			print("\n Ceiling not exist\n")
		end

	end

end

def main() 
	task = Ceiling.new()
	#  Display given array
	arr = [1, 9, 12, 44, 45, 52, 70, 230]
	#  Get the number of element
	n = arr.length
	#  Test
	task.findCeiling(arr, n, 64)
	task.findCeiling(arr, n, 0)
	task.findCeiling(arr, n, 117)
	task.findCeiling(arr, n, 240)
end

main()

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
// Scala program
// Find ceiling in a sorted array
class Ceiling
{
	// Find the ceiling of given number
	def findCeiling(arr: Array[Int], n: Int, num: Int): Unit = {
		var i: Int = 0;
		// Execute loop until array elements are not
		// Greater than given number
		while (i < n && arr(i) < num)
		{
			i += 1;
		}
		// Display given number
		print("\n Given number : " + num);
		if (i != n)
		{
			// Display the calculated result
			print("\n Ceiling : " + arr(i) + "\n");
		}
		else
		{
			// When ceiling of number number not exist
			print("\n Ceiling not exist\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Ceiling = new Ceiling();
		// Display given array
		var arr: Array[Int] = Array(1, 9, 12, 44, 45, 52, 70, 230);
		// Get the number of element
		var n: Int = arr.length;
		// Test
		task.findCeiling(arr, n, 64);
		task.findCeiling(arr, n, 0);
		task.findCeiling(arr, n, 117);
		task.findCeiling(arr, n, 240);
	}
}

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
// Swift 4 program
// Find ceiling in a sorted array
class Ceiling
{
	// Find the ceiling of given number
	func findCeiling(_ arr: [Int], _ n: Int, _ num: Int)
	{
		var i: Int = 0;
		// Execute loop until array elements are not
		// Greater than given number
		while (i < n && arr[i] < num)
		{
			i += 1;
		}
		// Display given number
		print("\n Given number : ", num, terminator: "");
		if (i  != n)
		{
			// Display the calculated result
			print("\n Ceiling : ", arr[i] );
		}
		else
		{
			// When ceiling of number number not exist
			print("\n Ceiling not exist");
		}
	}
}
func main()
{
	let task: Ceiling = Ceiling();
	// Display given array
	let arr: [Int] = [1, 9, 12, 44, 45, 52, 70, 230];
	// Get the number of element
	let n: Int = arr.count;
	// Test
	task.findCeiling(arr, n, 64);
	task.findCeiling(arr, n, 0);
	task.findCeiling(arr, n, 117);
	task.findCeiling(arr, n, 240);
}
main();

Output

 Given number :  64
 Ceiling :  70

 Given number :  0
 Ceiling :  1

 Given number :  117
 Ceiling :  230

 Given number :  240
 Ceiling not exist
// Kotlin program
// Find ceiling in a sorted array
class Ceiling
{
	// Find the ceiling of given number
	fun findCeiling(arr: Array < Int > , n: Int, num: Int): Unit
	{
		var i: Int = 0;
		// Execute loop until array elements are not
		// Greater than given number
		while (i < n && arr[i] < num)
		{
			i += 1;
		}
		// Display given number
		print("\n Given number : " + num);
		if (i != n)
		{
			// Display the calculated result
			print("\n Ceiling : " + arr[i] + "\n");
		}
		else
		{
			// When ceiling of number number not exist
			print("\n Ceiling not exist\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Ceiling = Ceiling();
	// Display given array
	var arr: Array < Int > = arrayOf(1, 9, 12, 44, 45, 52, 70, 230);
	// Get the number of element
	var n: Int = arr.count();
	// Test
	task.findCeiling(arr, n, 64);
	task.findCeiling(arr, n, 0);
	task.findCeiling(arr, n, 117);
	task.findCeiling(arr, n, 240);
}

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist
fn main()
{
	// Display given array
	let  arr = [
		1 , 9 , 12 , 44 , 45 , 52 , 70 , 230
	];
	// Get the number of element
	let n = arr.len();
	// Test
	find_ceiling(&arr, n, 64);
	find_ceiling(&arr, n, 0);
	find_ceiling(&arr, n, 117);
	find_ceiling(&arr, n, 240);
}
fn find_ceiling(arr: &[i32], n: usize, num: i32)
{
	let mut i: usize = 0;
	// Execute loop until array elements are not 
	// Greater than given number
	while i < n && arr[i] < num
	{
		i += 1;
	}
	// Display given number
	print!("\n Given number : {} ", num);
	if i != n
	{
		// Display the calculated result
		print!("\n Ceiling : {}\n", arr[i]);
	}
	else
	{
		// When ceiling of number number not exist
		print!("\n Ceiling not exist\n");
	}
}

Output

 Given number : 64
 Ceiling : 70

 Given number : 0
 Ceiling : 1

 Given number : 117
 Ceiling : 230

 Given number : 240
 Ceiling not exist


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