Harshad number In Python

          

        Harshad number In Python


Harshad Number is an integer that is divisible by the sum of its digits.

Program:

    n = int(input("Enter Number"))
    sum = 0
    temp = n
    while(temp > 0):
    sum += temp % 10
    temp = int(temp // 10)
    res = n % sum
    if(res == 0):
    print("Harshad Number");
    else:
    print("Not Harshad Number");

    OutPut:
    Enter Number1729
    Harshad Number

    Enter Number1930
    Not Harshad Number

    Automorphic Number In Given Range In Python

             

            Automorphic Number In Given Range In Python




    An automorphic number is a number whose square ends in the same digits as the 

    original number itself. Examples: 5, 76, etc.



    Program:

      def isAutomorphic(num):
        #square of the number
        sqr = num**2
        #number of digits in num 
        n = len(str(num))
        #last n digits of the square
        last = sqr%pow(10,n)
        return last == num


      n1=int(input("Enter Starting Value"))
      n2=int(input("Enter Ending Value")) 
      #loop through all the numbers in the range
      for x in range(n1,n2):
        if isAutomorphic(x):
          print(x,end=' ')

      OutPut:
      Enter Starting Value1
      Enter Ending Value100
      1 5 6 25 76 

      check whether the given number is Automorphic or not In Python

              

               check whether the given number is Automorphic or not.

       In Python


      An automorphic number is a number whose square ends in the same digits as the 

      original number itself. Examples: 5, 76, etc.



      Program:

        num = int(input("Enter a number:"))

        #find number of digits in num 
        n = len(str(num))
        #compute square
        sqr = num**2
        #Extract last n digits from the square
        last = sqr%pow(10,n)
        #compare last n digits with the input number
        if last == num:
          print("Automorphic Number")
        else:
          print("Not an Automorphic Number")

        OutPut:
        Enter a number: 
        5
        Automorphic Number


        Enter a number:25
        Automorphic Number

        Enter a number:26
        Not an Automorphic Number

        find if the given numbers are Friendly pair or not In Python

               

                find if the given numbers are Friendly pair or not 

         In Python


        For example,and 28 are Friendly Pair.

        (Sum of divisors of 6)/6 = (Sum of divisors of 28)/28.

        Program:

          n1 = int(input("Enter First Number"))
          n2 = int(input("Enter Second Number"))
          s1 = 0
          s2 = 0
          for i in range(1,n1):
          if(n1 % i == 0):
          s1 = s1 + i
          for i in range(1,n2):
          if(n2 % i == 0):
          s2 = s2 + i
          if(s1 == n1 and s2 == n2):
          print("Friendly Pair")
          else:
          print("Not Friendly Pair")


          OutPut:
          Enter First Number6
          Enter Second Number28
          Friendly Pair


          Enter First Number6
          Enter Second Number24
          Not Friendly Pair

          Perfect number Program In Python

                

                   Check whether a given number is perfect number or not

           In Python

          Program to check whether a given number is a perfect number or not is discussed here. A perfect number is a number which is equal to the sum of its proper positive divisors.

          For example, 6 is a perfect number.
          The divisors of 6 are 1, 2 and 3.
          1 + 2 + 3 = 6.
          Program:

            n = int(input("Enter a number : "))
            sum = 0
            for i in range (1,n):
                if(n % i == 0):
                    sum = sum + i
            if(sum == n):
                print(n,"is a perfect number")
            else:
                print(n,"is not a perfect number")


            OutPut:
            Enter a number : 6
            6 is a perfect number


            Enter a number : 123
            123 is not a perfect number

            Find the sum of numbers in a given range In Python

                 

                     Find the sum of numbers in a given range In Python

            Program:

              startnum = int(input("Enter Start Number" ))
              endnum = int(input("Enter Ending Number"))
              sum = 0
              for i in range(startnum, endnum + 1, 1):
              sum = sum + i
              print(sum)


              OutPut:
              Enter Start Number1
              Enter Ending Number10
              55

              Enter Start Number20
              Enter Ending Number30
              275

              Find Number of digits in Given Number In Python

                  

              Find Number of digits in Given Number  In Python 


              Program:


              n = int(input("Enter a number : "))
              count = 0
              while(n != 0):
                  n = int(n/10)
                  count = count + 1
              print("Number of digits in Given Number is :",count)



              OutPut:
              Enter a number : 1224
              Number of digits in Given Number is : 4







              find the largest among three numbers In Python

                 

              find the largest among three numbers  In Python 


              Program:


              a = int(input("Enter A"))
              b = int(input("Enter B"))
              c = int(input("Enter C"))
              if(a > b and a > c):
                  print("A is Max")
              elif(b > c and b > c):
                  print("B is Max")
              else:
                  print("C is Max")
                  



              OutPut:
              Enter A 12
              Enter B 45
              Enter C 6

              B is Max


              For More Information Visit: http://msktutorials.com/





              LCM of two numbers In Python

                    

              LCM of two numbers In Python 


              The Least Common Multiple (LCM) of a group of numbers is the smallest number that is a multiple of all the numbers.

              Program:

              a = int(input("Enter FIrst numbers"))
              b = int(input("Enter Second numbers"))
              lc = 0
              if(a > b):
               lcm = a
              else:
               lc = b
              while(1):
               if( lc % a == 0 and lc % b == 0 ):
                 print("LCM of",a,"and",b,"is",lc)
               break
               lc = lc + 1



              OutPut:
              Enter FIrst numbers7
              Enter Second numbers21
              LCM of 7 and 21 is 21

              Python program to check whether the character is an alphabet or not

                   

              Python program to check whether the character is an alphabet or not 


              Program:

              ch=input("Enter any character:")
              if((ch>='a' and ch<='z') or (ch>='A' and ch<='Z')):
                  print(ch," is an alphabet")
              else:
                
                  print(ch,"is not an alphabet")



              OutPut:
              Enter any character:2
              2 is not an alphabet

              Enter any character:d
              d  is an alphabet






              Python Program to Calculate the Power of a Number

                  

                 Python Program to Calculate the Power of a Number 


              Program:

              result = 1;
              base=int(input("Enter a base number: "))
                  
              exp=int(input("Enter an exponent: "))

              while (exp != 0):
                      result *= base;
                      exp=exp-1;
              print("Answer =", result);



              OutPut:
              Enter a base number: 3
              Enter an exponent: 4
              Answer = 81

              Enter a base number: 2
              Enter an exponent: 3
              Answer = 8







              GCD Using While Loop In Python

                 

                 GCD Using While Loop In Python 

              The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder).

              GCD: greatest common divisor 


              Program:

              n1=int(input("Enter n1 integer: "))
              n2=int(input("Enter n2 integer: "))       
                  
              while(n1!=n2):

                      if(n1 > n2):
                          n1 -= n2;
                      else:
                          n2 -= n1;

              print("GCD = ",n1);


              OutPut:
              Enter n1 integer: 81
              Enter 21 integer: 153
              GCD =  9








              Reverse number without recursion in Python

                

                       Reverse number  in Python 

              Python program to reverse a number and to print it on the screen. For example, if the input is 123, the output will be 321