Assignment 5
Assignment 5
Assignment 5
---------------------------------------------------------------------------------------------------------------------
(25 X 4 =100)
1) In number theory, a perfect number is a positive integer, where sum of its positive divisors
(excluding the number itself) is equal to the number itself. For example, 28 is a perfect
number where sum of its 1+2+4+7+14 = 28.
Write a program in C consisting two user defined functions to print all perfect numbers in a
given range provided by the user through keyboard. Through main(), the user-defined functions
will be called for performing the task. The user-defined functions are given below:
b) Write a function ListPerfectNumbers(int, int) that prints all the perfect numbers in the
range provided by the user.
Example :
Input the range : 100 10000
The perfect numbers between 100 and 10000 are : 496 8128
2) Write a C program to perform the following:
a) Write a function named removeFirst that takes character string and a single character as
two arguments, and removes the first occurrence of the given character from the string.
Print the function name and the updated string.
b) Write another function named removeLast that takes character string and a single
character as two arguments, and removes the last occurrence of the given character from
the string. Print the function name and the updated string.
c) Write a function named compareString that takes two strings as two input arguments,
compares the two strings, and returns 1 if they are same, else returns 0.
d) From the main () capture the string and a single character from the user. Call the
functions removeFirst and removeLast from the main and pass the same string and the
character as inputs to both functions. Compare the resultant strings from functions
removeFirst and removeLast using your compareString function (DO NOT use string
library functions). If the strings are same, print “Equal.”, else print “Not equal.”. Please
note, all the functions here are case-sensitive.
Example 1:
Enter a string: apple
Enter a character: a
removeFirst: pple
removeLast: pple
Resultant strings of removeFirst and removeLast are Equal
Example 2:
Enter a string: Avadakedavra
Enter a character: a
removeFirst: Avdakedavra
removeLast: Avadakedavr
Resultant strings of removeFirst and removeLast are Not equal
3) Read two positive integers n and b from the terminal (with n>=0 and b>1). Here n is any
positive number and b is the base. Write a C program using recursive function with
appropriate arguments to express n in base b. For example, the decimal expansion of 345 in
base 10 is (3,4,5)_10 = 3 x 10^2 + 4 x 10^1+ 5 x 10^0. Similarly, the expansion of 345 in
base 16 is (1,5,9)_16 = 1 x 16^2 + 5 x 16^1 + 9x 16^0.
Example 1:
Enter any positive integer : 123
Enter the base value : 10
123 = (1,2,3)_10
Example 2:
Enter any positive integer : 6789
Enter the base value : 16
6789 = (1,10,8,5)_16
Example 3:
Enter any positive integer : 987654321
Enter the base value : 123
987654321 = (4,38,92,23,114)_123
a) Write a C function read_string(char []) to read a string from the user through keyboard.
Use the above function to read two strings s1 and s2 from user. The two strings may have
blank spaces. However, assume that the strings do not have two successive blank spaces.
b) Write a C recursive function with appropriate arguments to merge the two strings (s1 and
s2) into a new string (s3), where s3 contains the words of s1 and s2 in interleaved
fashion, as shown in the examples.
c) Print the interleaved string s3 using the user-defined function with appropriate arguments.
Example 1:
Enter s1: I study at IIT KGP
Enter s2: The sky is cloudy
Interleaved String s3 = I The study sky at is IIT cloudy KGP
Example 2:
Enter s1: abc 1234 fgrt
Enter s2: asdf ghjk 1234 5678 89
Interleaved String s3 = abc asdf 1234 ghjk fgrt 1234 5678 89