Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Saumya Gupta IT-2 09213203120 Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

SAUMYA GUPTA

IT-2
09213203120

ASSIGNMENT

Q1: Determine the hierarchy of operations and evaluate the


following expression:
i= 2*3/4+4/4+8-2+5/8 1st Priority */%
2nd Priority +-
3rd Priority =

i= 6/4+4 (operation:*)
=1+4/4 (Operation:/)
= 1+1+8-2+5/8
=1+1+8-2+0
=2+8-2+0
=10-2+0
=8+0
=8
Q2: Which of the following are invalid and valid variable names and
why?
BASICSALARY (v) _basic (v) basic-hra (iv)
#MEAN (iv) group. (iv) 422 (iv)
population in 2019 (iv) over time (iv) mindovermatter (v)
FLOAT (v) hELLO (v) team’svictory (iv)
Plot#3 (iv) 2021_DDay (iv)
Q3: Point out errors, if any, in the following C statemnets:
a) int=314.562*150; (e)
b) name=’Ajay’; (e)
c) 3.14*r*r*h=vol_of_cyl; (e)
d) k=(a*b) (c+(2.5a+b) (d+e); (e)
e) m_inst=rate of interest*amount in rs; (e)
f) si=principal*rate*time/100; (ne)
g) area=3.14*r**2; (e)
h) a=b=3=4; ( e)
i) count=count+1; ( ne)
j) date=’2Mar21’; (e)
Q4: Evaluate the following expressions and show its hierarchy
g=big/2+big*4/big-big+abc/3;
(abc=2.5, big=2, assume g to be a float)
g=2/2+2*4/2-2+2.5/3 (operation:/)
g= 1+2*4 (operation:*)
g=1+8/2-2 /
g=1+4-2+2.5/3 /
g=1+4-2+0.83 +
g=5-2+0.83 -
g=3+0.83 +
g=3.83
Q5: Z=8.8(a+b)2/c-0.5+2a/(q+r)
(a+b)*(1/m)
Convert the above equation into corresponding C statement

Z= (8.8*(a+b)*2/c)-(0.5+2*a/(q+r))/((a+b)*(1/m))
Q6: What would be the output of the following programs:
a) main()
{ int i=2,j=3,k,l;
float a,b;
k=i/j*j; 0*3=0
l=j/i*i; 1*2=2
a=i/j*j; 0*3=0.00
b=j/i*i; 1*2=2.0000
printf(“%d%d%f%f”,k,l,a,b); 0 2 0.00 2.00
}
b) main()
{ int a,b;
a=-3- -3; -3+3=0
b=-3- -(-3); -3-3=-6
printf(“a=%d b=%d”,a,b); 0 -6
}
c) main()
{ float a=5, b=2;
int c;
c=a%b;
printf(“%d”,c); Error
}
d) main()
{ printf(“nn \n\n”);
Printf(“nn /n/n nn/n”);
} nn

nn /n/n nn/n

