C Chapter 02
C Chapter 02
Balagurusamy C PROGRAMMING
EXERCISE SOLUTION :CHAPTER-2
35 Votes
REVIEW QUESTIONS
STATE WHETHER THE FOLLOWING STATEMENTS ARE TRUE OR FALSE:
(a) Any valid printable ANSII character can be used in an identifier. ( False )
(b) All variables must be given a type when they are declared. ( True )
(c) Declarations can appear anywhere in a program. ( False )
(d) ANSI C treats the variable name and Name to be same. ( False )
(e) The underscore can be used anywhere in an identifier. ( True )
(f) The keyword void is a data type in C. ( True )
(g) Floating point data constants, by default, denote float type values. ( False )
(h) Like variables, constants have a type. ( True )
(i) Character constants are coded using double quotes. (False )
(j) Initialization is the process of assigning a value to a variable at the time of declaration. (
true )
(k) All static variables are automatically initialized to zero. ( True )
(l) The scanf function can be used to read only one value at a time. ( False )
Fill in the blanks with appropriate words:
(a)The keyword ……………..can be used to create a data type identifier.
Answer: int
(b) …………… is the largest value that an unsigned short int type variable can store.
Answer: 255
(c) A global variable is also known as …………….variable.
Answer: external
(d) A variable can be made constant by declaring it with the qualifier ……………. At the time of
initialization.
Answer: constant
Question: State the difference between the declaration of a variable and the definition of a
symbolic name?
Answer:
Variables has need to declare at the beginning of the body but after the main. The syntax for
declaring a variable is as follow:
Data-type v1,v2,……..vn;
v1, v2,……..vn are the names of variables. For example, valid declarations are
int count;
int number,total;
float ratio;
Symbolic names has need to define at the beginning of a program. A symbolic name constants is
defined as follows:
#define symbolic-name value of constant
Valid example of constant definations are:
#define STRENGTH 100
#define PASS MARK 5
Question: What are the qualifiers that an int can have at a time?
Answer:
A signed int can take a value between -32768 to 32767 and an unsigned int can take a value 0 to
65535.
Question: Describe the purpose of the qualifiers constant and volatile.
Answer:
We may like the value of certain variables to remain constant during the excution of
a program. We can achieve this by declaring the variable with the qualifier constant at the time
of initialization. Example:
const int class_size=40;
ANSI standard defines another qualifier volatile that could be used to tell explicitly the
complier that a variables value may be changed at any time by some external sources ( from
outside the program). For example:
volatile int date;
Question: When dealing with very small or very large numbers, what steps would you like
you take to improve the accurancy of the calculation?
Answer:
When we are dealing with a very short number we can improve the accurancy of calculation by
using a keyword short before the keyword. Example short int.
When we are dealing with a very large number we can improve the accurancy of calculation by
using a keyword long before the keyword. Example long int.
Question: Which of the following are invalid constants and why?
0.0001
Answer: (valid)
5×1.5
Answer: (Invalid)
Reason: Exponent must be an integer.
99999
Answer: Valid
Reason: Long integer.
+100
Answer: ( valid)
75.45E-2
Answer: ( Valid )
-45.6
Answer: ( Valid )
“15.75”
Answer: ( Invalid )
Reason: “” sign is not permitted.
-1.79e+4
Answer: (valid)
0.00001234
Answer: ( Valid )
Question: Which of the following are invalid variable and why?
Minimum
Answer: ( valid )
First.name
Answer: ( Invalid )
Reason:. Sign is not permitted.
N1+n2
Answer: ( Invalid )
Reason: + sign is not permitted.
&name
Answer: ( Invalid )
Reason: & is not permitted.
Doubles
Answer: ( Valid )
Reason: Keyword may be a part of variable name.
3rd_row
Answer: ( Invalid )
Reason: First character must be a letter or underscore.
n$
Answer: ( Invalid )
Reason: Dollar sign is not permitted.
Row1 ( Valid )
Float
Answer: ( Invalid )
Reason: float is a keyword.
Sum Total
Answer: ( Invalid )
Reason: White space is not permitted.
Row Total
Answer: ( Invalid )
Reason: White space is not permitted.
Column total
Answer: ( Invalid )
Reason: White space is not permitted.
Question: Find errors, if any, in the following declaration statements.
{
Intx;
float letter,DIGIT;
double=p,q;
exponent alpha,beta;
m,n.z:INTEGER
short char c;
long int m;count;
long float temp;
getch();
}
Error1: intx should have a type.
Error2: Line number 6, expression syntax.
Error3: Line number 7, Declaration should be properly.
Error4:Line number 9, Unreachable code.
Problem no 2.1: Write a program to determine and print the sum of the following
harmonic series for a given value of n:
1+1/2+1/3+………………+1/n
Solve:
#include<stdio.h>
#include<conio.h>
Void main()
{
int n;
float I, sum, t;
clrscr();
printf(“1+1/2+1/3+……………+1/n\n”);
printf(“Enter the value of n\n”);
scanf(“%d”,&n);
sum=0;
for(i=1;i<=n;i++)
{
t=1/i;
sum=sum+t;
}
printf(“%f”,sum);
getch();
}
Output:
1+1/2+1/3+………….+1/n
Enter the value of n
4
2.083333