You are on page 1of 11

BCA –SEMESTER 1

COMPUTER CONCEPT & C PROGRAMMING


Assingment Set 2

1. Ques: Write a program to read three floating point numbers and find their sum
and average.

Ans: #include<stdio.h>

#include<conio.h>

void main()

clrscr();

float x,y,z,avg,sum;

printf("Enter a three floating point number\n");

scanf("%f%f%f",&x,&y,&z);

sum=x+y+z;

avg=sum/3;

printf("sum=%5.2f,avg=%5.2f\n",sum,avg,(x+y+z)/3);

getch();

}
2. Ques: What are precedence of operators? How expressions are evaluated using
precedences?

Ans: Arithmetic Expressions

An expression is a combination of variables constants and operators written according to the


syntax of C language. In C every expression evaluates to a value i.e., every expression results in
some value of a certain type that can be assigned to a variable. Some examples of Cexpressions
are shown in the table given below.
Algebraic C Expression
Expression
axb–c a*b–c

(m + n) (x + y) (m + n) * (x + y)

(ab / c) a*b/c

3x2 +2x + 1 3*x*x+2*x+1

(x / y) + c x/y+c

Evaluation of Expressions

Expressions are evaluated using an assignment statement of the form

Variable = expression;

Variable is any valid C variable name. When the statement is encountered, the expression is
evaluated first and then replaces the previous value of the variable on the left hand side. All
variables used in the expression must be assigned values before evaluation is attempted.

Example of evaluation statements are

x = a * b – c
y = b / c * a
z = a – b / c + d;

The following program illustrates the effect of presence of parenthesis in expressions.

.
main ()
{
float a, b, c x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %fn”,x);
printf (“y = %fn”,y);
printf (“z = %fn”,z);
}
.
output

x = 10.00
y = 7.00
z = 4.00

An arithmetic expression without parentheses will be evaluated from left to right using the rules
of prescedence of operators. There are two distinct priority levels of arithmetic operators in C:

High priority * / %
Low priority + -

Operator precedence and associativity

Each operator in C has a precedence associated with it. The precedence is used to determine how
an expression involving more than one operator is evaluated. There are distinct levels of
precedence and an operator may belong to one of these levels. The operators of higher
precedence are evaluated first.

The operators of same precedence are evaluated from right to left or from left to right depending
on the level. This is known as associativity property of an operator.

This table shows the priority and associativity of each operator in Pike, with the highest priority
at the top:

Operators Associativity
::a a::b left to right
a() a[b] a->b a[b..c] ({}) ([]) (<>) left to right
a++ a-- left to right
!a ~a (type)a ++a --a right to left!
a*b a/b a%b left to right
a+b a-b left to right
a>>b a<<b left to right
a>b a>=b a<b a<=b left to right
a==b a!=b left to right
a&b left to right
a^b left to right
a|b left to right
&& left to right
|| left to right
a?b:c right to left!
= right to left!
+=
-=
*=
/=
%=
<<=
>>=
&=
|=
^=
@a right to left!
, left to right

Examples:

This expression is evaluated in this order


1+2*2 1+(2*2)
1+2*2*4 1+((2*2)*4)
(1+2)*2*4 ((1+2)*2)*4
1+4,c=2|3+5 (1+4),(c=(2|(3+5)))
1 + 5&4 == 3 (1 + 5) & (4 == 3)
c=1,99 (c=1),99
!a++ + ~f() (!(a++)) + (~(f()))
s == "klas" || i < 9 (s == "klas") || (i < 9)
r = s == "sten" r = (s == "sten")

3. Ques: Explain different types of constants.

Ans: Constants

Constants in C refer to fixed values that do not change during the execution of a program. C
supports several types of constants as illustrated in Fig 1

Fig 1

Integer constants

An integer constant refers to a sequence of digits. There are three types of integers, namely
decimal, octal and hexadecimal.
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional – or +.

Examples: 12, -546, 0, 354647, +56

An octal integer constant consists of any combination of digits from the set 0 through 7, with a
leading 0.

Examples: 045, 0, 0567

A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also


include alphabets A through F or a through f. The letters A through F represent numbers 10
through 15.

Examples: 0X6, 0×5B, 0Xbcd, 0X

Real constants

The numbers containing fractional parts like 67.45 are called real(or floating point) constants.
Examples: 0.0045, -8.5, +345.678
A real number may also be expressed in exponential(scientific) notation. The general form is:
mantissa e exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent is
an integer number with an optional plus or minus sign. The letter e separating the mantissa and
the exponent can be written in either lowercase or uppercase.

Examples: 04e4, 12e-2, -1.3E-2

7500000000 may be written as 7.5E9 or 75E8.

Floating point constants are normally represented as double-precision quantities. However, the
suffixes f or F may be used to force single precision and l or L to extend double-precision
further.

Character constants

