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

                      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