Chapter 4: Input & Output in C

Example 4.1, Page number: 47

In [1]:
#show the effect of mismatch of data types

#Reads Input value for a

a = raw_input("Enter value of 'A' : ")
a = int(a)

#In python, raw_input() is used to read values

#Result
print "A = ",a
Enter value of 'A' : 8
A =  8

Example 4.2, Page number: 47

In [1]:
#show the effect of mismatch of data types

import sys

#Variable initialization
#Reads Input value for a

a = raw_input("Enter value of 'A' : ")
a = int(a)

#In python, raw_input() is used to read values

#there is no char datatype of 1 byte size in python.
#python has string variable only.

#Result

sys.stdout.write("A = %d"%(a%256))
Enter value of 'A' : 256
A = 0

Example 4.3, Page number: 49

In [7]:
#show the effect of various escape sequences

import sys

#Variable initialization
a = 1
b = a + 1
c = b + 1
d = c + 1

#Result
sys.stdout.write ('\tA = %d\nB = %d \'C = %d\''%(a,b,c))
sys.stdout.write ('\n**D = %d**'%(d))
sys.stdout.write ('\nA = %d B = %d'%(a,b))
sys.stdout.write ('\r******')
	A = 1
B = 2 'C = 3'
**D = 4**
A = 1 B = 2******

Example 4.4, Page number: 50

In [2]:
#print the third power of 2 using pow() function.
#assume the floating point numbers

#Variable initialization
x = 2.0
y = 3.0

#Result
print '%lf raised to %lf is %lf\n' %(x,y,pow(x,y))
2.000000 raised to 3.000000 is 8.000000

Example 4.5, Page number: 50

In [9]:
#print the third power of 10 using pow() function.
#assume the floating point numbers

#Variable initialization
p = 3

#Result
#There is no pow10() function in Python.
print 'Ten raised to %lf is %lf\n' %(p,pow(10,p))
Ten raised to 3.000000 is 1000.000000

Example 4.6, Page number: 51

In [3]:
#detect an error while inputting a data.  Use return value of scanf() statement.

#Variable initialization
#in python,the map(int,a.split()) function splits the input characters
#and converts it into integers and returns a value if all the values are integers
#otherwise an exception will be generated and initializes v as 0.
#using exception handling, this result can be achieved.


#Exception handling
try:
    a = raw_input("Enter value of 'A','B' & 'C' : ")
    v = map(int,a.split())
except:
    v = 0

#In python, raw_input() is used to read values

#Result
print '\nError In Inputting.' if v < 3 else '\nValues Successfully read.'

    
Enter value of 'A','B' & 'C' : 1 2 3

Values Successfully read.

Example 4.7, Page number: 51

In [4]:
#Find length of the string using print() function

import sys

#Variable initialization
#in python, print()or write() function would not return the number of characters
#written to the output buffer. so len() function is used to find the length.

nm = raw_input("Enter String : ")

#In python, raw_input() is used to read values

l = len(nm)

#Result
print '\nLength = ',l
Enter String : HELLO

Length =  5

Example 4.8, Page number: 52

In [5]:
#Perform addition of two numbers

#Variable initialization
a = raw_input("ENTER TWO VALUES ")
b = raw_input("ENTER TWO VALUES ")

#In python, raw_input() is used to read values

c = int(a) + int(b)

#Result
print '\nSUM IS = ',c
ENTER TWO VALUES 5
ENTER TWO VALUES 8

SUM IS =  13

Example 4.9, Page number: 52

In [6]:
#Find the square of the given number

#Variable initialization
a = raw_input("ENTER ANY NUMBER ")

#In python, raw_input() is used to read values

c = int(a) * int(a)

#Result
print '\nSQUARE OF GIVEN NUMBER = ',c
ENTER ANY NUMBER 5

SQUARE OF GIVEN NUMBER =  25

Example 4.10, Page number: 53

In [7]:
#Calculate average of three real numbers.

#Variable initialization

a = raw_input("ENTER THREE FLOAT NUMBERS : ")
b = raw_input("ENTER THREE FLOAT NUMBERS : ")
c = raw_input("ENTER THREE FLOAT NUMBERS : ")

#In python, raw_input() is used to read values

d = float(a) + float(b) + float(c)