A single character constant( or simple character constant) contains a single character enclosed
within a pair of single quote marks.

Examples: ‘6’, ‘X’, ‘;’

Character constants have integer values known as ASCII values. For example, the statement

printf(“%d”, ‘a’);

would print the number 97, the ASCII value of the letter a. Similarly, the statement

printf(“%c”, 97);

would print the letter a.


String constants

A string constant is a sequence of characters enclosed within double quotes. The characters may
be letters, numbers, special characters and blank space.

Examples: “Hello!”, “1947”, “5+3”

Backslash character constants

C supports some special backslash character constants that are used in output functions. A list of
such backslash character constants is given in Table 2.1. Note that each one of them represents
one character, although they consist of two characters. These character combinations are called
escape sequences.

Constant Meaning

‘\b’ back space

‘\f’ from feed

‘\n’ new line

‘\r’ carriage return

‘\t’ horizontal tab

‘\v’ vertical tab

‘\’ single quote

‘\”’ double quote

‘\\’ backslash

‘\0’ null

4. Ques: Explain pre increment and post increment operator with an example.

Ans: When you use a post-increment (n++) you are first making a copy of the variable. This is
the value used in your statement. The variable will be incremented, but not until the current line
is done executing. When it finishes, the incremented value will replace n.

When you use a pre-increment (++n) you are first making a copy of the variable, as in post-
increment. The difference is that the value you copied will be incremented right away. This value
will then be copied back into n after the current line is done executing

Just remember that post-increment happens AFTER you use it, and pre-increment happens right
when you use it. Try running the following code to see it in action.
int main(int argc, char** argv) {

int n = 0;

printf("%d\n", n); // this would print out 0, since n = 0

printf("%d\n", n++); // this would print out 0, since n isn't incremented until after this line
printf("%d\n", n); // now n would print out as 1

printf("%d\n", ++n); // now n would print out as 2

// more illustrative examples:


printf("%d\t%d\n", n++, n); // this time we see that both ns are printed out as 2

printf("%d\n", n); // n is only shown as 3 after the post-increment line

printf("%d\t%d\n", ++n, n); // and now we see that the first n is printed as a 4
// while the second is still a 3

printf("%d\n", n); // and once again n is set to 4 after the last line

return 0;
}

5. Ques: Write a program to check whether a given number is palindrome


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
long int n,m,rev=0,r;
clrscr();
printf("\nEnter the number:");
scanf("%ld",&n);
m=n;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
If (rev==m)
printf("Palindrome");
else
printf("Not Palindrome");
getch();
}
output:
Enter the number:123321
Palindrome
Enter the number:12420
Not Palindrome

6. Ques: Write a recursive function to find sum of even numbers from 2 to 50

Ans: int sumEvens(int start, int end) {

// end condition
if (start > end) {
return 0;
}

// correction if we start on an odd number


if (start % 2 == 1) {
return sumEvens(start + 1, end);
}

// actual work
return start + sumEvens(start + 2, end);

7. Ques: Write a program to add two 3x3 matrices.


Ans: #include <stdio.h>
#include <conio.h>

void add_matrices(int a[][3], int b[][3], int result[][3]);


void print_matrix(int a[][3]);
void main(void)
{
int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };
int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };
int r[3][3];
add_matrices(p, q, r);

printf("\nMatrix 1:\n");
print_matrix(p);
printf("\nMatrix 2:\n");
print_matrix(q);
printf("\nResult:\n");
print_matrix(r);
}
void add_matrices(int a[][3], int b[][3], int result[][3])
{
int i, j;
for(i=0; i&lt3; i++)
{
for(j=0; j&lt3; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
}
void print_matrix(int a[][3])
{
int i, j;
for (i=0; i&lt3; i++)
{
for (j=0; j&lt3; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
getch();
}

8. Ques: What is the difference between pointer variable and simple variable? What are the
advantages of pointer variable.
Ans:
A variable has 3 things when you make them :
int <- type
x <- name
=3 <- data

A pointer doesn't have any data in themself, they "point" to a variable.


Like this :

int x = 3;
int *x = x

now the *x is 3, because it "points" to the x variable ( actually it points to it's


memory address )
*x = 6 means that where the pointer is pointing ( the memory part ) will be filled
with this data.
A little more thing:
incrementing ( i++ ) a pointer can do two different things :
(*x) ++ adds +1 to the value , while
*x++ will move the "pointing" to the next thing in the memory ( if we are not
talking about an array, then it's usually memory garbage )
The main advantages of using pointers are
1.) Function cannot return more than one value. But when the same function can
modify many pointer variables and function as if it is returning more than one
variable.
2.) In the case of arrays, we can decide the size of th array at runtime by allocating
the necessary space.
3.) In the case of pointers to classes, we can use polymorphism and virtual classes
to change the behavior of pointers to various types of classes at runtime

You might also like