0% found this document useful (0 votes)
1K views29 pages

Assignment On C Programming

This document contains an assignment on C programming submitted by Easteak Ahamed, a student in the Department of Petroleum and Mining Engineering. The assignment contains questions testing knowledge of expressions, operators, data types, and writing C code to evaluate mathematical equations. The student is asked to determine if statements are true/false, fill in blanks, write code snippets, and provide the values of arithmetic expressions.

Uploaded by

Easteak Ahamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views29 pages

Assignment On C Programming

This document contains an assignment on C programming submitted by Easteak Ahamed, a student in the Department of Petroleum and Mining Engineering. The assignment contains questions testing knowledge of expressions, operators, data types, and writing C code to evaluate mathematical equations. The student is asked to determine if statements are true/false, fill in blanks, write code snippets, and provide the values of arithmetic expressions.

Uploaded by

Easteak Ahamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

0

ASSIGNMENT ON C
PROGRAMMING

Easteak Ahamed
Roll: 180826
Department of Petroleum and Mining Engineering
1 year, 2nd semester
st

Submission Date: November 24,2020


4.1 state whether the following statement are true or false

(a) The expression !(x<=y) is same as the expression x>y.


: True
(b) A unary expression consists of only one operand with no operator.
: True

(c) All arithmetic operators have the same level of precedence.


: False

(d) An expression statement is terminated with a period.


: False

(e) The operators <=, >= and != all enjoy the same level of priority.
: False

(f) The modulus operator % can be used only with integers.


: True

(g) In C, if a data item is zero, it is considered false.


: True

(h) During the evaluation of mixed expressions, an implicit cast is generated


automatically.
: True
(I)An explicit cast can be used to change the expression.
: True

(j) Associativity is used to decide which of several different expressions is


evaluated first.
: False
(k) Parentheses can be used to change the order of evaluation expressions.
: True
(l) During modulo division, the sign of the result is positive, if both the operands
are of the same sign.
: True

4.2 Fill in the blanks with appropriate preposition.

(a) The expression containing all the integer operands is called arithmetic
(b) C supports as many as six relational operators.
(c) The sizeof operator returns the number of bytes the operand occupies.
(d) Precedence is used to determine the order in which different operators in an
expression are evaluated.
(e) An expression that combines two or more relational expressions is termed as
logical expression.
(f) The use of implicit type on a variable can change its type in the memory.
(g) The order of evaluation can be changed by using parenthesis in an expression.
(h) The operator % cannot be used with real operands.
(i) Functional call and comma operator operators have the highest and lowest
levels of precedence respectively.
(j) <= is a rational operator while && is a logical operator.
(k) A division operation involving integer operands truncates the resultant value.
This situation can be avoided by using _________.
(l) The result of a relational expression is either 0 or

4.3 Give the statement


int a=10, b=20, c;
(a) The statement a=+10, is valid= False
CODE OUTPUT
#include <stdio.h>
int main()
{
int a=+10;
printf("%d",a);
return 0;
}
(b) The expression a+4/6*6/2 evaluates to 11= False
CODE OUTPUT

#include<stdio.h>
int main()
{
int a=10,s;
s=a+4/6*6/2;
printf ("%d",s);
return 0;
}

(c) The expression b+3/2*2/3 evaluates to 20; True


CODE OUTPUT

#include<stdio.h>

int main()
{
int b=20,s;
s=b+3/2*2/3;
printf ("%d",s);
return 0;
}
(d) The statement a+=b; gives the values 30 to a and 20 to b; True
CODE
OUTPUT
#include<stdio.h>
int main()
{
int a=10, b=20;
a+=b;
printf ("%d\n",a);
printf("%d",b);
return 0;
}

(e) The statement ++a; gives the value 12 to a; False


CODE OUTPUT

#include<stdio.h>
int main()
{
int a=10;
int b,c;
b=++a;
c=b++;
printf("%d",c);
return 0;
}
(f) The statement a=1/b; assigns the value 0.5 to a; True
CODE

OUTPUT
#include<stdio.h>
int main()
{
float a=10, b=20;
a=1/b;
printf ("%f\n",a);
return 0;
}

4.4 Declared a as int and b as float, state whether the following statement are
true or false.

(a) The statement a=1/3+1/3+1/3; assigns the value 1 to a. False


CODE OUTPUT
#include<stdio.h>
int main()
{
float a;
a=1/3+1/3+1/3;
printf ("%d\n",a);
return 0;
}
(b) The statement b=1.0/3.0+1.0/3.0+1.0/3.0; assigns the value 1.0 to b. True
CODE
OUTPUT
#include<stdio.h>
int main()
{
float b;
b=1.0/3.0+1.0/3.0+1.0/3.0;
printf ("%f\n",b);
return 0;
}

(c) The statement b=1.0/3.0*3.0; Gives a value 1.0 to b. True

CODE
OUTPUT

