Python: Harshad or Niven Number | Python Interview Questions | DE Interview Questions | Data Science

Veröffentlicht am: 01 April 2024
auf dem Kanal: Grow with Data
103
8

Video 157: In this video, we'll explore how to find the given number is a Harshad number or not.

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.

In this video, we delve into the concept of Harshad numbers, which are integers divisible by the sum of their digits. We explore three different Python approaches to determine if a given number is a Harshad number or not.

** Approach 1: General Looping Logic **
In this approach, we use a simple loop to iterate through the digits of the given number, calculate their sum, and then check if the number is divisible by this sum. If it is divisible, we return the sum of the digits; otherwise, we return -1.

** Approach 2: Using List Comprehension **
Here, we leverage the power of list comprehension to succinctly sum the digits of the given number. We calculate the sum of digits using a list comprehension and then check if the number is divisible by this sum. If divisible, we return the sum of the digits; otherwise, we return -1.

** Approach 3: Using While Loop **
In this approach, we employ a while loop to iteratively extract digits from the given number, calculate their sum, and then check if the number is divisible by this sum. We continue this process until we have processed all the digits. If the number is divisible by the sum of its digits, we return the sum; otherwise, we return -1.

For a comprehensive understanding and practical demonstration, be sure to watch the entire video. Happy coding!

code: https://github.com/jeganpillai/python...

Follow me on,
Website : https://growwithdata.co/
YouTube :    / @growwithdata  
TikTok :   / growwithdata  
LinkedIn :   / growwithdata  
Facebook :   / growwithdata.co  
FB Group : facebook.com/groups/datainterviewpreparation
twitter :   / growwithdata_co  
WhatsApp : https://whatsapp.com/channel/0029VaF8...
Instagram :   / growwithdata.co  


#recursive #countingalgorithm #list #looping #listmanipulation #uniquesubstring #longestsubstring #slicing #lineartimecomplexity #python #pythonquestions #pythontest #pythonprogramming #pythontutorial #python3 #pythonforbeginners #interviewquestions #interview #dataengineers #deinterview #pythoninterview #interviewquestions #leetcode #placementpreparation #meta #google #facebook #apple #netflix #amazon #google #faang #maanga #dataengineers #alphanumeric #nonalphanumeric #growwithdata #set #split #remove #pythonfunctions #tamil #tamilpython #tamilinterview #tamilinterviewlatest #PythonInterviewQuestions #PythonInterviewQuestionsAndAnswers #ImportantPythonInterviewQuestions #PrepareForPythonJobs #PythonJobInterviewPreparation #TopPythonInterviewQuestions #CommonPythonInterviewQuestions #growwithdata


Auf dieser Seite können Sie das Online-Video Python: Harshad or Niven Number | Python Interview Questions | DE Interview Questions | Data Science mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Grow with Data 01 April 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 103 Mal angesehen und es wurde von 8 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!