Harshad Number

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
An integer divisible by the sum of its digits is said to be a Harshad number. You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1
LeetCode Problem - 3099
class Solution {
// Function to calculate the sum of the digits of a number and check if it's a Harshad number
public int sumOfTheDigitsOfHarshadNumber(int x) {
// Convert the number to a string
String str = String.valueOf(x);
// Initialize sum to 0
int sum = 0;
// Iterate through each digit in the number
for(char c : str.toCharArray()){
// Convert the character to integer and add it to the sum
sum += Integer.parseInt(String.valueOf(c));
}
// Check if the number is divisible by the sum of its digits (i.e., it's a Harshad number)
if(x % sum == 0){
return sum; // Return the sum if it's a Harshad number
}
return -1; // Return -1 if it's not a Harshad number
}
}




