Practice3
Practice3
1) What is recursion? How does a recursive function work? What is the advantage
and disadvantage of recursion?
2) A 5-digit positive integer is entered through the keyboard, write -
a) A recursive function and
b) A non recursive function to calculate the sum of the digits.
3) Answer the following-
a) A positive integer is entered through the keyboard, write a program to
obtain the prime factors of the number.
b) Modify the function suitably to obtain the prime factors recursively.
4) Complete the following -
a) int power_raiser(int base, int power)
{
int ans;
if (power == ______)
ans = ______;
else
ans = ______ * ____________;
return (ans);
}
b) What is the output of the following program? What does function
strange compute when called with a positive integer?
c) #include <stdio.h>
int strange(int n);
int
main(void)
{
printf("%d\n", strange(7));
}
int strange(int n)
{
int ans;
if (n == 1)
ans = 0;
else
ans = 1 + strange(n / 2);
return (ans);
}
5) Answer the following -
a) Write a C program with a function named fibonacci that computes the
n- th fibonacci number.
b) Modify the function to compute the n-th fibonacci number recursively.
c) Explain what would happen if the terminating condition for function
fibonacci were just (n == 1).
6) Write a C program with a recursive function to extract capital letters from a
string.
7) Write a C program with a recursive function to take n words as input and print
them in reverse order on separate lines
8) Write a recursive functions for the following problems
a) Calculate GCD of two numbers.
b) Calculate LCM of two numbers.
9) What will be the output of the following programs:
a) # include <stdio.h>
int main( )
{
float a = 13.5 ;
float *b, *c ;
b = &a ; /* suppose address of a is 1006 */
c = b ;
printf ( "%u %u %u\n", &a, b, c ) ;
printf ( "%f %f %f %f %f\n", a, *(&a), *&a, *b, *c ) ;
return 0 ;
}
b) # include <stdio.h>
void function ( int * ) ;
int main( )
{
int i = 35, *z ;
z = function ( &i ) ;
printf ( "%d\n", z ) ;
return 0 ;
}
void function ( int *m )
{
return ( m + 2 ) ;
}
Now, open a text file named Bank_info and write all the customers’ name and
their balance into them.
21) Write a C program to read a file and display its contents along with line
numbers before each line.
22) In C file handling Is it necessary that a file created in text mode must
always be opened in text mode for subsequent operations? What is the
purpose of the library function fflush( )?
23) Answer the following:
a) While using the following statements in C file handling-
fp = fopen ( "myfile.txt", "r" ) ;
what happens if ‘myfile.txt’ does not exist on the disk and if ‘myfile.txt’
exists on the disk.
b) While using following the statement,
fp = fopen ( "myfile.c", "wb" ) ;
what happens if ‘myfile.c’ does not exist on the disk and if ‘myfile.c’
exists on the disk.
24) Write a C program to print the elements of the following 2D array using a
pointer.
{11,22,33,44},
{55,66,77,88},
{11,66,77,44}
25) Point out the errors [if any] from the following problem:
a) # include <stdio.h>
int main( )
{
int twod[ ][ ] = {
2, 4,
6, 8
} ;
printf ( "%d\n", twod ) ;
return 0 ;
}
b) # include <stdio.h>
int main( )
{
int three[ 3 ][ ] = {
2, 4, 3,
6, 8, 2,
2, 3 ,1
} ;
printf ( "%d\n", three[ 1 ][ 1 ] ) ;
return 0 ;
}