How to resolve the algorithm Harshad or Niven series step by step in the Groovy programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Harshad or Niven series step by step in the Groovy programming language
Table of Contents
Problem Statement
The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example, 42 is a Harshad number as 42 is divisible by (4 + 2) without remainder. Assume that the series is defined as the numbers in increasing order.
The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:
Show your output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the Groovy programming language
Source code in the groovy programming language
class HarshadNiven{ public static boolean find(int x)
{
int sum = 0,temp,var;
var = x;
while(x>0)
{
temp = x%10;
sum = sum + temp;
x = x/10;
}
if(var%sum==0) temp = 1;
else temp = 0;
return temp;
}
public static void main(String[] args)
{
int t,i;
t = 0;
for(i=1;t<20;i++)
{
if(find(i))
{
print(i + " ");
t++;
}
}
int x = 0;
int y = 1000;
while(x!=1)
{
if(find(y)) x = 1;
y++;
}
println();
println(y+1);
}
}
You may also check:How to resolve the algorithm String append step by step in the Tcl programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Phix programming language
You may also check:How to resolve the algorithm Undefined values step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Global Script programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the uBasic/4tH programming language