0% found this document useful (0 votes)
117 views

Structure Programming Approach

This document provides solutions to questions from a structured programming approach prelim exam. It discusses type casting in C with examples, integer promotion with character addition, usual arithmetic conversions with mixed data types, and string handling library functions like strcat, strcmp, strcpy, and strlen with examples. It also explains function calls, return values, and parameters with examples.

Uploaded by

jayesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Structure Programming Approach

This document provides solutions to questions from a structured programming approach prelim exam. It discusses type casting in C with examples, integer promotion with character addition, usual arithmetic conversions with mixed data types, and string handling library functions like strcat, strcmp, strcpy, and strlen with examples. It also explains function calls, return values, and parameters with examples.

Uploaded by

jayesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

F.E. Sem.

II
Structured Programming Approach
Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 80

Q.1(a) Explain type casting in C. [5]


(A) Typecasting is a way to convert a variable from one data type to another
data type. For example if you want to store a long value into a simple integer
then you can type cast long to int. You can convert values from one type to
another explicitly using the cast operator as follows:
(type_name) expression

Consider the following example where the cast operator causes the division
of one integer variable by another to be performed as a floating-point
operation:
#include <stdio.h>

main()
{
int sum = 17, count = 5;
double mean;

mean = (double) sum / count;


printf("Value of mean : %f\n", mean );
}

When the above code is compiled and executed, it produces the following
result:
Value of mean : 3.400000

It should be noted here that the cast operator has precedence over division,
so the value of sum is first converted to type double and finally it gets
divided by count yielding a double value.

Type conversions can be implicit which is performed by the compiler


automatically, or it can be specified explicitly through the use of the cast
operator. It is considered good programming practice to use the cast
operator whenever type conversions are necessary. [2 marks]

1
Vidyalankar : F.E.  SPA

Integer Promotion [1½ marks]


Integer promotion is the process by which values of integer type "smaller"
than int or unsigned int are converted either to int or unsigned int. Consider
an example of adding a character in an int:
#include <stdio.h>
main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;

sum = i + c;
printf("Value of sum : %d\n", sum );
}

When the above code is compiled and executed, it produces the following result:
Value of sum : 116

Here value of sum is coming as 116 because compiler is doing integer


promotion and converting the value of 'c' to ascii before performing actual
addition operation.

Usual Arithmetic Conversion [1½ marks]


The usual arithmetic conversions are implicitly
performed to cast their values in a common type.
Compiler first performs integer promotion, if
operands still have different types then they are
converted to the type that appears highest in the
following hierarchy:
The usual arithmetic conversions are not performed
for the assignment operators, nor for the logical
operators && and ||. Let us take following example to
understand the concept:
#include <stdio.h>
main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
float sum;

sum = i + c;
printf("Value of sum : %f\n", sum );
}

2
Prelim Question Paper Solution

When the above code is compiled and executed, it produces the following
result:
Value of sum : 116.000000

Here it is simple to understand that first c gets converted to integer but


because final value is double, so usual arithmetic conversion applies and
compiler convert i and c into float and add them yielding a float result.

Q.1(b) Explain any three string library functions defined in string [5]
header file string.h with suitable example.
(A) String-Handling Functions [1 mark]
Fortunately, the C library supports a large number of stringhandling functions
that can be used to carry out many of the string manipulations discussed so
far. Following are the most commonly used stringhandling functions.

Function Action
strcat( ) concatenates two strings
strcmp( ) compares two strings
strcpy( ) copies one string over another
strlen( ) finds the length of a string

We shall discuss briefly how each of these functions can be used in the
processing of strings.

(i) strcat(s1, s2) [1 mark]


strcat( ) Function
The strcat function joins two strings together. It takes the following form:
strcat(string1, string2);

string1 and string2 are character arrays. When the function strcat
executed, string2 is appended to string1. It does so by removing the null
character at the end of string1 and placing string2 from there. The
string at string2 remains unchanged. For example, consider the following
three strings:

0 1 2 3 4 5 6 7 8 9 0 1
Part1 = V E R Y \0
0 1 2 3 4 5 6
Part2 = G O O D \0
0 1 2 3 4 5 6
Part3 = B A D \0

3
Vidyalankar : F.E.  SPA

Execution of the statement


strcat (part1, part2);
will result in:
0 1 2 3 4 5 6 7 8 9 0 1 2
Part1 = V E R Y G O O D \0