#Result
print '\nAverage of Given Numbers : ',d/3
ENTER THREE FLOAT NUMBERS : 2.5
ENTER THREE FLOAT NUMBERS : 3.5
ENTER THREE FLOAT NUMBERS : 4.5

Average of Given Numbers :  3.5

Example 4.11, Page number: 53

In [3]:
#Print message with inputted name

#Variable initialization
yourname = raw_input("Enter Your Name : ")

#In python, raw_input() is used to read values

#Result
print "\nWel Come %s to 'C' Programming Course"%(yourname)
Enter Your Name : Ajay

Wel Come Ajay to 'C' Programming Course

Example 4.12, Page number: 54

In [9]:
#Input a single character and display it

#Variable initialization
ch = raw_input("Enter any character : ")

#In python, raw_input() is used to read values

#Result
print "\nYour Entered Character is : %c"%(ch)
Enter any character : C

Your Entered Character is : C

Example 4.13, Page number: 54

In [21]:
#Swap the values of two variables without using third variable.

#Variable initialization
a = 7
b = 4

print "\n A = %d B = %d"%(a,b)

#Swapping
a = a + b
b = a - b
a = a - b

#Result
print "Now A = %d B = %d"%(a,b)
 A = 7 B = 4
Now A = 4 B = 7

Example 4.14, Page number: 55

In [3]:
#Accept characters through keyboard using getchar() function

import sys

#Variable initialization
#in python, string is an object. It has no nul-termination character.
#raw_input() reads the string constant from the stdin and the write() function
#prints it to the stdout based on the attribute length.

ch = raw_input(" ")

#In python, raw_input() is used to read values

#Result
sys.stdout.write(ch)
 COMPILER
COMPILER

Example 4.15, Page number: 56

In [4]:
#Print the characters using putchar() function

import sys

#Variable initialization
#in python, string is an object. It has no nul-termination character.
#raw_input() reads the string constant from the stdin.

ch = raw_input("Enter Text Here : ")

#In python, raw_input() is used to read values

#Result
#using for loop, the string can be displayed character by character.

sys.stdout.write("The Entered Text : ")
for c in ch:
    sys.stdout.write(c)
Enter Text Here : Characters
The Entered Text : Characters

Example 4.16, Page number: 56

In [5]:
#Show the effect of getche() and getch() functions

import sys

#Variable initialization
#in python, there is no equivalent function for getche() and getch() functions.
#raw_input() function reads the characters and terminates by pressing the
#enter key

ch = raw_input("Enter any two alphabetics ")

#In python, raw_input() is used to read values
#Result
sys.stdout.write(ch)
Enter any two alphabetics A 
A 

Example 4.17, Page number: 57

In [6]:
#Read and display the character using getch() and putch() functions

import sys

#Variable initialization
#in python, there is no equivalent function for getch() function.
#raw_input() function reads the characters and terminates by pressing the
#enter key

ch = raw_input("Press any key to continue ")

#In python, raw_input() is used to read values

#Result
sys.stdout.write("You Pressed : %s"%(ch))
Press any key to continue 9
You Pressed : 9

Example 4.18, Page number: 57

In [7]:
#Accept string through the keyboard using gets() function

import sys

#Variable initialization
#in python, raw_input() function reads the string and terminates by pressing the
#enter key and write() function displays the string.

ch = raw_input("Enter the String : ")

#In python, raw_input() is used to read values

#Result
sys.stdout.write("Entered String : %s"%(ch))
Enter the String : USE OF GETS()
Entered String : USE OF GETS()

Example 4.19, Page number: 58

In [8]:
#Print the accepted character using puts() function

import sys

#Variable initialization
#in python, raw_input() function reads the string and terminates by pressing the
#enter key and print() function displays the string.

ch = raw_input("Enter the String : ")

#In python, raw_input() is used to read values

#Result
print"Entered String : "
print ch
Enter the String : puts is in use.
Entered String : 
puts is in use.

Example 4.20, Page number: 58

In [9]:
#read string using cgets() and display it using cputs()

import sys

#Variable initialization
#in python, raw_input() function reads the string and terminates by pressing the
#enter key and print() function displays the string.
#in python, string is an object.

t = raw_input("Enter Text Here : ")

#In python, raw_input() is used to read values

#Result
print "Your Entered Text : %s"%(t)
Enter Text Here : How are you?
Your Entered Text : How are you?
In [ ]: