BCA Computer Concept & C Programming SEMESTER - 1 ST Assignment
BCA Computer Concept & C Programming SEMESTER - 1 ST Assignment
1. Ques: What is the difference between declaration and initialization? Explain with
example.
Ans: Declaration means that you are declaring a variable. In programming, to declare is to define
the name and data type of a variable or other programming construct. Many programming
languages, including C and Pascal, require you to declare variables before using them. For
example,
int x; / / here the integer variable x is just declared.
Declaration is:
int x;
double y;
char a;
Initialization means you are assigning a value to that variable. Initialize can refer to the process of
starting up a program or system.
For example=10; / /here the integer variable x, declared above, is assigned value 10.
Initialization is: x
= 1;
=
double 2.3;
char = 'w';
Also you can use declaration and initialization in the same time:
int x = 1;
double y = 2.3;
char a = 'w';
The operator? : works as follows: exprl is evaluated first. lfit is nonzero (true), then the
expression expr 2 is evaluated and becomes the value of the expression. If expr 1 is false,
expr3 is evaluated and its value becomes the value of the expression. For example, consider
the following statements:
a=100;
b=200;
c=(a>b)?a:b;
In this example, c will be assigned the value of b. This can be achieved using the if...Else
statements as follows:
if(a>b)
c=a;
else
c=b;
3. Ques: What are the commonly used input/output functions in C? How are they
accessed.
Ans: We have already seen that the C language is accompanied by some library functions to
handle input/output (I/O) operations. Commonly used input/output functions in C are six I/O
functions:
getchar 0, putchar 0, scanf 0, printf 0, gets 0 and puts 0 are the commonly used input/output
functions in C. These functions are used to transfer of information between the computer and the
standard input/output devices. getchar 0 and putchar 0 are the two functions to read and write single
character. scanf 0 and printf 0 are the two formatted input/output functions. These functions can
handle characters, numerical values and strings as well. gets 0 and puts 0 functions are used to
handle strings. scanf 0, printf 0, gets 0 and puts 0 functions are used in interactive programming.
These functions can be accessed within a program by including the header file stdlo.h.
The if statement
The simplest way to modify the control flow of a program is with an if statement, which in its
simplest form looks like this:
if(x> max)
max = x;
Even if you didn't know any C, it would probably be pretty obvious that what happens here is that if
x is greater than max, x gets assigned to max.
An if statement may also optionally contain a second statement, the "else clause," which is to be
executed if the condition is not met. Here is an example:
if(n > 0)
average = sum / n;
else {
average = 0;
Nesting of if statements
Irs also possible to nest one if statement inside another. (For that matter, irs in general possible
to nest any kind of statement or control flow construct within another.) For example, here is a
little piece of code which decides roughly which quadrant of the compass you're walking into,
based on an x value which is positive if you're walking east, and a y value which is positive if
you're walking north:
Example:
if(x> 0)
if(y > 0)
switch ( <variable> )
printf ("Northeast.\n");
{ case this-value:
else printf("Southeast.\n");
Code to execute if <variable> == this-value
}
break;
else {
case that-value:
if(y> 0)
Code to execute if <variable> == that-value
printf("N orthwest. \n ");
break;
else printf("Southwest. \n");
}
default:
When
Code toyou have one
execute if statement
if <variable> (or not
does loop) nested
equal the inside another, it's
value following anya very
of thegood idea to use
cases
explicit braces { }, as shown, to make it clear (both to you and to the compiler) how they're
nested
break; and which else goes with which if. It's also a good idea to indent the various levels, also as
shown, to make the code more readable to humans. Why do both? You use indentation to make
the code visually more readable to yourself and other humans, but the compiler doesn't pay
}
attention to the indentation (since all whitespace is essentially equivalent and is essentially
ignored). Therefore, you also have to make sure that the punctuation is right.
6. Ques: Write a recursive program to solve Towers of Hanoi problem.
Ans:
5. Ques: Explain with general syntax the switch statement.
#inc1ude<stdio.h>
Ans:
maim)
void The switch
Recursive statement
Hanoi(int, char, char, char); int
The
{ switch case statements are a substitute for long if statements that compare a variable to
n;
several "integral" values ("integral" values are simply values that can be expressed as an integer,
such as the value of a char). The basic format for using switch case is outlined below. The
printfi"
value ofTowers of Hanoi\n\n");
the variable given into switch is compared to the value following each of the cases, and
when one value matches the value of the variable, the computer continues executing the
printfi"
programHowfrommany disks?");
that point.
scanf("%d", &n);
printf("\n");
1* Transfer n disks from one pole to another *1
1* n= number of disks
from=origin
to=destination
temp=temporary storage *1
if(n>O){
return;
int i;
declares a single variable, named i, of type int. It is also possible to declare an array of
several elements. The declaration
int a[IO];
declares an array, named a, consisting often elements, each of type int. Simply speaking, an
array is a variable that can hold more than one value. You specify which of the several values
you're referring to at any given time by using a numeric subscript. (Arrays in
programming are similar to vectors or matrices in mathematics.) We can represent the
array a above with a picture like this:
The C language allows arrays of any dimension to be defined. In this section, we will take a
look at two-dimensional arrays. One of the most natural applications for a two dimensional
array arises in the case of a matrix. In C, the two-dimensional matrix can be declared as
follows:
int array [3 ] [6];
int array[3][6] = {
{4,5,6,7,8,9},
{I ,5,6,8,2,4},
{0,4,4,3,1,1 }
};
if (array[2][1] > 0) {
Remember that, like ordinary arrays, two-dimensional arrays are numbered from O.
Therefore, the array above has elements from array [0] [0] to array [2] [5].
8. Ques: Write a program to illustrate the concept of extern variable.
Ans: Type and save the following program in a source file called externvariables.h
float rate=5.5;
int time=2;
float interest;
Type and save the following program in a separate source file called demoexternvar.c
#include<stdio.h>
#include "externvariables.h" 1* the source file where the external variables are defined
should be included here. * I
main( )
{
1* external declarations of the variables which are defined in externvariables.h
*1
extern int principle;
extern float rate;
extern int time;
printf("Interest=%f\n", interest);