# Chapter 17-Streams Computation with Console¶

## Example-hello.c, Page no-688¶

In [1]:
print 'Hello World'
Hello World

## Example-hello.cpp, Page no-688¶

In [1]:
print 'Hello World'
Hello World

## Example-redirect.cpp, Page no-688¶

In [2]:
print 'Hello World with cout'
print 'Hello World with cerr'
print 'Hello World with clog'
Hello World with cout
Hello World with cerr
Hello World with clog

In [1]:
c=raw_input()
print c
Hello World
Hello World

## Example-put.cpp, Page no-692¶

In [1]:
for i in range(255):
if i==26:
continue
print i,chr(i)
0
1 
2 
3 
4 
5 
6 
7 
8 
9
10

11
12
13
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
27 
28
29
30
31
32
33 !
34 "
35 #
36 $37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127  128 � 129 � 130 � 131 � 132 � 133 � 134 � 135 � 136 � 137 � 138 � 139 � 140 � 141 � 142 � 143 � 144 � 145 � 146 � 147 � 148 � 149 � 150 � 151 � 152 � 153 � 154 � 155 � 156 � 157 � 158 � 159 � 160 � 161 � 162 � 163 � 164 � 165 � 166 � 167 � 168 � 169 � 170 � 171 � 172 � 173 � 174 � 175 � 176 � 177 � 178 � 179 � 180 � 181 � 182 � 183 � 184 � 185 � 186 � 187 � 188 � 189 � 190 � 191 � 192 � 193 � 194 � 195 � 196 � 197 � 198 � 199 � 200 � 201 � 202 � 203 � 204 � 205 � 206 � 207 � 208 � 209 � 210 � 211 � 212 � 213 � 214 � 215 � 216 � 217 � 218 � 219 � 220 � 221 � 222 � 223 � 224 � 225 � 226 � 227 � 228 � 229 � 230 � 231 � 232 � 233 � 234 � 235 � 236 � 237 � 238 � 239 � 240 � 241 � 242 � 243 � 244 � 245 � 246 � 247 � 248 � 249 � 250 � 251 � 252 � 253 � 254 � ## Example-space1.cpp, Page no-693¶ In [2]: import sys test=raw_input("Enter string: ") i=0 print "Output string: ", while True: if test[i].isspace(): break else: sys.stdout.write(test[i]) i+=1 Enter string: Hello World Output string: Hello ## Example-space2.cpp, Page no-693¶ In [1]: test=raw_input("Enter string: ") print "Output string:", test Enter string: Hello World Output string: Hello World ## Example-stand.cpp, Page no-694¶ In [1]: string1="Object-Computing" string2=" with C++" len1=len(string1) len2=len(string2) for i in range(1,len1): print string1[:i] for i in range(len1, 0, -1): print string1[:i] print "%s%s" %(string1[:len1], string2[:len2]) print string1+string2 print string1[:6] O Ob Obj Obje Objec Object Object- Object-C Object-Co Object-Com Object-Comp Object-Compu Object-Comput Object-Computi Object-Computin Object-Computing Object-Computin Object-Computi Object-Comput Object-Compu Object-Comp Object-Com Object-Co Object-C Object- Object Objec Obje Obj Ob O Object-Computing with C++ Object-Computing with C++ Object ## Example-student.cpp, Page no-697¶ In [1]: MAX_MARKS=600.0 def read(self): self.__name=raw_input("Enter Name: ") self.__marks=int(raw_input("Enter Marks Secured: ")) def show(self): print '{:>10}'.format(self.__name), print '{:>6}'.format(self.__marks), print '{0:10.0f}'.format((self.__marks/MAX_MARKS)*100) class student: __name=str __marks=int read=read show=show count=int(raw_input("How many students ? ")) s=[]*count for i in range(count): s.append(student()) for i in range(count): print "Enter Student", i+1, "details..." s[i].read() print "Student Report..." print '{:>3}'.format("R#"), print '{:>10}'.format("Student"), print '{:>6}'.format("Marks"), print '{:>15}'.format("Percentage") for i in range(count): print "%3s" %(i+1), s[i].show() How many students ? 3 Enter Student 1 details... Enter Name: Tejaswi Enter Marks Secured: 450 Enter Student 2 details... Enter Name: Rajkumar Enter Marks Secured: 525 Enter Student 3 details... Enter Name: Bindu Enter Marks Secured: 429 Student Report... R# Student Marks Percentage 1 Tejaswi 450 75 2 Rajkumar 525 88 3 Bindu 429 72 ## Example-salary.cpp, Page no-701¶ In [1]: desig=["CEO", "Manager", "Receptionist", "Clerk", "Peon"] salary=[10200,5200,2950,950,750] print "Salary Structure Based on Designation" print "-------------------------------------" print '{:>15}'.format('Designation '), print '{:>15}'.format('Salary (in Rs.)') print "-------------------------------------" for i in range(5): print '{:.>15}'.format(desig[i]), print '{:*>15}'.format(salary[i]) Salary Structure Based on Designation ------------------------------------- Designation Salary (in Rs.) ------------------------------------- ............CEO **********10200 ........Manager ***********5200 ...Receptionist ***********2950 ..........Clerk ************950 ...........Peon ************750 ## Example-hex.c, Page no-705¶ In [1]: num=raw_input("Enter any hexadecimal number: ") print "The input number in decimal = %d" %int(num, 16) Enter any hexadecimal number: ab The input number in decimal = 171 ## Example-hex.cpp, Page no-706¶ In [2]: num=raw_input("Enter any hexadecimal number: ") print "The input number in decimal = %d" %int(num, 16) Enter any hexadecimal number: ab The input number in decimal = 171 ## Example-foutput.cpp, Page no-709¶ In [1]: x=int(100) print format(x, '02x'), x f=122.3434 print f print '{:.2f}'.format(f) print "0x%0.4X" %x print '{:.3e}'.format(f) 64 100 122.3434 122.34 0x0064 1.223e+02 ## Example-payroll.cpp, Page no-709¶ In [1]: f1=123.45 f2=34.65 f3=float(56) print '{0:6.2f}'.format(f1) print '{0:6.2f}'.format(f2) print '{0:6.2f}'.format(f3) 123.45 34.65 56.00 ## Example-oct.cpp, Page no-710¶ In [1]: i=raw_input("Enter octal number: ") print "Its decimal equivalent is: %d" %int(i, 8) i=int(raw_input("Enter decimal number: ")) print "Its output:", oct(i) Enter octal number: 111 Its decimal equivalent is: 73 Enter decimal number: 73 Its output: 0111 ## Example-mattab.cpp, Page no-711¶ In [1]: import math num=int(raw_input("Enter Any Integer Number: ")) print "-----------------------------------------------------" print '{:>5}'.format("NUM"), print '{:>10}'.format("SQR"), print '{:>15}'.format("SQRT"), print '{:>15}'.format("LOG") print "-----------------------------------------------------" for i in range(1, num+1): print '{:>5}'.format(i), print '{:>10}'.format(i*i), print '{:15.3f}'.format(math.sqrt(i)), print '{:15.4e}'.format(math.log(i)) Enter Any Integer Number: 10 ----------------------------------------------------- NUM SQR SQRT LOG ----------------------------------------------------- 1 1 1.000 0.0000e+00 2 4 1.414 6.9315e-01 3 9 1.732 1.0986e+00 4 16 2.000 1.3863e+00 5 25 2.236 1.6094e+00 6 36 2.449 1.7918e+00 7 49 2.646 1.9459e+00 8 64 2.828 2.0794e+00 9 81 3.000 2.1972e+00 10 100 3.162 2.3026e+00 ## Example-space3.cpp, Page no-712¶ In [1]: def sp(): print "", x=1 y=2 z=3 w=4 print x, sp(), print y, sp(), print z, sp(), print w 1 2 3 4 ## Example-currency.cpp, Page no-713¶ In [2]: def rupee(): print "Rs.", def dollar(): print "US$",
print "Item Sales in India..."
item1=raw_input("Enter Item Name: ")
cost1=int(raw_input("Cost of Item: "))
print "Item Sales in US..."
item2=raw_input("Enter Item Name: ")
cost2=int(raw_input("Cost of Item: "))
print "Item Cost Statistics..."
print "Item Name:", item1
print "Cost:",
rupee(),
print cost1
print "Item Name:", item2
print "Cost:",
dollar(),
print cost2
Item Sales in India...
Enter Item Name: PARAM Supercomputer
Cost of Item: 55000
Item Sales in US...
Enter Item Name: CRAY Supercomputer
Cost of Item: 40500
Item Cost Statistics...
Item Name: PARAM Supercomputer
Cost: Rs. 55000
Item Name: CRAY Supercomputer
Cost: US\$ 40500

