{"id":16892,"date":"2021-08-12T09:25:20","date_gmt":"2021-08-12T03:55:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=16892"},"modified":"2021-11-22T18:38:31","modified_gmt":"2021-11-22T13:08:31","slug":"python-program-to-find-the-missing-term-of-any-arithmetic-progression","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-missing-term-of-any-arithmetic-progression\/","title":{"rendered":"Python Program to Find the Missing Term of any Arithmetic Progression"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Strong Numbers in a List<\/a> An Arithmetic progression is a mathematical sequence of numbers in which the difference between the consecutive terms is constant.<\/p>\n In general, an arithmetic sequence looks like this:\u00a0 a, a+d, a+2d, a+3d,…………….<\/p>\n where a = first term<\/p>\n d= common difference<\/p>\n Formula<\/strong> : d= second term – first term<\/p>\n Given an Arithmetic series, and the task is to find the missing term in the given series.<\/p>\n Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the missing term in the given Arithmetic progression series.<\/p>\n Approach:\u00a0<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:\u00a0<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program to Find Strong Numbers in a List Arithmetic progression: An Arithmetic progression is a mathematical sequence of numbers in which the difference between the consecutive terms is constant. In general, an arithmetic sequence looks like this:\u00a0 a, a+d, a+2d, a+3d,……………. where a = first term d= …<\/p>\n
\nArithmetic progression:<\/strong><\/p>\nGiven Arithmetic Progression series = [10, 14, 18, 22, 30]<\/pre>\n
The missing term in the given Arithmetic Progression series is:\r\n26<\/pre>\n
Given Arithmetic Progression series = [100, 300, 400, 500, 600, 700]<\/pre>\n
The missing term in the given Arithmetic Progression series is:\r\n200<\/pre>\n
Program to Find the missing term of any Arithmetic progression<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the list ( Arithmetic Progression series) as static input and store it in a variable.\r\ngvn_lst = [100, 200, 400, 500, 600, 700]\r\n# Calculate the length of the given series using the built-in len() function\r\n# and store it in another variable.\r\nlst_len = len(gvn_lst)\r\n# Calculate the common difference by finding the difference between the list's last and\r\n# first terms and divide by N and store it in another variable.\r\ncommn_diff = int((gvn_lst[lst_len-1]-gvn_lst[0])\/lst_len)\r\n# Take a variable, initialize it with the first term of the given list and store\r\n# it in another variable say \"first term\".\r\nfst_term = gvn_lst[0]\r\n# Loop from 1 to the length of the given list using for loop.\r\nprint(\"The missing term in the given Arithmetic Progression series is:\")\r\nfor itr in range(1, lst_len):\r\n # Inside the loop, check if the difference between the list(iterator) and \"first term\"\r\n # is not equal to the common difference using if conditional statement.\r\n if gvn_lst[itr]-fst_term != commn_diff:\r\n # If the statement is true, print the sum of \"first term\" and common difference\r\n print(fst_term+commn_diff)\r\n break\r\n else:\r\n # Else update the value of variable \"first term\"\u00a0 by iterator value.\r\n fst_term = gvn_lst[itr]\r\n<\/pre>\n
The missing term in the given Arithmetic Progression series is:\r\n26<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
#Give the list ( Arithmetic Progression series) as user input using list(),map(),input(),and split() \r\n#functions and store it in a variable.\r\ngvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given series using the built-in len() function\r\n# and store it in another variable.\r\nlst_len = len(gvn_lst)\r\n# Calculate the common difference by finding the difference between the list's last and\r\n# first terms and divide by N and store it in another variable.\r\ncommn_diff = int((gvn_lst[lst_len-1]-gvn_lst[0])\/lst_len)\r\n# Take a variable, initialize it with the first term of the given list and store\r\n# it in another variable say \"first term\".\r\nfst_term = gvn_lst[0]\r\n# Loop from 1 to the length of the given list using for loop.\r\nprint(\"The missing term in the given Arithmetic Progression series is:\")\r\nfor itr in range(1, lst_len):\r\n # Inside the loop, check if the difference between the list(iterator) and \"first term\"\r\n # is not equal to the common difference using if conditional statement.\r\n if gvn_lst[itr]-fst_term != commn_diff:\r\n # If the statement is true, print the sum of \"first term\" and common difference\r\n print(fst_term+commn_diff)\r\n break\r\n else:\r\n # Else update the value of variable \"first term\"\u00a0 by iterator value.\r\n fst_term = gvn_lst[itr]\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 100 300 400 500 600 700\r\nThe missing term in the given Arithmetic Progression series is:\r\n200<\/pre>\n
\n