Posted on by Kalkicode
Code Number

Nelson Number Program

The Nelson number problem is an interesting mathematical concept. A Nelson number is a number that is divisible by 111. It is named after the mathematician Edward Nelson, who worked in the field of logic and mathematical physics. In this problem, we are given a number, and our task is to determine whether it is a Nelson number or not.

Problem Statement

Given an integer input, we need to check whether it is a Nelson number or not. If the number is divisible by 111, then it is a Nelson number; otherwise, it is not.

Explanation with Suitable Example

Let's take an example to understand the Nelson number problem better. Consider the number 666. To check if it is a Nelson number, we divide it by 111:

666 ÷ 111 = 6

Since the remainder is 0, 666 is a Nelson number.

Standard Pseudocode

function isNelsonNumber(number):
    if number % 111 == 0:
        return True
    else:
        return False

Algorithm with Explanation

  1. Start the program.
  2. Define the function isNelsonNumber(number) that takes an integer number as input.
  3. Check if the number is divisible by 111 using the modulo operator (%).
  4. If the result of number % 111 is equal to 0, return True, indicating that the number is a Nelson number.
  5. If the result is not 0, return False, indicating that the number is not a Nelson number.
  6. End the function.
  7. In the main() function, call the isNelsonNumber() function for different test cases to check if they are Nelson numbers or not.
  8. Print the result for each test case.
  9. End the program.

Code Solution

Here given code implementation process.

// C program
// Nelson Number Program 
#include <stdio.h>

// Determine that given number is Nelson Number or not
void isNelsonNo(int number)
{
	if ((number % 111) == 0)
	{
		// When number is nelson
		printf(" %d is nelson number \n", number);
	}
	else
	{
		printf(" %d is not nelson number \n", number);
	}
}
int main()
{
	// Test Case
	isNelsonNo(444);
	isNelsonNo(113);
	isNelsonNo(333);
	return 0;
}

input

 444 is nelson number
 113 is not nelson number
 333 is nelson number
/*
  Java Program for
  Nelson Number 
*/
class NelsonNo
{
	// Determine that given number is Nelson Number or not
	public void isNelsonNo(int number)
	{
		if ((number % 111) == 0)
		{
			// When number is nelson
			System.out.println(" [" + number + "] is nelson number");
		}
		else
		{
			System.out.println(" [" + number + "] is not nelson number");
		}
	}
	public static void main(String[] args)
	{
		NelsonNo task = new NelsonNo();
		// Test Case
		task.isNelsonNo(444);
		task.isNelsonNo(113);
		task.isNelsonNo(333);
	}
}

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Nelson Number 
*/

class NelsonNo
{
	public:
		// Determine that given number is Nelson Number or not
		void isNelsonNo(int number)
		{
			if ((number % 111) == 0)
			{
				// When number is nelson
				cout << " [" << number << "] is nelson number" << endl;
			}
			else
			{
				cout << " [" << number << "] is not nelson number" << endl;
			}
		}
};
int main()
{
	NelsonNo *task = new NelsonNo();
	// Test Case
	task->isNelsonNo(444);
	task->isNelsonNo(113);
	task->isNelsonNo(333);
	return 0;
}

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
// Include namespace system
using System;
/*
  Csharp Program for
  Nelson Number 
*/
public class NelsonNo
{
	// Determine that given number is Nelson Number or not
	public void isNelsonNo(int number)
	{
		if ((number % 111) == 0)
		{
			// When number is nelson
			Console.WriteLine(" [" + number + "] is nelson number");
		}
		else
		{
			Console.WriteLine(" [" + number + "] is not nelson number");
		}
	}
	public static void Main(String[] args)
	{
		NelsonNo task = new NelsonNo();
		// Test Case
		task.isNelsonNo(444);
		task.isNelsonNo(113);
		task.isNelsonNo(333);
	}
}

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
<?php
/*
  Php Program for
  Nelson Number 
*/
class NelsonNo
{
	// Determine that given number is Nelson Number or not
	public	function isNelsonNo($number)
	{
		if (($number % 111) == 0)
		{
			// When number is nelson
			echo " [".$number.
			"] is nelson number".
			"\n";
		}
		else
		{
			echo " [".$number.
			"] is not nelson number".
			"\n";
		}
	}
}