0 1 2 3 4 5 6
Part2 = G O O D \0

while the statement


will result in:
0 1 2 3 4 5 6 7 8 9 0 1 2
Part1 = V E R Y B A D \0
0 1 2 3 4 5 6
Part3 = B A D \0

We must make sure that the size of string1 (to which string2 is
appended) is large enough to accommodate the final string.

strcat function may also append a string constant to a string variable. The
following is valid:
strcat(part1, “GOOD”) ;
C permits nesting of strcat functions. For example, the statement
strcat (strcat (string1, string2), string3) ;

is allowed and concatenates all the three strings together. The resultant
string is stored in string1.

(ii) strcmp(s1,s2) [1 mark]


strcmp( ) Function
The strcmp function compares two strings identified by the arguments
and has a value 0 if they are equal. If they are not, it has the numeric
difference between the first nonmatching characters in the strings. It
takes the form:
strcmp(string1, string2);

string1 and string2 may be string variable or string constants. Examples are:
strcmp (name1, name2) ;
strcmp (name1, “John”) ;
strcmp (“Rom”, “Ram”) ;

4
Prelim Question Paper Solution

Our major concern is to determine whether the strings are equal; if not,
which is alphabetically above. The value of the mismatch is rarely
important. For example, the statement
strcmp(“their”, “there”) ;

will return a value of 9 which is the numeric difference between ASCII


“i” and ASCII "r". That is, “i” minus “r” in ASCII code is 9. If the value
is negative, string1 is alphabetically above string2.

(iii) strcpy(s1,s2) [1 mark]


strcpy( ) Function
The strcpy function works almost like a stringassignment operator. It
takes the form:
strcpy(string1, string2);

and assigns the contents of string2 to string1. string2 may be a


character array variable or a string constant. For example, the
statement
strcpy(city, “DELHI”) ;

will assign the string “DELHI” to the string variable city. Similarly, the
statement
strcpy(city1, city2) ;

will assign the contents of the string variable city2 to the string variable
city1. The size of the array city1 should be large enough to receive the
contents of city2.

(iv) strlen(s1) [1 mark]


strlen( ) Function
This function counts and returns the number of characters in a string. It
takes the form
n = strlen = strlen(string);

Where n is an integer variable, which receives the value of the length of


the string. The argument may be a string constant. The counting ends at
the first null character.

Q.1(c) Explain any three terms related to function by giving suitable [5]
example :
(i) Function call
(ii) Function definition
(iii) Function declaration
(iv) Arguments of a function.

5
Vidyalankar : F.E.  SPA

(A) (i) Function call


A function is a group of statements that together perform a task. Every
C program has at least one function, which is main(), and all the most
trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up
your code among different functions is up to you, but logically the
division is such that each function performs a specific task.

(ii) Function definition


A function can also be referred as a method or a sub-routine or a
procedure, etc.

Defining a Function
The general form of a function definition in C programming language is as
follows:
return_type function_name( parameter list)
{
body of the function
}
e.g. : int fact (int n)
{ if (n == 1)
return 1;
else
return (n  fact (n1));
}

(iii) Function declaration


A function declaration tells the compiler about a function's name, return
type, and parameters. A function definition provides the actual body of
the function.
e.g. : f =fact(n); // function declearation.

The C standard library provides numerous built-in functions that your


program can call. For example, strcat() to concatenate two strings, memcpy()
to copy one memory location to another location, and many more functions.

(iv) Arguments of a function /Parameters : A parameter is like a


placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument.
The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function
may contain no parameters.
e.g. : f = fact (n) ; // n is actual parameter.

6
Prelim Question Paper Solution

Q.1(d) Differentiate between while and do while loop. [5]


(A) Difference between while and do-while loop
While do-while
1. It is an entry controlled loop. It is exit controlled loop. [1 mark]
2. It first check the condition & It execute loop first and then [1 mark]
then execute the loop. check the condition.
3. If the condition is false, Even if the condition is false, [2 marks]
while loop does not get do-while loop get execute
executed. once.

Q.2(a) Differentiate call by value and call by reference? Write a C [10]


program to calculate call by value and call by reference with
suitable example.
(A) Call By Value: [2 marks]
The call by value method of passing arguments to a function copies the
actual value of an argument into the formal parameter of the function. In
this case, changes made to the parameter inside the function have no effect
on the argument.

By default, C programming language uses call by value method to pass


