You are on page 1of 9

NAME Anshika Mahajan

ROLL NO 2114100019
PROGRAM MASTER OF COMPUTER APPLICATION (MCA)
SEMESTER SEMESTER 1
COURSE CODE & NAME DCA6102 – PROGRAMMING IN C

SESSION FEB/MAR 2021


SET 2

Q1 (a) Explain goto statement with example.

Ans:

The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the
program control to a predefined label. The goto statment can be used to repeat some part of the code
for a particular condition. It can also be used to break the multiple loops which can't be done by using a
single break statement.

The goto requires a label in order to identify the place where the branch is to be made. A label is any
valid variable name, and must be followed by a colon. The label is placed immediately before the
statement where the control is to be transferred. The general forms of goto and label statements are
shown below:

The label can be anywhere in the program either before the goto or after the goto label; statement.
During execution of the program when a statement like

goto first;

is met, the flow of control will jump to the statement immediately following the label first. This happens
unconditionally.

A goto breaks the normal sequential execution of the program. If the label is before the statement goto
label; a loop will be formed and some statements will be executed repeatedly. Such a jump is known as
a backward jump. On the other hand, if the label is placed after the goto label; some statements will be
skipped and the jump is known as the forward jump.

A goto is often used at the end of a program to direct the control to go to the input statement, to read
further data.

Example:

#include <stdio.h>

int main()

int num,i=1;

printf("Enter the number whose table you want to print?");

scanf("%d",&num);

table:

printf("%d x %d = %d\n",num,i,num*i);

i++;

if(i<=10)

goto table;

Output:

Enter the number whose table you want to print?10

10 x 1 = 10

10 x 2 = 20

10 x 3 = 30

10 x 4 = 40

10 x 5 = 50

10 x 6 = 60

10 x 7 = 70

10 x 8 = 80
10 x 9 = 90

10 x 10 = 100

Q1 (B)Explain nested if statement.

A nested if in C is an if statement that is the target of another if statement. Nested if statements mean
an if statement inside another if statement.

Example

//Program to print the largest of three numbers

#include<stdio.h>

main()

int a,b,c,big;

printf (“Enter three numbers”);

scanf (“%d %d %d”, &a, &b, &c);

if (a>b) // check whether a is greater than b if true then

if(a>c) // check whether a is greater than c


big = a ; // assign a to big

else big = c ; // assign c to big

else if (b>c) // if the condition (a>b) fails check whether b is greater than c

big = b ; // assign b to big

else big = c ; // assign C to big

printf (“Largest of %d,%d&%d = %d”, a,b,c,big);

Q2 (a) What is a function? How can you declare a function? Explain with example.

Ans:

A function is a self-contained program segment that carries out some specific, well-defined task. Every C
program contains one or more functions. One of these functions must be called main. Program
execution will always begin by carrying out the instructions in main. Additional functions will be
subordinate to main, and perhaps to one another.

Parameters (also called formal parameters) or arguments are the special identifiers through which
information can be passed to the function. A function has a body containing the actual instructions
(statements) for carrying out the task the function is supposed to perform; and it may give you back a
return value, of a particular type.

In general terms, the first line can be written as

data-type name(data-type parameter 1, data-type parameter 2, …, data-type parameter n)

Example : In this example, a function which accepts one argument, multiplies it by 4, and hands that
value back.

int multbyfour(int x)

int retval;

retval = x * 4;

return retval;

On the first line we see the return type of the function (int), the name of the function (multbyfour), and
a list of the function's arguments, enclosed in parentheses. Each argument has both a name and a type;
multbyfour accepts one argument, of type int, named x. The name x is arbitrary, and is used only within
the definition of multbyfour. The caller of this function only needs to know that a single argument of
type int is expected; the caller does not need to know what name the function will use internally to refer
to that argument.

It is considered good practice to use prototype declarations for all functions that you call. These
prototypes help to ensure that the compiler can generate correct code for calling the functions, as well
as allowing the compiler to catch certain mistakes one might make.

In general terms, a function prototype can be written as

data-type function_name(type1, type2, …, type n)

Examples:

int sample(int, int) or int sample(int a, int b);

float fun(int, float) or float fun( int a, float b);

If you write the function definition after the definition of its caller function, then the prototype is
required in the caller, but the prototype is optional if you write the definition of the function before the
definition of the caller function. But it is good programming practice to include the function prototype
wherever it is defined.

Q2 (b) What is recursion? Give an example of recursion.

Ans:

Recursion is a process by which a function calls itself repeatedly, until some specified condition has been
met. The process is used for repetitive computations in which each action is stated in terms of a
previous result. Many repetitive problems can be written in this form.

Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined in
terms of similar subtasks. For Example, recursion may be applied to sorting, searching, and traversal
problems.

