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

Pubblicato il: 01 aprile 2024
sul canale di: 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


In questa pagina del sito puoi guardare il video online Python: Harshad or Niven Number | Python Interview Questions | DE Interview Questions | Data Science della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Grow with Data 01 aprile 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 103 volte e gli è piaciuto 8 spettatori. Buona visione!