{"id":11881,"date":"2021-09-30T14:30:37","date_gmt":"2021-09-30T09:00:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=11881"},"modified":"2021-11-22T18:34:31","modified_gmt":"2021-11-22T13:04:31","slug":"python-program-to-print-floyds-triangle","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-floyds-triangle\/","title":{"rendered":"Python Program to Print Floyd\u2019s Triangle"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Given the number of rows of the triangle, the task is to print Floyd’s triangle in C, C++, and Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print Floyd’s triangle in C, C++, and Python.<\/p>\n Approach:<\/strong><\/p>\n 1) Python Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2) C++ Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n 1) Python Implementation<\/strong><\/p>\n Give the number of rows of the triangle as user input using the int(input()) function and store it in a variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2) C++ Implementation<\/strong><\/p>\n Give the number of rows of the triangle as user input using the cin function and store it in a variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Give the number of rows of the triangle as user input using the scanf function and store it in a variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. Given the number of rows of the triangle, the task is to print Floyd’s triangle in C, C++, and Python. Examples: Example1: Input: Given number of rows of the …<\/p>\nGiven number of rows of the triangle = 10<\/pre>\n
1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55<\/pre>\n
Given number of rows of the triangle = 13<\/pre>\n
1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55 \r\n56 57 58 59 60 61 62 63 64 65 66 \r\n67 68 69 70 71 72 73 74 75 76 77 78 \r\n79 80 81 82 83 84 85 86 87 88 89 90 91<\/pre>\n
Program to Print Floyd\u2019s Triangle in C, C++, and Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number of rows of the triangle as static input and store it in a variable.\r\ntriRows = 10\r\n# Take a variable and initialize it with 1 say sampNum.\r\nsampNum = 1\r\n# Loop from 1 to the number of rows of the triangle using For loop.\r\nfor m in range(1, triRows+1):\r\n # Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).\r\n for n in range(1, m+1):\r\n # Inside the inner for loop print the sampNum with a space character.\r\n print(sampNum, end=' ')\r\n # Increase the value of sampNum by 1.\r\n sampNum = sampNum+1\r\n # Print the Newline Character after the end of the inner for loop.\r\n print()\r\n<\/pre>\n
1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows of the triangle as static\r\n \/\/ input and store it in a variable.\r\n int triRows = 10;\r\n \/\/ Take a variable and initialize it with 1 say sampNum.\r\n int sampNum = 1;\r\n \/\/ Loop from 1 to the number of rows of the triangle\r\n \/\/ using For loop.\r\n for (int m = 1; m <= triRows; m++) {\r\n for (int n = 1; n <= m; n++) {\r\n \/\/ Inside the inner for loop print the\r\n \/\/ sampNum\u00a0with a space character.\r\n cout << sampNum << \" \";\r\n \/\/ Increase the value of sampNum by 1.\r\n sampNum = sampNum + 1;\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows of the triangle as static\r\n \/\/ input and store it in a variable.\r\n int triRows = 10;\r\n \/\/ Take a variable and initialize it with 1 say sampNum.\r\n int sampNum = 1;\r\n \/\/ Loop from 1 to the number of rows of the triangle\r\n \/\/ using For loop.\r\n for (int m = 1; m <= triRows; m++) {\r\n for (int n = 1; n <= m; n++) {\r\n \/\/ Inside the inner for loop print the\r\n \/\/ sampNum\u00a0with a space character.\r\n printf(\"%d \", sampNum);\r\n \/\/ Increase the value of sampNum by 1.\r\n sampNum = sampNum + 1;\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the number of rows of the triangle as user input\r\n# using the int(input()) function and store it in a variable.\r\ntriRows = int(input('Enter some random number of rows of the triangle = '))\r\n# Take a variable and initialize it with 1 say sampNum.\r\nsampNum = 1\r\n# Loop from 1 to the number of rows of the triangle using For loop.\r\nfor m in range(1, triRows+1):\r\n # Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).\r\n for n in range(1, m+1):\r\n # Inside the inner for loop print the sampNum\u00a0with a space character.\r\n print(sampNum, end=' ')\r\n # Increase the value of sampNum by 1.\r\n sampNum = sampNum+1\r\n # Print the Newline Character after the end of the inner for loop.\r\n print()\r\n<\/pre>\n
Enter some random number of rows of the triangle = 11\r\n1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55 \r\n56 57 58 59 60 61 62 63 64 65 66 \r\n\r\n<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows of the triangle as user input\r\n \/\/ using the cin function and store it in a variable.\r\n int triRows;\r\n cout<<\"Enter some random number of rows of the triangle = \";\r\n cin >> triRows;\r\n\r\n \/\/ Take a variable and initialize it with 1 say sampNum.\r\n int sampNum = 1;\r\n \/\/ Loop from 1 to the number of rows of the triangle\r\n \/\/ using For loop.\r\n for (int m = 1; m <= triRows; m++) {\r\n for (int n = 1; n <= m; n++) {\r\n \/\/ Inside the inner for loop print the\r\n \/\/ sampNum\u00a0with a space character.\r\n cout << sampNum << \" \";\r\n \/\/ Increase the value of sampNum by 1.\r\n sampNum = sampNum + 1;\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
Enter some random number of rows of the triangle = 13\r\n1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10 \r\n11 12 13 14 15 \r\n16 17 18 19 20 21 \r\n22 23 24 25 26 27 28 \r\n29 30 31 32 33 34 35 36 \r\n37 38 39 40 41 42 43 44 45 \r\n46 47 48 49 50 51 52 53 54 55 \r\n56 57 58 59 60 61 62 63 64 65 66 \r\n67 68 69 70 71 72 73 74 75 76 77 78 \r\n79 80 81 82 83 84 85 86 87 88 89 90 91\r\n\r\n<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows of the triangle as user input\r\n \/\/ using the scanf function and store it in a variable.\r\n int triRows;\r\n scanf(\"%d\", &triRows);\r\n \/\/ Take a variable and initialize it with 1 say sampNum.\r\n int sampNum = 1;\r\n \/\/ Loop from 1 to the number of rows of the triangle\r\n \/\/ using For loop.\r\n for (int m = 1; m <= triRows; m++) {\r\n for (int n = 1; n <= m; n++) {\r\n \/\/ Inside the inner for loop print the\r\n \/\/ sampNum\u00a0with a space character.\r\n printf(\"%d \", sampNum);\r\n \/\/ Increase the value of sampNum by 1.\r\n sampNum = sampNum + 1;\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
number of rows =4\r\n1 \r\n2 3 \r\n4 5 6 \r\n7 8 9 10<\/pre>\n
\n