You are on page 1of 9

E3004A07

Group No 1

Homework Title / No. : Homework 1________________________________Course Code: CAP101___

Course Instructor : Ms. Richa Malhotra ______________ Course Tutor (if applicable) : _____________

Date of Allotment: 23/08/2010 ____________________ Date of submission : 04/09/2010__________

Student’s Roll No.A07____________________ Section No. : E3004_____________________________

Declaration:
I declare that this assignment is my individual work. I have not copied from any other student’s work or
from any other source except where due acknowledgment is made explicitly in the text, nor has any part
been written for me by another person.

Student’s Signature: Amit Kumar___________

Evaluator’s comments:
_____________________________________________________________________

Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:

1|Page
E3004A07
Group No 1

Home Work 1
CAP101
Part A

1. Explain the difference between associativity and precedence with the help
of an example?

Ans. The difference between associativity and precedence are:

Basis Associativity Precedence


Definition Associativity refers to the order in Precedence determines the order in
which the consecutive operations which operations are performed
within the same precedence group during the evaluation of an
are carried out. expression.
Purpose The purpose of associativity is to The purpose of precedence is to
familiar with direction in which familiar that which operator has
multiple operators with same higher priority.
precedence are evaluated.
Types Associativity direction are of two Precedence are of two types:-
types:- 1. Highest precedence
1. Left to Right direction 2. Lowest precedence
2. Right to Left direction
Need Associativity is needed when Precedence is needed when multiple
operators of the same precedence operator of different priority are
are grouped together in the absence present in same expression.
of parenthesis. Precedence reducing the confusion
during the evaluation of process.
Direction On the basis of associativity, the On the basis of precedence:
and priority addition, subtraction, multiplication Division
and division operator has direction Multiplication Highest
Left to Right(L R) Addition Precedence
Subtraction
Example I=3*2/2+8/4 i=2*3+8-7
i=6/2+8/4 <Operator: * > i=6+8-7 <Operator * >
i=3+8/4 <Operator: / > i=14-7 <Operator + >
i=3+2 <Operator: / > i=7 <Operator - >
i=5 <Operator: + > <Operator = >

2|Page
E3004A07
Group No 1

<Operator: = >

2. Differentiate between Implicit and Explicit Type Conversions?

Ans. The difference between Implicit and Explicit Type Conversions:

Basis Implicit Type Conversion Explicit Type Conversion


Definition Implicit type conversion can be Explicit type conversion can be
performed implicitly by the performed by the user explicitly.
compiler.
Automatic It is automatic type conversion. It is not automatic type coversion.
Conversion
Alternative It is also known as Type Promotion. It is also known as Type Casting.
Name
Syntax It has no syntax. Var1=(data type)var2;
Hierarchy long double We use a type cast operator for
double explicit type conversion.
float
unsigned long int
long int
unsigned int
int
short int
Example int a=5,b=7; int a=7,b=5;
long int c; float c;
c=a+b; c=(float)a/b;
printf(“c= %ld”,c); printf(“c= %f”,c);

Output: c=12 Output: c= 1.400000

3|Page
E3004A07
Group No 1

3. a) Differentiate between the Prefix and Postfix increment operator.

Ans.

Basis Prefix increment operator Postfix increment operator


Definition In prefix increment operator first In postfix increment operator first
increment by 1 then incremented the value will be assigned to the
value will be assigned to the variable then the value will be
variable. increment by 1.
Donated By Prefix increment operator is Postfix increment operator is
donated by (++) before the donated (++) after the operand.
operand.
Syntax ++varname; varname++;
Example int x=5,y=3; int x=5,y=3;
a=(++x)+(++y); a=(x++)+(y++);
//a=4+6=10 //a=3+5=8
printf(“y= %d x=%d a=%d”,x,y,a); printf(“y= %d x=%d a=%d”,x,y,a);

output: output:
y=4 x=6 a=10 y=5 x=6 a=8

3. b) Differentiate between the Assignment and Equal To operator.

Ans.

Basis Assignment operator Equal to operator