#include<stdio.h>
int main()
{
float b;
b=1.0/3.0*3.0;
printf ("%f\n",b);
return 0;
}
(d) The statement b=1.0/3.0+2.0/3.0; assigns a value 1.0 to b. True
CODE OUTPUT

#include<stdio.h>
int main()
{
float b;
b=1.0/3.0+2.0/3.0;
printf ("%f\n",b);
return 0;
}

(e) The statement a=15/10.0+3/2; assigns a value 3 to b. False


CODE
OUTPUT
#include<stdio.h>
int main()
{
int a;
a=15/10.0+3/2;
printf ("%d\n",a);
return 0;
}
4.5 Which of the following expressions are true

(a) !(5+5>=10); False CODE

OUTPUT
#include<stdio.h>
int main()
{
int a;
a=!(5+5>=10);
printf ("%d",a);
return 0;
}

(b) 5+5==10||1+3==5; True

CODE OUTPUT

#include<stdio.h
>
int main()
{
int a;
a=5+5==10||
1+3==5;
printf ("%d",a);
return 0;
}
(c) 5>10||10<20&&3<5; True,
CODE OUTPUT
#include<stdio.h>
int main()
{
int a;
a=5>10||10<20&&3<5;
printf ("%d",a);
return 0;
}

(d) 10!=15&&!(10<20)||15>30; False

CODE
#include<stdio.h> OUTPUT

int main()
{
int a;
a=10!=15&&!3<5;

printf ("%d",a);
return 0;
}
4.6 Which of the following arithmetic expressions are valid? If valid,
give the value of the expression, otherwise give the rea
a) the arithmetic expression is valid and the value of the expression is 0

CODE
OUTPUT
#include<stdio.h>
int main()
{
int z;
z=25/3%2;
printf("%d\n" ,z);
return 0;
}

b) The arithmetic expression is valid and the value of the expression is 7


CODE
OUTPUT
include<stdio.h>
int main()
{
int z;
z=+9/4+5;
printf("%d\n" ,z);
return 0;
}
(c) The arithmetic expression is invalid. Reason : We can not do modulo of a float
by integer.
CODE

#include<stdio.h>
int main()
{
float z;
z=7.5%3;
printf("%f\n" ,z);
return 0;
}

(d) The arithmetic expression is valid and the value of the expression is 3

CODE OUTPUT

#include<stdio.h>
int main()
{
float z;
z=14%3+7%2;
printf("%f\n" ,z);
return 0;
}
(e) The arithmetic expression is valid and the value of the expression is -2.

CODE OUTPUT

#include<stdio.h>
int main()
{
int z;
z=-14%3;
printf("%d\n" ,z);
return 0;
}

(f) The arithmetic expression is valid and the value of the expression is 0.

CODE OUTPUT

#include<stdio.h>
int main()
{
float z;
z=15.25+-5.0;
printf("%f\n" ,z);
return 0;
}
(g) The arithmetic expression is valid and the value of the expression is 5.

CODE

#include<stdio.h> OUTPUT
OUTPUT
int main()
{
float z;
z=(5/3)*3+5%3;
printf("%f\n" ,z);
return 0;
}

(h) The arithmetic expression is valid and the value of the expression is 1.
CODE OUTPUT
#include<stdio.h>
int main()
{
float z;
z=21%(int)4.5;
printf("%f\n" ,z);
return 0;
}
4.7 Write C assignment statements to evalute the following
equation:

a) Area=3.1416*r^2+2+3.1416*r*h

CODE OUTPUT

#include <stdio.h>

int main()
{
float pi=3.1416, Area, r, h;
printf("Please enter radius and
height\n");
scanf("%f %f",&r,&h);
Area=pi*pow(r,2)+2*pi*r*h;
printf("The area is %f",Area);
return 0;
}

b)Torque =(2*m1*m2*g)/(m1+m2)
CODE OUTPUT

#include<stdio.h>
int main()
{ int m1,m2; float g,torque;
printf(“m1=”);
scanf(“%d ”,&m1);
printf(“m2=”);
scanf(“%d”,&m2);
printf(“g=”);
scanf(“%f”,&g);
torque=(2*m1*m2*g)/(m1+m2);
printf(“The Torque=%f”,torque);
return 0;
}

c)side=√ (a^2+b^2-2abcos(x))
CODE
OUTPUT

#include <stdio.h>
int main()
{
float Side,a,b,x,y,pi=3.1416;
printf("Please enter two sides of triangle\n");
scanf("%f %f",&a,&b);
printf("Please enter angle of those two sides\
n");
scanf("%f",&x);
y=x*(pi/180);
Side=sqrt(pow(a,2)+pow(b,2)-2*a*b*cos(y));
printf("side is %f",Side);
return 0;
}

