You are on page 1of 15

Computer Programming Assignment-1

(Submission Date: 10/02/2021)


Note: Students need to take printout of this sheet and write answers of the questions along
with correct explanation in the space provided after questions.

Q1. Which of the following are invalid variable names and why?

BASICSALARY _basic basic-hra


#MEAN group. 422
population in 2006 over time mindovermatter
FLOAT hELLO queue.
team’svictory Plot # 3 2015_DDay

Q2. Point out the errors, if any, in the following C statements, if there is an error write
reason of it:

(a) int = 314.562 * 150 ;


(b) name = ‘Ajay’ ;
(c) varchar = ‘3’ ;
(d) 3.14 * r * r * h = vol_of_cyl ;
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
(f) m_inst = rate of interest * amount in rs ;
(g) si = principal * rateofinterest * numberofyears / 100 ;
(h) area = 3.14 * r ** 2 ;
(i) volume = 3.14 * r ^ 2 * h ;
(j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ;
(k) a = b = 3 = 4 ;
(l) count = count + 1 ;
(m) date = '2 Mar 04' ;

Q3. Evaluate the following expressions and show their hierarchy.


(a) g = big / 2 + big * 4 / big - big + abc / 3 ;
(abc = 2.5, big = 2, assume g to be a float)
(b) on = ink * act / 2 + 3 / 2 * act + 2 + tig ;
(ink = 4, act = 1, tig = 3.2, assume on to be an int)

(c) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ;


(qui = 4, add = 2, god = 2, assume s to be an int)

(d) s=1/3*a/4-6/2+2/3*6/g;
(a = 4, g = 3, assume s to be an int)

Q4. Convert the following equations into corresponding C statements.


Q5. What would be the output of the following programs:

(a) (b)
void main() main( )
{ {
int i = 2, j = 3, k, l ; int a, b ;
float a, b ; a = -3 - - 3 ;
k=i/j*j; b = -3 - - ( - 3 ) ;
l=j/i*i; printf ( "a = %d b = %d", a, b ) ;
a=i/j*j; }
b=j/i*i;
printf( "%d %d %f %f", k, l, a, b ) ;
}

(c) (d)
int main() main( )
{ {
int a = 80, b = 85, c = 70, d = 75, e = 60; printf ( "nn \n\n nn\n" ) ;
printf("Average of 5 subjects is: printf ( "nn /n/n nn/n" ) ;
%0.2f",(a+b+c+d+e)/5.0);
}
return 0;
}

(e) (f)
main( )
int main() {
{ int p, q ;
int a = 5, b = 8; printf ( "Enter values of p and q" ) ;
printf("Numbers before swapping a = %d, b = %d",
scanf ( " %d %d ", p, q ) ;
a,b);
a = a+b; printf ( "p = %d q =%d", p, q ) ;
b = a-b; }
a = a-b;
printf("\nNumbers after swapping a = %d, b = %d",
a,b);
return 0;
}
Q6. What would be the output of the following programs?

(a) main( ) (b) main( )


{ {
int a = 300, b, c ; int a = 500, b, c ;
if ( a >= 400 ) if ( a >= 400 )
b = 300 ; b = 300 ;
c = 200 ; c = 200 ;
printf ( "\n%d %d", b, c ) ; printf ( "\n%d %d", b, c ) ;
} }

(c) main( ) (d) main( )


{ {
int x = 10, y = 20 ; int x = 3, y = 5 ;
if ( x == y ) ; if ( x == 3 )
printf ( "\n%d %d", x, y ) ; printf ( "\n%d", x ) ;
} else ;
printf ( "\n%d", y ) ;
}

(e) main( ) (f) main( )


{ {
int x = 3 ; int x = 3, y, z ;
float y = 3.0 ; y = x = 10 ;
if ( x == y ) z = x < 10 ;
printf ( "\nx and y are equal" ) ; printf ( "\nx = %d y = %d z = %d", x, y, z ) ;
else }
printf ( "\nx and y are not equal" ) ;
}

(g) (h)
main( ) main( )
{ {
int k = 35 ; int i = 65 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ; char j = ‘A’ ;
} if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}

(i) (j)
main( ) main( )
{ {
int a = 5, b, c ; int x = 15 ;
b = a = 15 ; printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ;
c = a < 15 ; }
printf ( "\na = %d b = %d c = %d", a, b, c ) ;
}

Q7. Point out the errors, if any, in the following programs:

(a) main( ) (b) main( )


{ {
float a = 12.25, b = 12.52 ; int j = 10, k = 12 ;
if ( a = b ) if ( k >= j )
printf ( "\na and b are equal" ) ; {
} {
k=j;
j=k;
}
}
}

(c) main( ) (d) main( )


{ {
if ( 'X' < 'x' ) int x = 10 ;
printf ( "\nascii value of X is smaller than that of x" if ( x >= 2 ) then
); printf ( "\n%d", x ) ;
} }
(e) main( ) (f) main( )
{ {
int x = 10 ; int x = 10, y = 15 ;
if x >= 2 if ( x % 2 = y % 3 )
printf ( "\n%d", x ) ; printf ( "\nCarpathians" ) ;
} }

(g) main( ) (h) main( )


{ {
int x = 30 , y = 40 ; int x = 10 ;
if ( x == y ) if ( x >= 2 ) then
printf( "x is equal to y" ) ; printf ( "\n%d", x ) ;
elseif ( x > y ) }
printf( "x is greater than y" ) ;
elseif ( x < y )
printf( "x is less than y" ) ;
}

Q8. What would be the output of the following programs:

(a) main( ) (b) main( )


{ {
int i = 4, z = 12 ; int i = 4, z = 12 ;
if ( i = 5 || z > 50 ) if ( i = 5 && z > 5 )
printf ( "\nDean of students affairs" ) ; printf ( "\nLet us C" ) ;
else else
printf ( "\nDosa" ) ; printf ( "\nWish C was free !" ) ;
} }

(c) main( ) (d) main( )


{ {
int i = 4, j = -1, k = 0, w, x, y, z ; int i = 4, j = -1, k = 0, y, z ;
w = i || j || k ; y = i + 5 && j + 1 || k + 2 ;
x = i && j && k ; z = i + 5 || j + 1 && k + 2 ;
y = i || j && k ; printf ( "\ny = %d z = %d", y, z ) ;
z = i && j || k ; }
printf ( "\nw = %d x = %d y = %d z = %d", w, x,
y, z ) ;
}

(e) main( ) (f) main( )


{ {
int i = -3, j = 3 ; int a = 40 ;
if ( !i + !j * 1 ) if ( a > 40 && a < 45 )
printf ( "\nMassaro" ) ; printf ( "a is greater than 40 and less than 45" ) ;
else else
printf ( "\nBennarivo" ) ; printf ( "%d", a ) ;
} }

(g) main( ) (h) main( )


{ {
int p = 8, q = 20 ; int i = -1, j = 1, k ,l ;
if ( p == 5 && q > 5 ) k = i && j ;
printf ( "\nWhy not C" ) ; l = i || j ;
else printf ( "%d %d", I, j ) ;
printf ( "\nDefinitely C !" ) ; }
}

(i) main( ) (j) main( )


{ {
int x = 20 , y = 40 , z = 45 ; int i = -1, j = 1, k ,l ;
if ( x > y && x > z ) k = !i && j ;
printf( "x is big" ) ; l = !i || j ;
else if ( y > x && y > z ) printf ( "%d %d", i, j ) ;
printf( "y is big" ) ; }
else if ( z > x && z > y )
printf( "z is big" ) ;
}
Q9. Point out the errors, if any, in the following programs and give reason of error:

(a) (b) main( )


main( ) {
{ int code, flag ;
int i = 2, j = 5 ; if ( code == 1 & flag == 0 )
if ( i == 2 && j == 5 ) printf ( "\nThe eagle has landed" ) ;
printf ( "\nSatisfied at last" ) ; }
}

(c) main( ) (d) main()


{ {
char spy = 'a', password = 'z' ; int i = 10, j = 20 ;
if ( spy == 'a' or password == 'z' ) if ( i = 5 ) && if ( j = 10 )
printf ( "\nAll the birds are safe in the nest" ) ; printf ( "\nHave a nice day" ) ;
} }

(e) main( ) (f) main( )


{ {
int x = 10 , y = 20; int a, b ;
if ( x >= 2 and y <=50 ) if ( a == 1 & b == 0 )
printf ( "\n%d", x ) ; printf ( "\nGod is Great" ) ;
} }

(g) main( ) (h) main( )


{ {
int x = 2; int i = 10, j = 10 ;
if ( x == 2 && x != 0 ) ; if ( i && j == 10)
{ printf ( "\nHave a nice day" ) ;
printf ( "\nHi" ) ; }
printf( "\nHello" ) ;
}
else
printf( "Bye" ) ;
}
Q10. What would be the output of the following programs:

(a) main( ) (b) main( )


{ {
int i = -4, j, num ; int k, num = 30 ;
j = ( num < 0 ? 0 : num * num ) ; k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;
printf ( "\n%d", j ) ; printf ( "\n%d", num ) ;
} }

(c) main( )
{
int j = 4 ;
( !j != 1 ? printf ( "\nWelcome") : printf ( "\nGood Bye") ) ;
}

Q11. Point out the errors, if any, in the following programs:

(a) main( ) (b) main( )


{ {
int tag = 0, code = 1 ; int ji = 65 ;
if ( tag == 0 ) printf ( "\nji >= 65 ? %d : %c", ji ) ;
( code > 1 ? printf ( "\nHello" ) ? printf ( }
"\nHi" ) ) ;
else
printf ( "\nHello Hi !!" ) ;
}

(c) main( ) (d) main( )


{ {
int i = 10, j ; int a = 5 , b = 6 ;
i >= 5 ? ( j = 10 ) : ( j = 15 ) ; ( a == b ? printf( "%d",a) ) ;
printf ( "\n%d %d", i, j ) ; }
}
(e) main( ) (f) main( )
{ {
int n = 9 ; int kk = 65 ,ll ;
( n == 9 ? printf( "You are correct" ) ; : printf( ll = ( kk == 65 : printf ( "\n kk is equal to 65" ) : printf (
"You are wrong" ) ;) ; "\n kk is not
} equal to 65" ) ) ;
printf( "%d", ll ) ;
}

(g) main( )
{
int x = 10, y = 20 ;
x == 20 && y != 10 ? printf( "True" ) : printf(
"False" ) ;
}

Q12. What would be the output of the following programs

(a) main( ) (b) main( )


{ {
int j ; int i = 1 ;
while ( j <= 10 ) while ( i <= 10 ) ;
{ {
printf ( "\n%d", j ) ; printf ( "\n%d", i ) ;
j=j+1; i++ ;
} }
} }

(c) main( ) (d) main( )


{ {
int j ; int x = 1 ;
while ( j <= 10 ) while ( x == 1 )
{ {
printf ( "\n%d", j ) ; x=x-1;
j=j+1; printf ( "\n%d", x ) ;
} }
} }
(e) main( ) (f) main( )
{ {
int x = 1 ; char x ;
while ( x == 1 ) while ( x = 0 ; x <= 255 ; x++ )
x=x-1; printf ( "\nAscii value %d Character
printf ( "\n%d", x ) ; %c", x, x ) ;
} }

(g) main( ) (h) main( )


{ {
int x = 4, y, z ; int x = 4, y = 3, z ;
y = --x ; z = x-- -y ;
z = x-- ; printf ( "\n%d %d %d", x, y, z ) ;
printf ( "\n%d %d %d", x, y, z ) ; }
}

(i) main( ) (j) main( )


{ {
while ( 'a' < 'b' ) int i = 10 ;
printf ( "\nmalyalam is a palindrome" ) ; while ( i = 20 )
} printf ( "\nA computer buff!" ) ;
}

(k) main( ) (l) main( )


{ {
int i ; float x = 1.1 ;
while ( i = 10 ) while ( x == 1.1 )
{ {
printf ( "\n%d", i ) ; printf ( "\n%f", x ) ;
i=i+1; x = x – 0.1 ;
} }
} }

(m) main( ) (n) main( )


{ {
while ( '1' < '2' ) char x ;
printf ( "\nIn while loop" ) ; for ( x = 0 ; x <= 255 ; x++ )
} printf ( "\nAscii value %d Character
%c", x, x ) ;
}

(o) main( ) (p) main( )


{ {
int x = 4, y = 0, z ; int x = 4, y = 0, z ;
while ( x >= 0 ) while ( x >= 0 )
{ {
x-- ; if ( x == y )
y++ ; break ;
if ( x == y ) else
continue ; printf ( “\n%d %d”, x, y ) ;
else x-- ;
printf ( “\n%d %d”, x, y ) ; y++ ;
} }
} }

(q) main( ) (r) int main()


{ {
int i = 1, j = 1 ; int i = 1, j = 1;
for ( ; ; ) for( ; j; printf("%d %d ",i, j))
{ j = i++ <= 1;
if ( i > 5 ) return 0;
break ; }
else
j += i ;
printf ( "\n%d", j ) ;
i += j ;
}
}

(13)Find the output for following programs

(a) (b)
int main() int main()
{ {
unsigned int a = 10; if (7 & 8)
a = ~a; printf("Honesty");
printf("%d\n", a); if ((~7 & 0x000f) == 8)
} printf("is the best policy\n");}
(c) (d)
void main() void main()
{ {
int x = 97; int a = 5, b = -7, c = 0, d;
int y = sizeof(x++); d = ++a && ++b || ++c;
printf("x is %d", x); printf("\n%d%d%d%d", a, b, c, d);
} }

(e) (f)
void main() int main()
{ {
int a = -5; int x = -2;
int k = (a++, ++a); x = x >> 1;
printf("%d\n", k); printf("%d\n", x);
} }

(g) (h)
int main() void main()
{ {
if (~0 == 1) float x = 0.1;
printf("yes\n"); if (x == 0.1)
else printf("Sanfoundry");
printf("no\n"); else
} printf("Advanced C Classes");
}

(i) (j)
int main() void main()
{ {
unsigned int i = 23; float x = 0.1;
signed char c = -23; printf("%d, ", x);
if (i > c) printf("%f", x);
printf("Yes\n"); }
else if (i < c)
printf("No\n"); }
(k) (l)
void main() int main()
{ {
double ch; switch (printf("Do"))
printf("enter a value between 1 to 2:"); {
scanf("%lf", &ch); case 1:
switch (ch) printf("First\n");
{ break;
case 1: case 2:
printf("1"); printf("Second\n");
break; break;
case 2: default:
printf("2"); printf("Default\n");
break; break;
} }
} }

(m) (n)
int main() int main()
{ {
int i = 0, j = 0; int i = 0;
while (i < 5, j < 10) do
{ {
i++; i++;
j++; if (i == 2)
} continue;
printf("%d, %d\n", i, j); printf("In while loop ");
} } while (i < 2);
printf("%d\n", i);
}

(o) (p)
int main() int main()
{ {
int i = 0, j = 0; int i = 0;
for (i; i < 2; i++){ char c = 'a';
for (j = 0; j < 3; j++){ while (i < 2){
printf("1\n"); i++;
break; switch (c) {
} case 'a':
printf("2\n"); printf("%c ", c);
} break;
printf("after loop\n"); break;
} }
}
printf("after loop\n");
}

(q)
int main()
{
int i = 0, j = 0;
while (i < 2)
{
l1 : i++;
while (j < 3)
{
printf("Loop\n");
goto l1;
}
}
}

You might also like