Harshad Number

Here given code implementation process.

/*
  C Program 
+ Check if a given number is Harshad Number or not
*/
#include<stdio.h>
#include<math.h>

//Returning the sum of digit in given number
int digit_sum(int number)
{
  int sum = 0; 

  while (number!=0) 
  { 
    //sum of digit
    sum+=number%10;
    number/=10; 
  } 
  return sum;
}
void is_harshad_no(int number)
{
  //When given number digit sum  divisible remainder is zero?
  if(number % digit_sum(number) == 0)
  {
    //When Yes
    printf("%d Is an Harshad Number\n",number);
  }
  else
  {
    //When No
    printf("%d Is not an Harshad Number\n",number);
  }
 
}
int main(){

  //Test Case
  is_harshad_no(72);
  is_harshad_no(46);
  is_harshad_no(135);
  is_harshad_no(57);
 
  return 0;
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
 C++ Program
 Check if a given number is Harshad Number or not
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Returning the sum of digit in given number
		int digit_sum(int number) {
			int sum = 0;
			while (number != 0) {
				//sum of digit
				sum += number % 10;
				number /= 10;
			}
			return sum;
		}
	//When given number digit sum  divisible remainder is zero
	void is_harshad_no(int number) {
		if (number % this->digit_sum(number) == 0) {
			//When Yes

			cout << number << " Is an Harshad Number\n";
		} else {
			//When No

			cout << number << " Is not an Harshad Number\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.is_harshad_no(72);
	obj.is_harshad_no(46);
	obj.is_harshad_no(135);
	obj.is_harshad_no(57);
	return 0;
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
  Java Program
  Check if a given number is Harshad Number or not
*/
public class MyNumber {
 
  //Returning the sum of digit in given number
  public int digit_sum(int number)
  {
    int sum = 0; 

    while (number!=0) 
    { 
      //sum of digit
      sum+=number%10;
      number/=10; 
    } 
    return sum;
  }
  //When given number digit sum  divisible remainder is zero
  public void is_harshad_no(int number)
  {

    if(number % digit_sum(number) == 0)
    {
      //When Yes
     System.out.print(number+" Is an Harshad Number\n");
    }
    else
    {
      //When No
      System.out.print(number+" Is not an Harshad Number\n");
    }
   
  }
 
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Test Case
    obj.is_harshad_no(72);
    obj.is_harshad_no(46);
    obj.is_harshad_no(135);
    obj.is_harshad_no(57);


  }
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
  C# Program
  Check if a given number is Harshad Number or not
*/
using System;
public class MyNumber {

	//Returning the sum of digit in given number
	public int digit_sum(int number) {
		int sum = 0;

		while (number != 0) {
			//sum of digit
			sum += number % 10;
			number /= 10;
		}
		return sum;
	}
	//When given number digit sum  divisible reMainder is zero
	public void is_harshad_no(int number) {

		if (number % digit_sum(number) == 0) {
			//When Yes
			Console.Write(number + " Is an Harshad Number\n");
		} else {
			//When No
			Console.Write(number + " Is not an Harshad Number\n");
		}

	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		//Test Case
		obj.is_harshad_no(72);
		obj.is_harshad_no(46);
		obj.is_harshad_no(135);
		obj.is_harshad_no(57);


	}
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
# Python 3 Program
# Check if a given number is Harshad Number or not
class MyNumber :
	# Returning the sum of digit in given number
	def digit_sum(self, number) :
		sum = 0
		while (number != 0) :
			# sum of digit
			sum += number % 10
			number = int(number / 10)
		
		return sum
	
	# When given number digit sum  divisible remainder is zero
	def is_harshad_no(self, number) :
		if (number % self.digit_sum(number) == 0) :
			# When Yes
			print(number ," Is an Harshad Number")
		else :
			# When No
			print(number ," Is not an Harshad Number")
		
	

def main() :
	obj = MyNumber()
	# Test Case
	obj.is_harshad_no(72)
	obj.is_harshad_no(46)
	obj.is_harshad_no(135)
	obj.is_harshad_no(57)


if __name__ == "__main__":
	main()

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
# Ruby Program 
# Check if a given number is Harshad Number or not
class MyNumber 
	# Returning the sum of digit in given number
	def digit_sum(number) 
		sum = 0
		while (number != 0) 
			# sum of digit
			sum += number % 10
			number /= 10
		end
		return sum
	end
	# When given number digit sum  divisible remainder is zero
	def is_harshad_no(number) 
		if (number % self.digit_sum(number) == 0) 
			# When Yes

			print(number ," Is an Harshad Number\n")
		else 
			# When No

			print(number ," Is not an Harshad Number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.is_harshad_no(72)
	obj.is_harshad_no(46)
	obj.is_harshad_no(135)
	obj.is_harshad_no(57)
end
main()

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
 Scala Program
 Check if a given number is Harshad Number or not
*/
class MyNumber {
	//Returning the sum of digit in given number
	def digit_sum(value: Int): Int = {
		var sum: Int = 0;
        var number : Int = value;
		while (number != 0) {
			//sum of digit
			sum += number % 10;
			number = (number / 10).toInt;
		}
		return sum;
	}
	//When given number digit sum  divisible remainder is zero
	def is_harshad_no(number: Int): Unit = {
		if (number % this.digit_sum(number) == 0) {
			//When Yes
			print(s"$number Is an Harshad Number\n");
		} else {
			//When No
			print(s"$number Is not an Harshad Number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.is_harshad_no(72);
        obj.is_harshad_no(46);
        obj.is_harshad_no(135);
        obj.is_harshad_no(57);
	}
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
  Swift 4 Program
  Check if a given number is Harshad Number or not
*/
class MyNumber {
	//Returning the sum of digit in given number
	func digit_sum(_ value: Int) -> Int {
		var sum: Int = 0;
        var number: Int = value;
		while (number != 0) {
			//sum of digit
			sum += number % 10;
			number /= 10;
		}
		return sum;
	}
	//When given number digit sum  divisible remainder is zero
	func is_harshad_no(_ number: Int) {
		if (number % self.digit_sum(number) == 0) {
			//When Yes
			print(number ," Is an Harshad Number");
		} else {
			//When No
			print(number ," Is not an Harshad Number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.is_harshad_no(72);
	obj.is_harshad_no(46);
	obj.is_harshad_no(135);
	obj.is_harshad_no(57);
}
main();

Output

72  Is an Harshad Number
46  Is not an Harshad Number
135  Is an Harshad Number
57  Is not an Harshad Number
<?php
/*
  Php Program
  Check if a given number is Harshad Number or not
*/
class MyNumber {
	//Returning the sum of digit in given number

	public 	function digit_sum($number) {
		$sum = 0;
		while ($number != 0) {
			//sum of digit
			$sum += $number % 10;
			$number = intval($number / 10);
		}
		return $sum;
	}
	//When given number digit sum  divisible remainder is zero

	public 	function is_harshad_no($number) {
		if ($number % $this->digit_sum($number) == 0) {
			//When Yes

			echo($number ." Is an Harshad Number\n");
		} else {
			//When No

			echo($number ." Is not an Harshad Number\n");
		}
	}
};

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

	$obj->is_harshad_no(72);
	$obj->is_harshad_no(46);
	$obj->is_harshad_no(135);
	$obj->is_harshad_no(57);
}
main();

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
 Node Js Program
 Check if a given number is Harshad Number or not
*/
class MyNumber {
	//Returning the sum of digit in given number
	digit_sum(number) {
		var sum = 0;
		while (number != 0) {
			//sum of digit
			sum += number % 10;
			number = parseInt(number / 10);
		}
		return sum;
	}
	//When given number digit sum  divisible remainder is zero
	is_harshad_no(number) {
		if (number % this.digit_sum(number) == 0) {
			//When Yes

			process.stdout.write(number + " Is an Harshad Number\n");
		} else {
			//When No

			process.stdout.write(number + " Is not an Harshad Number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.is_harshad_no(72);
	obj.is_harshad_no(46);
	obj.is_harshad_no(135);
	obj.is_harshad_no(57);
}
main();

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad 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







© 2021, kalkicode.com, All rights reserved