Q7: Two numbers are input through the keyboard into two locations
C and D. WAP to interchange the contents of C and D
Program: main()
{ int c,d,e;
printf(“\n Enter the number at location C:”);
scanf(“%d”,&c);
printf(“\n Enter the number at location D:”);
scanf(“%d”,&d);
/* Interchange the contents of two variables using a third
variable as temporary store*/
e=c;
c=d;
d=e;
printf(“\n New number at location C= %d”,c);
printf(“\n New number at location D= %d”,d);
}
Q8: Point out errors if any:
a) main()
{ int a=5,b,c;
b=a=15;
c=a<15;
printf(“\na=%d b=%d c=%d”,a,b,c);
}
b) main()
{ int x=15;
Printf(“\n%d%d%d”,x!=15, x=20, x<30);
}
c) main()
{ int a,b;
scanf(“%d%d”,a,b);
if(a>b)
printf(“This is a game”);
else
printf(“You have to play it”);
}
Q9: If cost price and selling prince of an item is input through the
keyboard, WAP to determine whether the seller has made profit or
incurred loss.
Also determine how much profit he made or loss he incurred.
Program:
main()
{ float cp,sp,p,l;
printf(“Enter cost price and selling price”);
scanf(“%f%f”,&cp,&sp);

p=sp-cp; //Profit=Selling Price-Cost Price


l=cp-sp;
if(p>0)
printf(“The seller has made a profit of Rs.%f”,p);

if(l>0)
printf(“The seller is in loss by Rs.%f”,l);

if(p==0)
printf(“There is no loss, no profit”);
}
Q10: WAP to check whether a triangle is valid or not, when the three
angles are entered through the keyboard?
PROGRAM:
main()
{ float angle1, angle2, angle3;
printf(“Enter three angles of triagle”);
scanf(“%f%f%f”, &angle1,&angle2,&angle3);
if((angle1+angle2+angle3)==180)
printf(“The triangle is a valid triangle”);
else
printf(“The triangle is a invalid triangle”);
}
Q11: Given a point(x,y), WAP to find out if it lies on the x-axis, y-axis
or on the origin?
PROGRAM:
main()
{ int x,y;
printf(“\n Enter the x and y coordinates of a point:\n”);
scanf(“%d%d”,&x,&y);
if(x==0 && y==0)
printf(“\n Point lies on origin”);
else
if(x==0 && y!=0)
printf(“\nPoint lies on Y axis”);
else
if(x!=0 && y==0)
printf(“\n Point lies on X axis”);
else
printf(“Point doesn’t lie on any axis, nor origion ”);
}
Q12: If a=10, b=12, c=0, find the values of Logical operation
expressions in the following:
a) a!=6&&b>5 :1
b) a==9||b<3 :0
c) !(a<10) :1
d) !(a>5&&c) :1
e) 5&&c!=8||!c :1
Q13: What would be the output of the following:
main()
{ int i=4, j=-1, k=0, w, x, y, z;
w= i||j||k;
x= i&&j&&k;
y= i||j&&k;
z=i&&j||k;
printf(“\nw=%d x=%d y=%d z=%d”,w,x,y,z);
}
OUTPUT:
w=1 x=0 y=1 z=1
Q14: Point out errors if any:
a) main()
{ int code,flag;
if(code==1 & flag==0) //&&
printf(“\n The eagle is landed”);
}
b) main()
{ char spy=’a’, password=’z’;
If (spy==’a’ or password==’z’) // ‘I’ of if should be in small
letter
printf(“\nAll the birds are safe in the nest”);
}
c) main()
{ int i=10,j=20;
If(i=5)&&if(j=10) // Syntax error
printf(“\nHave a nice day”);
}
d) main()
{ int x=2;
If(x==2 && x!=0);
{ printf(“\nHi”);
Printf(“\nHello”); //Syntax error ‘p’ of print should be in
small letter
}
else
printf(“Bye”);
}
Q15: A library charges a fine for every book returned late. For first 5
days the fine 50 paise, for 6-10 days fine is one rupee and above 10
days fine is 5 rupees. If you return the book after 30 days your
membership will be cancelled. WAP to accept the number of days
the member is late to return the book and display the fine or the
appropriate message?
PROGRAM:
main()
{ int days;
printf(“Enter the number of days by which member is late”);
scanf(“%d”,&days);
if(days>=1 && days<=5)
printf(“\nFine=Rs. %d”, days*50/100);
else
{ if(days>=6 && days<=10)
printf(“\nFine=Rs. %d”, days*1);
else
{ if(days>=10 && days<=30)
printf(“\n Fine= Rs. %d”, days*5);
else
printf(\n The membership is cancelled);
}}}
Q16: What would be the output of the following:
a) main()
{ int i=-4, j, num;
j= (num<0? 0: num*num)
printf(“\n%d”,j);
}
Output : 0

b) main()
{ int k, num=30;
k= (num>5?(num<=10? 100:200) :500);
printf(“\n%d”,num);
}

Output: 30

Q17: Point out errors if any:


a) main()
{ int tag=0, code=1;
If(tag==0)
(code> 1 ? printf (“\nHello”) ? printf(“\nHi”));
else
printf(“\nHello Hi!!”);
}
Error :
main()
{ int tag=0, code=1;
If(tag==0)
(code> 1 ? printf (“\nHello”) ? printf(“\nHi”)); //? Operator
always comes with :
else
printf(“\nHello Hi!!”);
}
b) main()
{ int x=10, y=20;
X==20 && y!=10 ? printf(“True”) : printf(“False”);
}
Error:
main()
{ int x=10, y=20;
X==20 && y!=10 ? printf(“True”) : printf(“False”); //x should
be in small letter
}

