Lucky numbers program
Here given code implementation process.
// C program
// Lucky numbers program
#include <stdio.h>
void isLuckyNo(int num)
{
// Define some auxiliary variables
int n = num;
int position = 2;
int result = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Change n
n = n - n / position;
// Increase position
position++;
}
}
if (result == 1)
{
printf(" %d is lucky number\n", num);
}
else
{
printf(" %d is not lucky number\n", num);
}
}
int main(int argc, char
const *argv[])
{
// Test
isLuckyNo(9);
isLuckyNo(1);
isLuckyNo(21);
isLuckyNo(13);
isLuckyNo(1321);
return 0;
}
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Java program
// Lucky numbers program
// iterative solution
public class LuckyNumber
{
// Lucky numbers program
public void isLuckyNo(int num)
{
// Define some auxiliary variables
int n = num;
int position = 2;
int result = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Change n
n = n - n / position;
// Increase position
position++;
}
}
if (result == 1)
{
System.out.print(" " + num + " is lucky number\n");
}
else
{
System.out.print(" " + num + " is not lucky number\n");
}
}
public static void main(String[] args)
{
LuckyNumber task = new LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
}
}
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Include header file
#include <iostream>
using namespace std;
// C++ program
// Lucky numbers program
// iterative solution
class LuckyNumber
{
public:
// Lucky numbers program
void isLuckyNo(int num)
{
// Define some auxiliary variables
int n = num;
int position = 2;
int result = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Increase position
// Change n
n = n - n / position;
position++;
}
}
if (result == 1)
{
cout << " " << num << " is lucky number\n";
}
else
{
cout << " " << num << " is not lucky number\n";
}
}
};
int main()
{
LuckyNumber task = LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
return 0;
}
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Include namespace system
using System;
// C# program
// Lucky numbers program
// iterative solution
public class LuckyNumber
{
// Lucky numbers program
public void isLuckyNo(int num)
{
// Define some auxiliary variables
int n = num;
int position = 2;
int result = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Increase position
// Change n
n = n - n / position;
position++;
}
}
if (result == 1)
{
Console.Write(" " + num + " is lucky number\n");
}
else
{
Console.Write(" " + num + " is not lucky number\n");
}
}
public static void Main(String[] args)
{
LuckyNumber task = new LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
}
}
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
<?php
// Php program
// Lucky numbers program
// iterative solution
class LuckyNumber
{
// Lucky numbers program
public function isLuckyNo($num)
{
// Define some auxiliary variables
$n = $num;
$position = 2;
$result = 0;
// Execute the loop until not decide given number is
// lucky or not
while ($result == 0)
{
if ($position > $n)
{
// When number is Lucky
$result = 1;
}
else if (($n % $position) == 0)
{
// When n is divisible by position
$result = -1;
}
else
{
// Increase position
// Change n
$n = $n - intval($n / $position);
$position++;
}
}
if ($result == 1)
{
echo " ". $num ." is lucky number\n";
}
else
{
echo " ". $num ." is not lucky number\n";
}
}
}
function main()
{
$task = new LuckyNumber();
$task->isLuckyNo(9);
$task->isLuckyNo(1);
$task->isLuckyNo(21);
$task->isLuckyNo(13);
$task->isLuckyNo(1321);
}
main();
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Node Js program
// Lucky numbers program
// iterative solution
class LuckyNumber
{
// Lucky numbers program
isLuckyNo(num)
{
// Define some auxiliary variables
var n = num;
var position = 2;
var result = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Increase position
// Change n
n = n - parseInt(n / position);
position++;
}
}
if (result == 1)
{
process.stdout.write(" " + num + " is lucky number\n");
}
else
{
process.stdout.write(" " + num + " is not lucky number\n");
}
}
}
function main()
{
var task = new LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
}
main();
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
# Python 3 program
# Lucky numbers program
# iterative solution
class LuckyNumber :
# Lucky numbers program
def isLuckyNo(self, num) :
# Define some auxiliary variables
n = num
position = 2
result = 0
# Execute the loop until not decide given number is
# lucky or not
while (result == 0) :
if (position > n) :
# When number is Lucky
result = 1
elif((n % position) == 0) :
# When n is divisible by position
result = -1
else :
# Change n
n = n - int(n / position)
# Increase position
position += 1
if (result == 1) :
print(" ", num ," is lucky number")
else :
print(" ", num ," is not lucky number")
def main() :
task = LuckyNumber()
# Test
task.isLuckyNo(9)
task.isLuckyNo(1)
task.isLuckyNo(21)
task.isLuckyNo(13)
task.isLuckyNo(1321)
if __name__ == "__main__": main()
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
# Ruby program
# Lucky numbers program
# iterative solution
class LuckyNumber
# Lucky numbers program
def isLuckyNo(num)
# Define some auxiliary variables
n = num
position = 2
result = 0
# Execute the loop until not decide given number is
# lucky or not
while (result == 0)
if (position > n)
# When number is Lucky
result = 1
elsif((n % position) == 0)
# When n is divisible by position
result = -1
else
# Change n
n = n - n / position
# Increase position
position += 1
end
end
if (result == 1)
print(" ", num ," is lucky number\n")
else
print(" ", num ," is not lucky number\n")
end
end
end
def main()
task = LuckyNumber.new()
# Test
task.isLuckyNo(9)
task.isLuckyNo(1)
task.isLuckyNo(21)
task.isLuckyNo(13)
task.isLuckyNo(1321)
end
main()
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Scala program
// Lucky numbers program
// iterative solution
class LuckyNumber
{
// Lucky numbers program
def isLuckyNo(num: Int): Unit = {
// Define some auxiliary variables
var n: Int = num;
var position: Int = 2;
var result: Int = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Increase position
// Change n
n = n - (n / position).toInt;
position += 1;
}
}
if (result == 1)
{
print(" " + num + " is lucky number\n");
}
else
{
print(" " + num + " is not lucky number\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: LuckyNumber = new LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
}
}
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Swift 4 program
// Lucky numbers program
// iterative solution
class LuckyNumber
{
// Lucky numbers program
func isLuckyNo(_ num: Int)
{
// Define some auxiliary variables
var n: Int = num;
var position: Int = 2;
var result: Int = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Increase position
// Change n
n = n - n / position;
position += 1;
}
}
if (result == 1)
{
print(" ", num ," is lucky number");
}
else
{
print(" ", num ," is not lucky number");
}
}
}
func main()
{
let task: LuckyNumber = LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
}
main();
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
// Kotlin program
// Lucky numbers program
// iterative solution
class LuckyNumber
{
// Lucky numbers program
fun isLuckyNo(num: Int): Unit
{
// Define some auxiliary variables
var n: Int = num;
var position: Int = 2;
var result: Int = 0;
// Execute the loop until not decide given number is
// lucky or not
while (result == 0)
{
if (position > n)
{
// When number is Lucky
result = 1;
}
else if ((n % position) == 0)
{
// When n is divisible by position
result = -1;
}
else
{
// Increase position
// Change n
n = n - n / position;
position += 1;
}
}
if (result == 1)
{
print(" " + num + " is lucky number\n");
}
else
{
print(" " + num + " is not lucky number\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: LuckyNumber = LuckyNumber();
// Test
task.isLuckyNo(9);
task.isLuckyNo(1);
task.isLuckyNo(21);
task.isLuckyNo(13);
task.isLuckyNo(1321);
}
Output
9 is not lucky number
1 is lucky number
21 is not lucky number
13 is lucky number
1321 is lucky number
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