Area Of Equilateral Triangle In Python

                 

    Area Of Equilateral Triangle In Python

This is how an equilateral triangle looks like:

Equilateral Triangle

As you can see, this is an equilateral triangle with a side length of “a” units and a height of “h” units.

The altitude of an equilateral triangle cuts the opposite side at right angles. So, by using the Pythagoras theorem, we can calculate the height of an equilateral triangle to be sqrt(3/4)a.

Thus, the area of an equilateral triangle = 1/2 * sqrt (3/4)a * a

Area = (sqrt 3)/4 * a^2

Program:

    import math
    a=float(input("enter side of the triangle: "));
    area=(math.sqrt(3)/4)*a*a;
    print("Area Of Equilateral Triangle ",area);

    OutPut:
    enter side of the triangle: 10
    Area Of Equilateral Triangle  43.301270189221924

    Simple Calculator Program In Python

                    

        Simple Calculator Program In Python


    Program:

      # define functions  
      def add(x, y):  
         
         return x + y 
      def sub(x, y): 
         
         return x - y 
      def mul(x, y): 
         
         return x * y 
      def div(x, y): 
         
         return x / y  
      # take input from the user  
      print("Select operation.")  
      print("1.Addition")  
      print("2.Subtraction")  
      print("3.Multiplication")  
      print("4.Division")  
        
      choice = input("Enter choice(1/2/3/4):")  
        
      num1 = int(input("Enter first number: "))  
      num2 = int(input("Enter second number: "))  
        
      if choice == '1':  
         print(num1,"+",num2,"=", add(num1,num2))  
        
      elif choice == '2':  
         print(num1,"-",num2,"=", sub(num1,num2))  
        
      elif choice == '3':  
         print(num1,"*",num2,"=", mul(num1,num2))  
      elif choice == '4':  
         print(num1,"/",num2,"=", div(num1,num2))  
      else:  
         print("Invalid input")  

      OutPut:
      Select operation.
      1.Addition
      2.Subtraction
      3.Multiplication
      4.Division
      Enter choice(1/2/3/4):2
      Enter first number: 12
      Enter second number: 23
      12 - 23 = -11

      Check whether a number can be expressed as a sum of two prime numbers In Python

                     

          Check whether a number can be expressed as a sum of two prime numbers In Python

      For example, the number 34 is given as input.
      
      34 = 3 + 31
      34 = 5 + 29
      34 = 11 + 23
      34 = 17 + 17

      Program:

        # Python program to check whether a number can be expressed as a sum of two prime numbers

        def prime(n):
            isPrime = 1
            for i in range (2,int(n/2),1):
                if(n % i == 0):
                    isPrime = 0
                    break
            return isPrime

        n = int(input("Enter a number : "))
        f = 0;
        i = 2
        for i in range (2,int(n/2),1):
            if(prime(i) == 1):
                if(prime(n-i) == 1):
                    print(n,"can be expressed as the sum of",i,"and",n-i)
                    f = 1;
        if (f == 0):
            print(n,"cannot be expressed as the sum of two prime numbers")

        OutPut:
        Enter a number : 34
        34 can be expressed as the sum of 3 and 31
        34 can be expressed as the sum of 5 and 29
        34 can be expressed as the sum of 11 and 23


        Enter a number : 35
        35 can be expressed as the sum of 4 and 31

        Enter a number : 37
        37 cannot be expressed as the sum of two prime numbers

        replace all 0's with 1 in a given integer In Python

                      

                Replace all 0's with 1 in a given integer In Python

        Input: 102405

        Output: 112415

        Program:

          # Python program to replace all 0’s with 1 in a given integer

          def re(n):

              if (n == 0):
                  return 0;



          # Extract the last digit and change it if needed

              digit = n % 10

              if (digit == 0):

                  digit = 1
              return re(int(n/10)) * 10 + digit

          n = int(input("Enter the number : "))

          print("Number after replacement : ",re(n))

          OutPut:
          Enter the number : 123034
          Number after replacement :  123134

          Python program to print the Armstrong numbers between the two intervals

                       

                  Python program to print the Armstrong numbers between the two intervals

          An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153  is an Armstrong

          Program:

            # Python program to print the Armstrong numbers between the two intervals

            import math
            n1 = int(input("Enter start value : "))
            n2 = int(input("Enter end value : "))
            result = 0
            n = 0
            for i in range(n1+1 ,n2,1):
                t2 = i
                t1 = i

                while (t1 != 0):
                    t1 = int(t1/10)
                    n = n + 1

                while (t2 != 0):
                    remainder = t2 % 10
                    result = result + math.pow(remainder, n)
                    t2 = int(t2/10)

                if (result == i):
                    print(i)

                n = 0
                result = 0

            OutPut:
            Enter start value : 100
            Enter end value : 500
            153
            370
            371
            407


            Enter start value : 200
            Enter end value : 1000
            370
            371
            407

            Prime Number Program in Given Range in Python

              

                     Prime Number Program in Given Range in Python

            Prime number in C: Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers.

            factors Program In Python

                        

                    Factors Program In Python


            the factors of number 10 are

            1 * 10 = 10

            2 * 5 = 10

            5* 2 = 10

            10* 1 = 10

            Program:

              n = int(input("Enter a number : "))
              count = 0
              for i in range(1, n+1, 1):
                  if(n % i == 0):
                      count = count + 1
                      print(i, end = " ")
              print("\nTotal factors of",n," is :",count)

              OutPut:
              Enter a number : 10
              1 2 5 10 
              Total factors of 10 : 4

              Enter a number : 20
              1 2 4 5 10 20 
              Total factors of 20  is : 6

              Abundant number In Python

                         

                      Abundant number In Python


              An abundant number is a number for which the sum of its proper divisors is greater than the

               number itself.

              The divisors of 12 are 1, 2, 3, 4 and 6.

              The sum of divisors of 12 is 16.

              12 < 16.Hence, 12 is an abundant number.

              Program:

                n = int(input("Enter N Value"))
                sum = 0
                for i in range(1,n):
                if(n % i == 0):
                sum = sum + i
                if(n < sum):
                print("It is a Abundant Number")
                else:
                print("Not Abundant Number")

                OutPut:
                Enter N Value12
                It is a Abundant Number

                Enter N Value4
                Not Abundant Number

                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