d) Energy=mass[acceleration*height+{(velocity)^2/2}
CODE OUTPUT

#include <stdio.h>
int main()
{
float
Energy,mass,acceleration,height,velocity;
printf("Please enter
Energy,mass,acceleration,height,velocity\
n");
scanf("%f %f %f %f
%f",&Energy,&mass,&acceleration,&height
,&velocity);

Energy=mass*(acceleration*height+pow(vel
ocity,2)/2);
printf("Energy is %f",Energy);
return 0;
}
4.8 identify unnecessary parentheses in the following arithmetic
expressions:

(a) ((x-(y/5)+z)%8)+25 . Here all parentheses are unnecessary.

(b) ((x-y)*p)+q . Here ((x-y)*p) is unnecessary. It can be written as (x-y)*p+q


(c) (m*n)+(-x/y) . Here all parentheses are necessary.
(d) x/(3*y). Here all parentheses are unnecessary. It can be written as x/3*y

4.9 determine the value of each of following logical expressions if


a=5,b=10 and c= -6

a) the value of the logical expression is 0

CODE
OUTPUT
#include <stdio.h>

int main()
{ int a=5, b=10,
c=-6, z;
z=a>b&&a<c;
printf("%d",z);
return 0;
}
b) the value of the logical expression is 1
CODE OUTPUT
#include <stdio.h>

int main()
{ int a=5, b=10,
c=-6, z;
z=a<b&&a>c;
printf("%d",z);
return 0;
}

c) the value of the logical expression is 1


CODE OUTPUT

#include <stdio.h>

int main()
{ int a=5, b=10, c=-6,
z;
z=a==c||a>0;
printf("%d",z);
return 0;
}
d) the value of the logical expression is 1

CODE
OUTPUT
include <stdio.h>

int main()
{ int a=5, b=10, c=-
6, z;
z=b>15&&c<0||
a>0;
printf("%d",z);
return 0;
}

e) ) the value of the logical expression is 0


OUTPUT
CODE

#include <stdio.h>

int main()
{ int a=5, b=10, c=-6, z;
z=(a/2.0==0.0&&b/2.0!
=0.0)||c<0.0;
printf("%d",z);
return 0;
}
4.10 What is the out of following program:

CODE
OUTPUT

#include <stdio.h>
int main()
{
char x;
int y;
x=100;
y=125;
printf("%c\n",x);
printf("%c\n",y);
printf("%d\n",x);
return 0;

}
4.11 Find the output of the following program:

CODE
OUTPUT

#include <stdio.h>

int main()
{
int x=100;
printf("%d\n",10 + x++);
printf("%d\n",10 + ++x);
return 0;
}

4.12 What is printed by the following program:


CODE

#include <stdio.h> OUTPUT

int main()
{
int x=5, y=10, z=10 ;
x=y==z;
printf("%d",x);
return 0;
}
4.13 What is the output of the following program:

OUTPUT
CODE

#include <stdio.h>

int main()
{
int x=100, y=200;
printf("%d", (x>y)? x:y);

return 0;
}
4.14 What is the output pf the following program:
Explain: Unsigned x=1 is invalid because there is no declaration of int type. It should Be
unsigned char x=1, that’s why it does not go to if rather than it goes toElse,

CODE OUTPUT

#include <stdio.h>

int main()
{
unsigned x=1 ;
signed char y=-1;
if (x>y)
printf("x>y");
else
printf("x<=y");
return 0;
4.15 what is the output of the following program:
Explain: The condition of if statement does not have a logical
operator , means x=10 is not a logical operator that’s why it prints
anything in if

CODE OUTPUT

#include <stdio.h>

int main()
{
int x=10;
if (x=20)
printf("TRUE");
else
printf("False");
return 0;
}
4.16 What is printed by the program:
CODE OUTPUT

#include <stdio.h>

int main()
{
int m;
for(m = 0; m<3; ++m);
printf("%d\n", (m%2) ? m: m+2);

return 0;
}
4.17 What is printed by the program:

CODE OUTPUT

#include <stdio.h>

int main()
{
int m=-14, n=3;
printf("%d\n",
m/n*10);
n=-n,
printf("%d\n",
m/n*10);
return 0;
}
DEBUGGING EXERCISES
4.1 What is the error in the statement:

We can not do modulo of a float by integer. Error is x=y%x;

4.2 What is the error in the statement:


(a) two condition should be added by &&. Error is if(m==1 & n!=0)
(b) conditional operator is the error, it should be >=. Error is if (x=<5)

4.3 Find error and rectify them:

(a) x=y=z=0.5, 2.0. -5.75; here 2.0. -5.75 is unnecessary part. After rectified
x=y=z=0.5;
(b) m=++a*5; no error.
(c) y=sqrt(100); no error.
(d) p*=x/y; here p*=x/y; is the error. After rectified p=x/y;
(e) s=/5; here s=/5; is the error. After rectified s=5;
(f) a=b++-c*2; here a=b++-c*2; is the error. After rectified a=b-c*2;;

You might also like