Definition Assignment operator is those which Equal to operator is use to compare
are used to assigning a value to a the two values.
variable.
Denoted by It is denoted by ‘=’ It is denoted by ‘==’
Operator It is a assignment operator. It is a relational operator.
Associativity Associativity is right-to-left Associativity left-to-right
Use Can be used multiple times in one To use multiple times there is a
statement. need of logical operator.
Example int a=1; int a,b;
if(a==b)
3. c) Differentiate between the Expression and Statement.

4|Page
E3004A07
Group No 1

Ans.

Basis Expression Statement


Definition An expression is a A statement is a collection of data type, functions,
combination of expression, operator and may or may not end with a
constants, variable, semicolon.
operators and has an
assignment operator
and also end with a
semicolon.
Need It is only used when It is necessary in every programme.
there is a need of
calculations.
Evaluation/ Expression is A statement can be executed.
Executed evaluated to produce
a result.
Types Types of expression Types of statement like:
like: a) Expression Statement
a) Arithmetic b) Compound Statement
Expression c) Control Statement
b) Relational
Expression
c) Logical
Expression
d) Assignment
Expression
e) Conditional
Expression
Example x=a+b+c; If(a>0) //statement
//expression Statement1;

3. d) Differentiate between the Identifiers and Keywords.

5|Page
E3004A07
Group No 1

Ans.

Basis Identifiers Keywords


Definition Identifiers is the name that is given Keywords are the reserved words
to various programming elements that have standard predefined
such as functions, variable array meaning and these meaning cannot
etc. be changed.
Userdefined/ Identifiers are the user defined Keywords are the predefined
Predefined names. names.
Consist It consists of letter, alphabet and Keywords consists only
digit. letter/Alphabet.
Character Identifiers first character must be All characters of keyword are
Start with letter or underscore. letter.
Uppercase/ We can write the identifier either in Keywords are always written in
lowercase upper case or lower case. lower case.
Use It can be used as keywords using It cannot be used as identifier.
'typedef'.
Example int a; int a;
'a' is the identifier. 'int' is the keyword

3. e) Differentiate between the Signed and Unsigned integers.

Ans.

Basis Signed integer Unsigned integer


Definition Integer may be positive or In unsigned integer always contains
negative. If no sign proceeds then the positive value.
integer is assumed to be positive.
Declared By Declared using 'int' keyword. Declared using 'unsigned int'.
Range Range for integer is -32768 to Range for unsigned integer is 0 to
+32767 65535
Negative Signed integer can be negative. Unsigned integer cannot be
negative.
Format Format string for signed integer is Format string for unsigned integer
string ‘%d’. is ‘%u’.
Syntax int varname; unsigned int varname;
Example int a,b; // a, b is type of integer unsigned int a,b; // a, b is type of

6|Page
E3004A07
Group No 1

unsigned integer

PART B

4. Write a program that will examine each character in a character type array
and write the ASCII equivalent of each character. Size of the array is known
in advance by the integer variable n.

Ans.

#include<conio.h>
#include<stdio.h>
void main()
{
char a[10];
int i; clrscr();
printf("Enter the string: ");
gets(a);
for(i=0;a[i]!='\0';i++)
{
printf("%c= %d",a[i],a[i]);
printf("\n");
}
getch();
}

5. Write a program to display the day of week according to the day no. entered
by the user.

7|Page
E3004A07
Group No 1

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\nEnter the day number of the week:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nDay is Monday");
break;
case 2:
printf("\nDay is Tuesday");
break;
case 3:
printf("\nDay is Wednesday");
break;
case 4:
printf("\nDay is Thursday");
break;
case 5:
printf("\nDay is Friday");
break;
case 6:
printf("\nDay is Saturday");
break;
case 7:
printf("\nDay is Sunday");
break;
default:
printf("\nInvalid Choice");
}
getch();
}

8|Page
E3004A07
Group No 1

6. Write a program that receives a multi-word string and prints it in the form
of one word per line.

Ans.

#include<conio.h>
#include<stdio.h>
void main()
{
char a[30];
int i;
clrscr();
printf("Enter the string: ");
gets(a );
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ')
printf("\n");
else
printf("%c",a[i]);
}
getch();
}

************

9|Page

You might also like