Sie sind auf Seite 1von 7

BCA -SEMESTER 1

COMPUTER CONCEPT & C PROGRAMMING


Assingment Set 1

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';

2. Ques: What is conditional operator? using conditional operator find a smallest


number among two numbers.

Ans: The Conditional operator

The Conditional operator (ternary operator) pair "?:" is available in C to construct


conditional expressions of the form
expr 17 expr2:expr3

Where expr 1, expr 2 and expr 3 are expressions.

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 & puts 0

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.

4. Ques: Explain the different types of if statements with examples.


Ans: The if statement may be implemented in different forms depending on the complexity of
condition to be tested. the different forms are :-
1) Simple if statement
2) If ............else statement
3) Nested If .... else statement

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.

Example: Program to calculate the absolute value of an integer

# include < stdio.h >


void main ()
{
int number;
printf ("Type a number:");
scanf ("%d", & number);
if (number < 0) /* check whether the number is a negative number */

number = ~ number; /* If it is negative then convert it into positive. * /


printf ("The absolute value is % d \n", number);
}

The if-else statement

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 {

printf("can't compute average\n");

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){

1* move n-I disks from origin to temporary *1

Recursive _ Hanoim-l , from, temp, to);

1* move nth disk from origin to destination *1

printf(" Move disk %d from %c to %c\n", n, from, to);

1* move n-I disks from temporary to destination *1

Recursive_Hanoi(n-l, temp, to, from);

return;

7. Ques: Explain single dimensional and two dimensional array concepts.

Ans: One Dimensional Arrays


So far, we've been declaring simple variables: the declaration

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:

Multidimensional Arrays or Two dimensional

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];

Following is the way of declaring as well as initializing two-dimensional arrays.

int array[3][6] = {

{4,5,6,7,8,9},
{I ,5,6,8,2,4},

{0,4,4,3,1,1 }

};

Such arrays are accessed like so:

array[l] [4]= -2;

if (array[2][1] > 0) {

printf("Element [2][1] is %d", array[2][I]);

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

int principle= 10000;

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;

extern float interest;


I*compute interest* I

interest= principle*rate*timell 00.0;

printf("Interest=%f\n", interest);

Das könnte Ihnen auch gefallen