Anda di halaman 1dari 5

Solution of Assignment-2

Q1: Distinguish between getchar( ), getche() and getch( )?


Ans: All the three functions i.e. getchar(), getche() and getch() are single
character input functions. They are used to read a single character fro the
user at a tie. !ut still there is soe difference in their wor"ing:
getchar(): This function recei#es a character at a tie fro standard input.
$onsider the following e%aple:
char ch & getchar()'
The character is echoed on the screen. (hen the user presses the enter "ey,
the character iediately gets assigned to ch #ariable appearing on the left
side of the assignent stateent.
getche(): )i"e getchar() function, getche() also scans a character at a tie
fro standard input. $onsider the following e%aple:
char ch & getche()'
The character is echoed on the screen. !ut here, the user need not to press
the enter "ey, because the character itself is the cobination of character
#alue as well as the enter "ey. *o as the user types the character, it
iediately gets assigned to ch #ariable appearing on the left side of the
assignent stateent.
getch(): )i"e getchar() and getche(), it is also used to ta"e a single character
#alue at a tie. $onsider the following e%aple:
char ch & getch()'
This function wor"s siilar to getche() function. The only difference is that the
character is not echoed on the screen.
Q+: Differentiate between foratted and unforatted ,-. and /-. functions?
Ans: $ language pro#ides a nuber of library functions for handling input-
output operations. *oe of these are called foratted ,-/ functions and soe
are unforatted ,-/ functions.
0oratted ,-. and /-. functions are those functions that are used to recei#e
or display data in a specified- desired forat. The desired forat is achie#ed
by using a nuber of forat specifiers. ,t is possible to display data-results
within the reser#ed space, left alignent, right alignent etc. printf and
scanf() functions are the e%aples of foratted ,-. and /-. functions.
1nforatted ,-/ functions don2t follow any specific forat- pattern. They don2t
use any forat specifiers for displaying the #alues rather they recei#e or
display data where#er the cursor is placed currently. getchar(), putchar()
getche(), getch(), gets() and puts() are the e%aples of unforatted ,-. and
/-. functions
Q3: A fi#e digit nuber is entered through the "eyboard. (rite a progra to
obtain the re#ersed nuber and to deterine whether the original nuber
and re#ersed nuber is e4ual or not.
Ans:
5oid ain()
6
long int a,nu,r,s&7'
printf(8enter the #alue of a(up to fi#e digits)9)'
scanf(8:d9, ;a)'
nu&a'
while(a<&7)
6
r&a:17'
s&17=s>r'
a&a-17'
?
if(nu&&s)
printf(8nuber and its re#erse are e4ual9)'
else
printf(8nuber and its re#erse are not e4ual9)'
?
Q@: Any character is entered through the "eyboard, write a progra to
deterine whether the character entered is a capital case letter, a sall case
letter, a digit or a special sybol.
Ans: .
AincludeBctype.hC
5oid ain()
6
char a'
printf9enter a character:9)'
scanf(8:c9,;a)'
if(isupper(a))
printf(8entered character is in upper caseDn9)'
else
if(islower(a))
printf(8entered character is in lower case:9)'
else
if(isdigit(a))
printf(8entered character is digit9)'
else
printf(8entered character is aspecial sybols9)'
?
QE: (rite a progra to find the #alue of one nuber raised to the power of
another. Two nubers has to be entered through "eyboard
Ans:
5oid ain()
6
int %,n,p&1,i'
printf(8enter any two integer nubers9)'
scanf(8:d :d9,;%,;n)'
for(i&1'iB&n'i>>)
6
p=&%'
?
printf( The result of this operation is :d9,p)'
?
QF' (rite a progra to print the following pattern:

3
3+3
3+1+3
3+3
3
Ans: #oid ain()
6
int n,a,G,",p&7,4,H,,s'
printf(8saple input for n9)'
sacnf(8:d9, ;n)'
for(a&1'aB&3'a>>)
6
for(G&1'GB&nIa'G>>)
printf(8 8)'
4&n'
for("&1'"B&a'">>)
6
H&p>1'
for(&p'Bn'>>)
6
printf(8:d9,H)'
H>>'
?
?
printf(8Dn9)'
?
for(a&1'aB&nI1'a>>)
6
for(G&1'GB&a'G>>)
printf(8 8)'
4&n'
for("&1'"B&nIa'">>)
6
printf(8:d9,4)'
II4'
?
for(s&4>+' sB&n's>>)
printf(8:d9,s)'
printf(8Dn9)'
?
?
note: for theory 4uestions please refer your boo"

Anda mungkin juga menyukai