arguments. In general, this means that code within a function cannot alter
the arguments used to call the function.

Call By Reference : [2 marks]


The call by reference method of passing arguments to a function copies the
address of an argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call. This means
that changes made to the parameter affect the passed argument.
To pass the value by reference, argument pointers are passed to the
functions just like any other value

Program : [3 marks]
1. Factorial of a given number using call by value
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n,ans;
clrscr();
ans=fact(n);

7
Vidyalankar : F.E.  SPA

getch();
}
int fact(int n)
{
int i,fac =1;
for(i=0;i<=n;i++)
fac=fac*i;
return fac;
}

2: Factorial of a given number using call by reference [3 marks]


#include<stdio.h>
#include<conio.h>
int fact(int *p);
void main(){
int n,ans;
clrscr();
ans=fact(&n);
getch();
}
int fact(int *n){
int i,fac=1;
for(i=0;i<=*p;i++)
fac=fac*i;
return fac;
}

Q.2(b) Explain different storage classes with suitable example. [10]


(A) Different Storage Classes [2 marks]
Storage class / scope and life of a variable
Storage class basically tell the compiler about the particular variable for
following things :
(i) Memory or storage
(ii) Initial or default value i.e. the value which when not assigned.
(iii) Scope i.e. area in which variable can be accessible.
(iv) Life which tells how long the variable can exist.
There are total four types of storage classes :
(1) Auto [2 marks]
It is default storage class used in Cprogramming.
(i) Memory  secondary memory (Hard disk, ROM)
(ii) Default value  garbage value

8
Prelim Question Paper Solution

(iii) Scope  local


(iv) Life  Fill the control remain within the particular block.
Example : {
int a = 10;
printf (“%d”, a); // 10
{
a = 20
printf (“%d”, a); // 20
}
printf (“%d”, a); // 10
}

(2) Register [2 marks]


In this the variable will get stored in a C.P.U. register memory i.e.
primary memory. Those variables can be assessed faster than the one
which is stored in secondary memory.
(i) Memory  primary register memory (RAM)
(ii) Default value  garbage value
(iii) Scope  local
(iv) Life  Till the control remain within the particular block.
Example : {
register int i;
for (i = 1; i < = n, i + +)
{
______
______
______
}

(3) Static [2 marks]


Static variables are the variables which keep their value fined between
different function calls. Static variables can be initialized only once
when the function get call 1st time. After 1st call they always persist
their value and will not get initialized for each time
(i) Memory  secondary memory
(ii) default value  0
(iii) scope  local
(iv) life  value persist between different function calls
Example : void main ( )
{
increment ( );

9
Vidyalankar : F.E.  SPA

increment ( );
increment ( );
getch ( );
}
increment ( )
{
static int i;
printf (“%d”, i);
i + +;
}

(4) Extern [2 marks]


Extern variables are declared outside the functions but are available and
accessible to all functions which uses them.
(i) Memory  secondary memory
(ii) Default value  0
(iii) Scope  global
(iv) Life  Till the program execution comes to end.

Example : int a = 10;


void main ( )
{
printf (“%d”, a); // 10
increment ( );
printf (“%d”, a); // 12
}
increment ( )
{
printf (“%d”, a); // 10
a = a + 2;
printf (“%d”, a); // 12
}

Q.3(a) Define a structure called cricket that will describe following [10]
information. Player name, country name, best score, batting
average. Develop a program that will store information of 25
cricket players using the structure.
(i) Display the name of those cricketers in decaending order with
respect to their batting average
(ii) Display the name of the player with highest batting average.

10
Prelim Question Paper Solution

(A) # include < stdio.h >


# include < conio.h >
struct cricket
{
char name, country ;
int best_score, batting_average ;
};
void main ( )
{
struct cricket c [25]; temp ;
int i, j ;
for (i = 0; i < n; i++)
{
printf (“Enter player details”) ;
scanf (“%s % s %d ; %d & if [i]. name,
& c [i] country, & c [i].best_score, & c [i].batting_average)
}
printf (“Player details names according to batting average”) ;
for (i = 1; i < 25; i++)
{
for (j = 0; j < 25  i; j++)
{
if (c [j].batting_average < c [j + 1].batting_average)
{
temp = c [j] ;
c [j] = c [j + 1] ;
c [j + 1] . temp ;
}
}
}
for (i = 0; i < 25 ; i++)
{
printf (“%s” ; c[i]. name) ;
}
printf (“Player with highest batting average %s, c [0].name) ;
getch ( ) ;
} [10 marks]

Q.3(b) What is file? What are the types of files? What are the different [10]
file handling modes?
(A) In C programming, file is a place on disk where a group of related data is stored.

11
Vidyalankar : F.E.  SPA

Why files are needed? [2 marks]


When the program is terminated, the entire data is lost in C programming. If you
want to keep large volume of data, it is time consuming to enter the entire data.
But, if file is created, these information can be accessed using few commands.
There are large numbers of functions to handle file I/O in C language. In
this tutorial, you will learn to handle standard I/O(High level file I/O
functions) in C.
High level file I/O functions can be categorized as:
1. Text file
2. Binary file

File Operations [2 marks]


1. Creating a new file
2. Opening an existing file
3. Reading from and writing information to a file
4. Closing a file

File Operating modes : [6 marks]


r Open for reading.
w Open for writing. If the file exists, its contents are
overwritten. If the file does not exist, it
will be created.
a Open for append. i.e, Data If the file does not exists, it will be
is added to end of file. created.
r+ Open for both reading and If the file does not exist, fopen() returns
writing. NULL.
w+ Open for both reading and If the file exists, its contents are
writing. overwritten. If the file does not exist, it
will be created.
a+ Open for both reading and If the file does not exists, it will be
appending. created.
Q.4(a) Develop an algorithm to fine GCD and LCM of two integers [6]
(A) Algorithm
i) Take input a and b from user
[1 mark]
ii) Calculate c=a*b
iii) while(b!=0)
{ t=a%b;
a=b; [3 marks]
b=t;
}
iv) gcd=a;
[2 mark]
v) lcm= c/gcd
12
Prelim Question Paper Solution