In order to solve a problem recursively, two conditions must be satisfied. First, the problem must be
written in a recursive form, and the second, the problem statement must include a stopping condition.

Example :

Factorial of a number. In the following example, recursion is used to calculate the factorial of a positive
integer, n. It is normally expressed as n!=1 x 2 x 3 x … x n.
This can also be written as n!=n x (n-1)!. This is the recursive statement of the problem in which the
desired action(the calculation of n!) is expressed in terms of a previous result (the value of (n-1)! which
is assumed to be known). Also, since we know that 0!=1 by definition. This expression provides stopping
condition for the recursion.

Thus the recursive definition for finding factorial of positive integer n can be written as:

fact(n)={ 1 if n=0

n x fact(n-1) otherwise}

Example:

//Program to find factorial of a given positive integer

#include <stdio.h>

int main()

{ int n;

long int fact(int);

/* Read in the integer quantity*/

scanf(“%d”, &n);

/*calaculate and display the factorial*/

printf(“n!=%ld\n”, fact(n));

long int fact(int n)

if(n==0)

return(1);

else

return (n*fact(n-1));

Q3 (a) What do you understand by storage class?


Ans:

Storage Classes
There are two ways to categorize variables: by data type, and by storage class. Data type refers to the
type of information represented by a variable, for example, integer number, floating-point number,
character etc. Storage class refers to the persistence of a variable and its scope within the program, that
is, the portion of the program over which the variable is recognized.

The following types of storage-class specifications in C are discussed in this unit: global, automatic or
local, static, and extern. The exact procedure for establishing a storage class for a variable depends upon
the particular storage class, and the manner in which the program is organized, (i.e. single file vs.
multiple file).

Why would you want to limit the visibility of a variable? For maximum flexibility, wouldn't it be handy if
all variables were potentially visible everywhere? As it happens, that arrangement would be too flexible:
everywhere in a program, you would have to keep track of the names of all the variables declared
anywhere else in the program, so that you didn't accidentally re-use one. Whenever a variable had the
wrong value by mistake, you'd have to search the entire program for the bug, because any statement in
the entire program could potentially have modified that variable. You would constantly be stepping all
over yourself by using a common variable name like i in two parts of your program, and having one
snippet of code accidentally overwrite the values being used by another part of the code.

Q3 (b) What is static storage class? Write a program to generate Fibonacci numbers using static
variables.

Ans:

Static Storage Class


The static storage class instructs the compiler to keep a local variable in existence during the life-time of
the program instead of creating and destroying it each time it comes into and goes out of scope.
Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's
scope to be restricted to the file in which it is declared.

In C programming, when static is used on a global variable, it causes only one copy of that member to be
shared by all the objects of its class. Static variables are defined within individual functions and
therefore have the same scope as automatic variables, i.e. they are local to the functions in which they
are declared. Unlike automatic variables, however, static variables retain their values throughout the life
of the program. As a result, if a function is exited and then reentered later, the static variables defined
within that function will retain their previous values. This feature allows functions to retain information
permanently throughout the execution of a program. Static variables can be utilized within the function
in the same manner as other variables. They cannot be accessed outside of their defining function. In
order to declare a static variable the keyword static is used as shown below:

static int count;

You can define automatic or static variables having the same name as global variables. In such situations
the local variables will take precedence over the global variables, though the values of global variables
will be unaffected by any manipulation of the local variables.

Initial values can be included in static variable declaration. The rules associated with the initialization
remain same as the initialization of automatic or global variables. They are:

1. The initial values must be constants, not expressions.

2. The initial values are assigned to their respective variables at the beginning of the program execution.
The variables retain these values throughout the life of the program, unless different values are assigned
during the course of computation.

3. Zeros will be assigned to all static variables whose declarations do not include explicit initial values.

Example:

// Program to generate Fibonacci numbers.

#include<stdio.h>

main()

int count, n;

long int fib(int);

printf(“\n How many Fibonacci numbers?”);

scanf(“%d\n”, &n);

for(count=1;count<=n;count++)

printf(“\ni=%d F=%ld”, count, fib(count));

long int fib(int count)


{

/* calculate a Fibonacci number using the formula

if i=1, F=0; if i=2, F=1, and F=F1+F2 for i>=3 */

static long int f1=0, f2=1; /* declaration of static variables */

long int f;

if (count==1)

f=0;

else if (count==2)

f=1;

else

f=f1+f2;

f2=f1;

f1=f; /* f1 and f2 retain their values between different calls of the

function*/

return f;

You might also like