SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.loops
Exercise:
Programming Excercises
Question:5 | ISBN:978013274719 | Edition: 6

Question

(Conversion from kilograms to pounds and pounds to kilograms) Write a program  that displays the following two tables side by side (note that 1 kilogram is 2.2 pounds and that 1 pound is .45 kilograms):
Kilograms       Pounds      |        Pounds           Kilograms
1                       2.2           |           20                   9.09
3                      6.6            |            25                 11.36
...
 197                  433.4        |         510                 231.82     
199                   437.8         |          515                235.09

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Conversion from kilograms to pounds and pounds to kilograms Program Code:

 

# Display the table header
print("Kilograms    Pounds      |      Pounds      Kilograms")

# Iterate from 1 to 199 with a step of 2
for kilograms in range(1, 200, 2):
    # Convert kilograms to pounds using the conversion factor 2.2
    pounds = kilograms * 2.2
    
    # Convert pounds to kilograms using the conversion factor 0.45
    reversedkilograms = pounds * 0.45
    
    # Display the values in the table format
    print("{:<12} {:<12.1f} | {:<16} {:.2f}".format(kilograms, pounds, pounds, reversedkilograms))

 

Executed Output:

Kilograms    Pounds      |      Pounds      Kilograms
1            2.2          | 2.2              0.99
3            6.6          | 6.6000000000000005 2.97
5            11.0         | 11.0             4.95
7            15.4         | 15.400000000000002 6.93
9            19.8         | 19.8             8.91
11           24.2         | 24.200000000000003 10.89
13           28.6         | 28.6             12.87
15           33.0         | 33.0             14.85
17           37.4         | 37.400000000000006 16.83
19           41.8         | 41.800000000000004 18.81
21           46.2         | 46.2             20.79
23           50.6         | 50.6             22.77
25           55.0         | 55.00000000000001 24.75
27           59.4         | 59.400000000000006 26.73
29           63.8         | 63.800000000000004 28.71
31           68.2         | 68.2             30.69
33           72.6         | 72.60000000000001 32.67
35           77.0         | 77.0             34.65
37           81.4         | 81.4             36.63
39           85.8         | 85.80000000000001 38.61
41           90.2         | 90.2             40.59
43           94.6         | 94.60000000000001 42.57
45           99.0         | 99.00000000000001 44.55
47           103.4        | 103.4            46.53
49           107.8        | 107.80000000000001 48.51
51           112.2        | 112.2            50.49
53           116.6        | 116.60000000000001 52.47
55           121.0        | 121.00000000000001 54.45
57           125.4        | 125.4            56.43
59           129.8        | 129.8            58.41
61           134.2        | 134.20000000000002 60.39
63           138.6        | 138.60000000000002 62.37
65           143.0        | 143.0            64.35
67           147.4        | 147.4            66.33
69           151.8        | 151.8            68.31
71           156.2        | 156.20000000000002 70.29
73           160.6        | 160.60000000000002 72.27
75           165.0        | 165.0            74.25
77           169.4        | 169.4            76.23
79           173.8        | 173.8            78.21
81           178.2        | 178.20000000000002 80.19
83           182.6        | 182.60000000000002 82.17
85           187.0        | 187.00000000000003 84.15
87           191.4        | 191.4            86.13
89           195.8        | 195.8            88.11
91           200.2        | 200.20000000000002 90.09
93           204.6        | 204.60000000000002 92.07
95           209.0        | 209.00000000000003 94.05
97           213.4        | 213.4            96.03
99           217.8        | 217.8            98.01
101          222.2        | 222.20000000000002 99.99
103          226.6        | 226.60000000000002 101.97
105          231.0        | 231.00000000000003 103.95
107          235.4        | 235.4            105.93
109          239.8        | 239.8            107.91
111          244.2        | 244.20000000000002 109.89
113          248.6        | 248.60000000000002 111.87
115          253.0        | 253.00000000000003 113.85
117          257.4        | 257.40000000000003 115.83
119          261.8        | 261.8            117.81
121          266.2        | 266.20000000000005 119.79
123          270.6        | 270.6            121.77
125          275.0        | 275.0            123.75
127          279.4        | 279.40000000000003 125.73
129          283.8        | 283.8            127.71
131          288.2        | 288.20000000000005 129.69
133          292.6        | 292.6            131.67
135          297.0        | 297.0            133.65
137          301.4        | 301.40000000000003 135.63
139          305.8        | 305.8            137.61
141          310.2        | 310.20000000000005 139.59
143          314.6        | 314.6            141.57
145          319.0        | 319.0            143.55
147          323.4        | 323.40000000000003 145.53
149          327.8        | 327.8            147.51
151          332.2        | 332.20000000000005 149.49
153          336.6        | 336.6            151.47
155          341.0        | 341.0            153.45
157          345.4        | 345.40000000000003 155.43
159          349.8        | 349.8            157.41
161          354.2        | 354.20000000000005 159.39
163          358.6        | 358.6            161.37
165          363.0        | 363.00000000000006 163.35
167          367.4        | 367.40000000000003 165.33
169          371.8        | 371.8            167.31
171          376.2        | 376.20000000000005 169.29
173          380.6        | 380.6            171.27
175          385.0        | 385.00000000000006 173.25
177          389.4        | 389.40000000000003 175.23
179          393.8        | 393.8            177.21
181          398.2        | 398.20000000000005 179.19
183          402.6        | 402.6            181.17
185          407.0        | 407.00000000000006 183.15
187          411.4        | 411.40000000000003 185.13
189          415.8        | 415.8            187.11
191          420.2        | 420.20000000000005 189.09
193          424.6        | 424.6            191.07
195          429.0        | 429.00000000000006 193.05
197          433.4        | 433.40000000000003 195.03
199          437.8        | 437.8            197.01

 

0 0

Discussions

Post the discussion to improve the above solution.