Q.4(b) Write a program to print following pattern [6]


*
* *
* * *
* * * *
(A)
*
* *
* * *
* * * *

#include <stdio.h>
#include <conio.h> [1 mark]
void main ( )
{
int i, j;
for (i = 1; i <= s; i++)
{ [3 marks]
for (j = 1; j <= i; j++)
{
printf (“ * ”);
}
printf (“\n”);
} [2 marks]
getch ( );
}

Q.4(c) Write a C program to perform matrix multiplication. [8]


(A) #include<stdio.h> [8 marks]
#include<conio.h>
void main()
{
int m, n, p, i, j, a[10][10], b[10][10] ;
void MatMul (int a[10][10], int b[10][10], int m, int n, int p);
clrscr();
printf("Enter the number of rows and columns of matrix 1:");
scanf("%d %d", &m, &n);
printf("Enter the values of matrix l\n");
for(i=0;i<=m1;i++)
{
for(j=0;j<=n1;j++)

13
Vidyalankar : F.E.  SPA

{
printf("Enter a value:");
scanf("%d", &a[i] [j]) ;
}
}
printf("The number of rows for matrix 2 will be %d\n",n);
printf("Enter the columns of matrix 2:");
scanf("%d", &p);
printf("Enter the elements of matrix 2:\n");
for (i=0;i<=n1;i++)
{
for(j=0;j<=p1;j++)
{
printf("Enter a value:");
scanf("%d", &b[i] [j]) ;
}
}
MatMul (a, b, m, n, p);
getch();
}
void MatMul (int a [10] [10], int b[10][10], int m, int n, int p)
{
int i, j , k, c [10] [10] ;
for (i=0;i<=m1; i++)
{
for(j=0;j<=p1;j++)
{
c[i] [j]=0;
for(k=0;k<=n1;k++)
{
c[i] [j]=c[i] [j]+a[i] [k]*b[k] [j];
}
}
}
printf("The resultant matrix is:\n");
for(i=0;i<=m1;i++)
{
for(j=0;j<=p1;j++)
{
printf("%d\t", c[i] [j]) ;
}

14
Prelim Question Paper Solution

printf("\n");
}
}

Q.5(a) Explain bitwise operators in C [5]


(A) Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ are as follows: [2 marks]
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011

The Bitwise operators supported by C language are listed in the following


table. Assume variable A holds 60 and variable B holds 13, then