## Example-pmani.cpp, Page no-715¶

In [1]:
def output(self, x):
print '{x:{f}>{w}.{p}f}'.format(x=x,f=self._my_manipulator__fill,w= self._my_manipulator__width, p=self._my_manipulator__precision)
class my_manipulator:
__width=int
__precision=int
__fill=chr
def __init__(self, tw, tp, tf):
self.__width=tw
self.__precision=tp
self.__fill=tf
output=output
def set_float(w, p, f):
return my_manipulator(w, p, f)
f1=123.2734
f2=23.271
f3=16.1673
set_float(10, 3, '*').output(f1)
set_float(9, 2, '^').output(f2)
set_float(8, 3, '#').output(f3)
***123.273
^^^^23.27
##16.167

## Example-point.cpp, Page no-717¶

In [1]:
class POINT:
__x=int
__y=int
def __init__(self):
self.__x=0
self.__y=0
def output(self):
print "(%d,%d)" %(self.__x, self.__y)
def input(self):
self.__x, self.__y=[int(x) for x in raw_input().split()]
p1=POINT()
p2=POINT()
print "Enter two coordinate points (p1, p2):",
p1.input(), p2.input()
print "Coordinate points you entered are:"
p1.output()
p2.output()
Enter two coordinate points (p1, p2):2 3
5 6
Coordinate points you entered are:
(2,3)
(5,6)

## Example-1, Page no-719¶

In [2]:
num=int(num, 16)
print "Octal equivalent:", oct(num)
Octal equivalent: 012

## Example-2, Page no-719¶

In [1]:
PI=3.14159265
print "The values at different levels of precision are:"
for i in range(1, 6):
print '%0.*f' % (i, PI)
The values at different levels of precision are:
3.1
3.14
3.142
3.1416
3.14159