Q18: Rewrite the following programs using conditional operators:


a) main()
{ int x, min, max;
scanf(“%d%d”, &max, &x);
if(x>max)
max=x;
else
min=x;
}

SOLUTION:
int main()
{
int x, min, max ;

scanf ( "\n%d %d", &max, &x ) ;


x = (x > max ? max : min);

return 0;
}

b) main()
{ int code;
scanf(“%d”, &code);
if(code>1)
printf(“\nSalem”);
else
if(code<1)
printf(“\nEddie”);
else
printf(“\n Brain”);
}

SOLUTION:
main()
{ int code;
scanf(“%d”, &code);
(code>1)?printf(“\nSaelm”):(code<1?printf(“\nEddie”):printf(“\n Brain));
}

Q19: What would be the output of the following:


a) main()
{ int i=1;
while(i<=10);
{ printf(“\n%d”,i);
i++;
}}

OUTPUT:
12345678910

b) main()
{ int x=4,y,z;
y= -- x;
z= x --;
printf(“\n %d%d%d”,x,y,z);
}

OUTPUT:
433

c) main()
{ int x=4, y=3, z;
z= x-- -y;
printf(“\n %d%d%d”,x,y,z);
}

OUTPUT:
331

d) main()
{ char suit=3;
switch(suite)
{ case 1:
printf(“\nDiamond”);
case 2:
printf(“\nSpade”);
default:
printf(“\nHeart”);
}
printf(“\nThese are my favourite suites”);
}

OUTPUT:
These are my favorite suites

e) main()
{ int i=3;
switch(i)
{ case 0:
printf(“\nCustomers are dicey”);
case 1+0:
printf(“\nMarkets are pricey”);
case 4/2:
printf(“\nInvestors are moody”);
case 8%5:
printf(“\nAt least employees are good”);
}}

OUTPUT:
Atleast all emolyees are good

Q20: Point out errors if any:


a) main()
{ int suite=1;
switch(suite);
{ case 0;
printf(“\nClub”);
case 1;
printf(\nDiamond);
}}

SOLUTION: main()
{ int suite=1;
switch(suite);\\Here semicolon is not used
{ case 0;
printf(“\nClub”); \\ Break statement
to be used
case 1;
printf(\nDiamond);\\Break statement to
be used
}}

b) main()
{ int temp;
scanf(“%d”,&temp);
switch(temp)
{
case(temp<=20):
printf(“\nCool”);
case(temp>20 && temp<=30):
printf(“\nRain rain here again!”);
case(temp>30 && temp<=40):
printf(“\nWish I am on Everest”);
default:
printf(“\nGood old Nagpur weather”);
}}
ERRORS
main()
{ int temp;\\Input should be taken
scanf(“%d”,&temp);
switch(temp)
{
case(temp<=20):
printf(“\nCool”); // break statement missing
case(temp>20 && temp<=30):
printf(“\nRain rain here again!”); // break statement
missing
case(temp>30 && temp<=40):
printf(“\nWish I am on Everest”); // break statement
missing
default:
printf(“\nGood old Nagpur weather”); // break
statement missing
}}

c) main()
{ float a=3.5;
switch(a)
{
case 0.5:
printf(“\nThe art of C”);
break;
case 1.5:
printf(“\nThe spirit of C”);
break;
case 2.5:
printf(“\nSee through C”);
break;
case 3.5:
printf(“\nSimply C”);
}}

ERRORS
main()
{ float a=3.5;
switch(a)
{
case 0.5: // float is not used only char and integer are use
printf(“\nThe art of C”);
break;
case 1.5: // Float is not used only char and integer are use
printf(“\nThe spirit of C”);
break;
case 2.5: // Float is not used only char and integer are use
printf(“\nSee through C”);
break;
case 3.5: // Float is not used only char and integer are use
printf(“\nSimply C”);// break statement missing
}}

You might also like