Example [3 marks]
Try the following example to understand all the bitwise operators available
in C programming language:
#include<stdio.h>
main()
{
unsignedint a =60; /* 60 = 0011 1100 */
unsignedint b =13; /* 13 = 0000 1101 */
int c =0;

c = a & b;/* 12 = 0000 1100 */


printf("Line 1 - Value of c is %d\n", c );

c = a | b;/* 61 = 0011 1101 */


printf("Line 2 - Value of c is %d\n", c );

15
Vidyalankar : F.E.  SPA

c = a ^ b;/* 49 = 0011 0001 */


printf("Line 3 - Value of c is %d\n", c );

c =~a;/*-61 = 1100 0011 */


printf("Line 4 - Value of c is %d\n", c );

c = a <<2;/* 240 = 1111 0000 */


printf("Line 5 - Value of c is %d\n", c );

c = a >>2;/* 15 = 0000 1111 */


printf("Line 6 - Value of c is %d\n", c );
}

When you compile and execute the above program it produces the following
result:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

Operator Description Example


& Binary AND Operator copies a bit to (A & B) will give 12 which is
the result if it exists in both operands. 0000 1100
| Binary OR Operator copies a bit if it (A | B) will give 61 which is
exists in either operand. 0011 1101
^ Binary XOR Operator copies the bit if (A ^ B) will give 49 which
it is set in one operand but not both. is 0011 0001
~ Binary Ones Complement Operator is (~A) will give -61 which is
unary and has the effect of 'flipping' 1100 0011 in 2's
bits. complement form due to a
signed binary number.
<< Binary Left Shift Operator. The left A << 2 will give 240 which
operands value is moved left by the is 1111 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left A >> 2 will give 15 which is
operands value is moved right by the 0000 1111
number of bits specified by the right
operand.

16
Prelim Question Paper Solution

Q.5(b) Explain general structure and flow chart of : else if ladder [5]
(A) else  if ladder [3 marks]
General structure :
if (c1)
{
b1
}
else if (c2)
{
b2
}
else if (c3)
{
b3
}


else if (cn)
{
bn
}
else
{
else body
}
Flow chart : [2 marks]
Y N
c1

Y N
c2

b2

Y N
cn

bn else body

stop

17
Vidyalankar : F.E.  SPA

else  if ladder is used to reduce the complexity obtained in nested if 


else. Generally a program which contains multiple conditions can be easily
evaluated with else  if ladder.

Q.5(c) State the difference between gets() and puts() function. [5]
(A) Using getchar and gets Functions [2½ marks]
We have discussed, as to how to read a single character from the terminal,
using the function getchar. We can use this function repeatedly to read
successive single characters from the input and place them into a character
array. Thus, an entire line of text can be read and stored in an array. The
reading is terminated when the newline character (‘\n’) is entered and the
null character is then inserted at the end of the string. The getchar
function call takes the form:
char ch;
ch = getchar( );

Note that the getchar function has no parameters.

Every time a character is read, it is assigned to its location in the string line
and then tested for newline character. When the newline character is read
(signaling the end of line), the reading loop is terminated and the newline
character is replaced by the null character to indicate the end of character
string.

When the loop is exited, the value of the index c is one number higher than
the last character position in the string (since it has been incremented after
assigning the new character to the string). Therefore the index value c-1
gives the position where the null character is to be stored.

Another and more convenient method of reading a string of text containing


whitespaces is to use library function gets available in the <stdio.h> header
file. This is a simple function with one string parameter and called as under:
gets (str);

str is a string variable declared properly. It reads characters into str from
the keyboard until a newline character is encountered and then appends a
null character to the string. Unlike scanf, it does not skip whitespaces. For
example the code segment
char line [80];
gets (line) ;
printf (“%s”, line);

18
Prelim Question Paper Solution

reads a line of text from the keyboard and displays it on the screen. The
last two statements may be combined as follows:
printf(“%s”, gets(line)) ;

Using Putchar and Puts Functions [2½ marks]


Like getchar, C supports another character handling function putchar to
output the values of character variables. It take the following form:
char ch = ‘A’;
purchar (ch);

The function putchar requires one parameter. This statement is equivalent to:
printf(“%c”, ch);