function main()
{
	$task = new NelsonNo();
	// Test Case
	$task->isNelsonNo(444);
	$task->isNelsonNo(113);
	$task->isNelsonNo(333);
}
main();

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
/*
  Node JS Program for
  Nelson Number 
*/
class NelsonNo
{
	// Determine that given number is Nelson Number or not
	isNelsonNo(number)
	{
		if ((number % 111) == 0)
		{
			// When number is nelson
			console.log(" [" + number + "] is nelson number");
		}
		else
		{
			console.log(" [" + number + "] is not nelson number");
		}
	}
}

function main()
{
	var task = new NelsonNo();
	// Test Case
	task.isNelsonNo(444);
	task.isNelsonNo(113);
	task.isNelsonNo(333);
}
main();

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
#  Python 3 Program for
#  Nelson Number 
class NelsonNo :
	#  Determine that given number is Nelson Number or not
	def isNelsonNo(self, number) :
		if ((number % 111) == 0) :
			#  When number is nelson
			print(" [", number ,"] is nelson number")
		else :
			print(" [", number ,"] is not nelson number")
		
	

def main() :
	task = NelsonNo()
	#  Test Case
	task.isNelsonNo(444)
	task.isNelsonNo(113)
	task.isNelsonNo(333)

if __name__ == "__main__": main()

input

 [ 444 ] is nelson number
 [ 113 ] is not nelson number
 [ 333 ] is nelson number
#  Ruby Program for
#  Nelson Number 
class NelsonNo 
	#  Determine that given number is Nelson Number or not
	def isNelsonNo(number) 
		if ((number % 111) == 0) 
			#  When number is nelson
			print(" [", number ,"] is nelson number", "\n")
		else 
			print(" [", number ,"] is not nelson number", "\n")
		end

	end

end

def main() 
	task = NelsonNo.new()
	#  Test Case
	task.isNelsonNo(444)
	task.isNelsonNo(113)
	task.isNelsonNo(333)
end

main()

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
/*
  Scala Program for
  Nelson Number 
*/
class NelsonNo()
{
	// Determine that given number is Nelson Number or not
	def isNelsonNo(number: Int): Unit = {
		if ((number % 111) == 0)
		{
			// When number is nelson
			println(" [" + number + "] is nelson number");
		}
		else
		{
			println(" [" + number + "] is not nelson number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: NelsonNo = new NelsonNo();
		// Test Case
		task.isNelsonNo(444);
		task.isNelsonNo(113);
		task.isNelsonNo(333);
	}
}

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number
/*
  Swift 4 Program for
  Nelson Number 
*/
class NelsonNo
{
	// Determine that given number is Nelson Number or not
	func isNelsonNo(_ number: Int)
	{
		if ((number % 111) == 0)
		{
			// When number is nelson
			print(" [", number ,"] is nelson number");
		}
		else
		{
			print(" [", number ,"] is not nelson number");
		}
	}
}
func main()
{
	let task: NelsonNo = NelsonNo();
	// Test Case
	task.isNelsonNo(444);
	task.isNelsonNo(113);
	task.isNelsonNo(333);
}
main();

input

 [ 444 ] is nelson number
 [ 113 ] is not nelson number
 [ 333 ] is nelson number
/*
  Kotlin Program for
  Nelson Number 
*/
class NelsonNo
{
	// Determine that given number is Nelson Number or not
	fun isNelsonNo(number: Int): Unit
	{
		if ((number % 111) == 0)
		{
			// When number is nelson
			println(" [" + number + "] is nelson number");
		}
		else
		{
			println(" [" + number + "] is not nelson number");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: NelsonNo = NelsonNo();
	// Test Case
	task.isNelsonNo(444);
	task.isNelsonNo(113);
	task.isNelsonNo(333);
}

input

 [444] is nelson number
 [113] is not nelson number
 [333] is nelson number

Resultant Output Explanation

Here's the explanation of each test case:

  1. Test Case 1 (444): 444 is divisible by 111, so it is a Nelson number.
  2. Test Case 2 (113): 113 is not divisible by 111, so it is not a Nelson number.
  3. Test Case 3 (333): 333 is divisible by 111, so it is a Nelson number.

Time Complexity of the Code

The time complexity of the provided code is O(1) because the function isNelsonNumber() performs a constant number of operations, regardless of the magnitude of the input number. The operations involved are simple arithmetic operations and a single modulo operation. Thus, the time complexity remains constant for any input size.

Comment

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