We have used putchar function to write characters to the screen. We can use
this function repeatedly to output a string of characters stored in an array
using a loop.
Example: char name[6] = “PARIS”
for (i=0, i<5; i++)
putchar(name[i];
putchar(‘\n’) ;

Another and more convenient way of printing string values is to use the
function puts declared in the header file <stdio.h>. This is a one parameter
function and invoked as under:
puts ( str );

where str is a string variable containing a string value. This prints the value
of the string variable str and then moves the cursor to the beginning of the
next line on the screen. For example, the program segment
char line [80] ;
gets (line) ;
puts (line) ;

reads a line of text from the keyboard and displays it on the screen. Note
that the syntax is very simple compared to using the scanf and printf
statements.

Q.5(d) Write a program to take n from user and print first n terms of [5]
Fibonacci series.
(A) #include<stdio.h> [5 marks]
#include<conio.h>
void main()

19
Vidyalankar : F.E.  SPA

{
int a = 0,b = 1, c, i, n;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
printf("Fibonacci Series\nO\nl\n");
for(i=l;i<=n-2;i++)
{
c=a+b;
printf("%d\n",c) ;
a=b;
b=c;
}
getch();
}

Q.6(a) Define recursion? Write a recursive program to find “X raised to [8]


Y” using recursion.
(A) Definition [1 mark]
Recursion is a process where Function OR Subroutine is called itself.

Overview : [3 marks]
Many times the length of program matters for the execution. Sometimes
code gets expand which becomes tedious to read and understand. Therefore
it is necessary that the code should be compact. ’Recursion’ is a concept
where we call same function inside function itself. Recursion is heavily used
whenthere is need of repeated execution of some task. Though recursion is
advantageous, it proves inefficient with respect to time and space
complexity. Calling the same function again and again leads to increase in
execution time. For every next pass, the details of previous pass are pushed
on to the stack (data structure) which increases the space requirements of a
program. Recursion may seem strange and complicated at first glance, but it
is often the most direct way to code an algorithm, and once you are familiar
with recursion, the clearest way of doing so.

Why recursion? : [4 marks]


Most of the time Recursion logic is very difficult for us to construct. Especially
we need to focus on 2 important pointswhile writing programs on recursion:
- The task which has a repetitive nature in the program. Such task need not
to write again and again. We can call such task recursively.

20
Prelim Question Paper Solution

- Execution of task should stop somewhere. Therefore we must have proper


terminating condition that will stop the repetitive execution of task.
Logically, construction of “Termination
1. /*
2. * C Program to find Power of a Number using Recursion
3. */
4. #include <stdio.h>
5.
6. long power (int, int);
7.
8. int main()
9. {
10. int pow, num;
11. long result;
12.
13. printf("Enter a number: ");
14. scanf("%d", &num);
15. printf("Enter it's power: ");
16. scanf("%d", &pow);
17. result = power(num, pow);
18. printf("%d^%d is %ld", num, pow, result);
19. return 0;
20. }
21.
22. long power (int num, int pow)
23. {
24. if (pow)
25. {
26. return (num * power(num, pow - 1));
27. }
28. return 1;
29. }

Q.6(b) Write a C program to ckeck wheter entered string is palindrome or [8]


not. (do not use string header file)
(A) #include<stdio.h> [8 marks]
#include<conio.h>
void main()
{
int n=0, i;
char a[100], rev[100];

21
Vidyalankar : F.E.  SPA

clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
for (i=0;i<=(n1);i++)
{
rev [n  i  1] = a[i];
}
for(i=0;i<=n-l;i++)
{
if(a[i]!=rev[i]) break;
}
if(i==n)
printf("The string is palindrome.");
else
printf("The string is not palindrome.");
getch();
}

Q.6(c) What are the various factors need to be considered while designing [4]
problem definition of a particular program?
(A) The first step in software development is to specify the problem to be solved.
This may be more demanding than it sounds. Software development
professionals can tell plenty of stories of how they demonstrated a program, or
an early prototype, and found that the client’s first comment was to request a
change or addition to the program’s original purpose. [1 mark]

The specification stage is also called analysis. The skills required have more
to do with understanding a business or organization than using a
programming language. The more thorough the analysis and specification are,
the less effort will be needed at a later stage to re-specify the task that
the program is to perform. Professionals who specialize in this work are
called systems analysts. [1 mark]

Key components of a program’s specification are its input, its output, and the
relationship between them. Software developers have long prepared sample
input/output as part of program specification. Most of our programming
exercises include sample I/O. [1 mark]

22
Prelim Question Paper Solution

As software has become more complex, the specification may include entire
screens, such as forms with which to get input or windows to display results.

Note that the specification phase does not address the critical question of
how to arrive at the desired output from a given set of user input. That
issue is left for the design phase. [1 mark]



23

You might also like