Problem 1

Write a program in C to print all perfect numbers in a range given by the user. In this program you need to use two C functions.
The first function will check whether the given number is perfect or not, and return 1 (in case of perfect number) or 0 (otherwise).
The second function should print all the perfect numbers in the range provided by the user, by using the first function. The user has to specify the range of numbers using two numbers (through keyboard), say p and q, where p and q are lower and upper bounds of the range.
Through the main function, the user has to call the second function by passing the range as arguments to print all perfect numbers within the range.

Note: A number is said to be perfect, if the sum of all the divisors of a number equal to the number itself.

In this problem, consider the numbers as positive integers.

Eg (i) 6 = 1,2,3 (1+2+3 = 6); (ii) 28 = 1,2,4,7,14 (1+2+4+7+14= 28)

Solution
Problem 2

Write a C program that accepts the input string (alphanumeric characters), and display the output string by removing all the occurrences of a given character.
For carrying out this task, you need to write two C functions. The first C function is used forinputting the string with recursive calls. The string entry will be controlled/terminated by enter(\n) key.
The second function will take an array which contains the input string, the specific character and an empty string as input. The function should remove all the occurrences of a specific character in the input string, and the remaining string is stored in the other array. From the main(), the user has to call these two functions and then both input and output (after removing the specific character) strings to be printed in the main().
EX-1:
Enter the input string: apple
Specific Character: p
Input : apple
Output : ale
EX-2:
Enter the input string: apple
Specific Character: x
Input : apple
Output : apple

Solution
Problem 3

Write a C program to generate the strings with alphanumeric characters using the following constraints:

(i) Consider an integer array loaded by the user from main().
(ii) Input two alphanumeric characters through the keyboard.
(iii) Write a C function to generate the strings with the following features:
(a) Number of strings equal to number of elements of an input array.
(b) The lengths of the strings are restricted by the values of the array elements.
(c) The characters within the string should have the following properties: If the length of the string is even, the first half of the string is repeated with 1st character and 2nd half of the string is repeated 2nd character. Otherwise, the 1st character has to repeat one time more than the 2nd character.
You need to call this C function from the main() and pass the desired four parameters. Example:
Input array length N = 4
Input Array Elements = p[4] = {2,9,3,10}.
Enter two characters = ‘a’, ‘b’
Output: ab, aaaaabbbb, aab, aaaaabbbbb

Solution
Problem 4

Write a C program with the functions mentioned below to perform the following:
(i) Take two positive integers (i1,i2) less than 50,000 as an input, and find their binary representations and store it in arrays (b1[], b2[]) using integer to binary conversion function void int_to_bin(int, int[]). For converting each integer you need to call the function separately.
(ii) Perform XOR operation (x[] = b1[] xor b2[]) using the function void xor(int [], int [], int[]).
(iii) Convert x[] into integer using the function int bin_to_int(int []). Call these functions through main() to perform the sequence of above mentioned operations.
Ex-1:
Input: 5 5
b1 = b2 = 0000000000000101
X = 0000000000000000
Integer value of x = 0.
Ex-2:
Input:
Input : 456, 321
b1 = 0000000111001000b2 = 0000000101000001
X = 0000000010001001
Integer value of x = 137

Solution
Problem 5

Write a C program to compute nCr for a given n and r.
You take the input n and r from the user in the main function. Then invoke the function 'nCr' for the necessary computation. Finally print the result of the computation in the main function.
You are expected to write a nice modular program (say, compute factorial inside a function....recursive or non-recursive...up to you..).

Solution
Problem 6

Write a recursive program to do the following. With a integer n (l to 25) as input, compute and print the values of m choose r, for m to n, r 0 to m, using the following recurrence:
C(m,r) C(m-l,r) + C(m-l ,r-l).

You should use one (and only one) ID integer array of size 26 for this.
Example (n 5):
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Solution
Problem 7

Write a function check_prime(.), which takes an integer x and returns 1 if it is prime else returns 0.
Write a main function, which reads a number N, and returns its sum of all prime factors. Note that 1 is not considered as a prime factor.

For example, for the number 8, its sum of prime factors:
2+2+2=6.
Provide results for the following set of numbers:
37, 237, 423, 321, 2054

Solution
Problem 8

Write a function named SumDigit(.), which takes an integer x as input and returns its sum of digits if it is positive, otherwise it returns 0.

We define a class number of an integer by performing SumDigit(.) operations on it on succession till it gets a single digit, which is its class number.

For example,
class number of 123456 is 3 as it follows:
SumDigit(123456)=21
SumDigit(21)=3
Hence, Class number of 123456 is 3.

Write a function ClassNumber(x) which computes the class number of integer x.

Write a main function, which reads two numbers and prints an appropriate message whether their class
numbers are same or not.Provide results for the following sets of inputs:
(234512, 32678)
(1112, 5054)
(-34567,0)
(28912,12892)

Solution
Problem 9

Write a function named test_fn(.) such that given x, and parameters a, b and c as inputs it computes the following:

a.x2.sin(x)+b.x.ln(x)+c.cosh(x)

Write a main function which computes the definite integral of the above function (given its parameters) in the interval [m,n] (m< n) in real axis by using the following computational method.

For a function f(x), the integration of f(x) from x=m
to x=n (m< n), is given by as follows:

h=(n-m)/N (for sufficiently large N).
Integral value=h(f(m)+2f(m+h)+2f(m+2h)+….+2f(m+(N-1).h)+f(n))

Provide results for the following set of inputs:
(i) a=-5, b=0, c=20, m=23, n=80
(ii) a=5, b=10, c=2, m=5, n=10
(iii) a=5, b=-10, c=4.5, m=7.8, n=20.5

Solution
Problem 10

Write a program with two functions using appropriate parameter passing and return value as described below:
(i) recursive_fn (), which computes f(n) recursively as defined below:

f(n) = 0, for n< 0
= 1, for n=0
= f(n-2) -2* f(n-1) + f(n-3), otherwise
Note that n is an integer argument and f(n) returns an integer value.

(ii) iterative_fn(.): It implements the same function f(n) without using a recursive call. Using above two functions separately compute the following expression Sum separately:

Sum = f(0)+f(1)+f(2)+ ….. for M terms (M to be read in the beginning)
Finally the program prints the following:
(i) Sum computed using recursion, and the number of calls to recursive_fn(.), made while computing it.
(ii) Sum computed using iteration, and the number of calls to iterative_fn(.), made while computing it.
Show outputs for M=5, 10, 15 and 20.

Solution
Problem 11

Write a function computeStatistics(.) with appropriate parameter passing and return values, which takes an array of real numbers, and return its mean, and standard deviation, using pointer argument variables.
Write a main program, which reads two lists of real numbers, say list1 and list2, with the numbers of elements N1 (to be read before reading list1) and N2 (to be read before reading list2), respectively.
The program computes their means and standard deviations using the function computeStatistics(.). Let m1, and m2 be means and, s1 and s2 be standard deviations of list1 and list2, respectively. The program also computes the following statistics T using them:
T= (m1-m2)/ √ (s1^2/N1+s2^2/N2)
The program prints the following:
(i) Number of elements, mean and standard deviation of list1.
(ii) Number of elements, mean and standard deviation of list2.
(iii) The statistics T.

Show your outputs with the following input dataset:

(i) list1: N1: 5
46.5, 78.9, -3.5, 4.9, 12.3
list2: N2: 10
26.4, 88.1, -34.7, -40.2, 222.3, 56.4, 12.4, 43.2, -35.7, -88.8

(ii) list1: N1: 10
36.2, 68.5, 331.3, -44.4, -220.4, -66.2, -120.8, 430.3, -432.9, -45.7
list2: N2: 15
16.4, 18.1, -14.7, -43.2, 22.3, -156.2, 125.3, -143.2, -55.7, -78.8, 26.4, 18.2, -4.5, 7.9, 27.3

Solution
Problem 12

Write functions to return the area of a circle whose radius is supplied as an argument (circle_area()).
Write a main program to scan the input of the function and print the output of the function for that input.

Solution
Problem 13

Write a function int digitsum(int x) to compute the sum of digits of x. Example: digitsum(1572) is 1 5. Write another function int eqdigitsum(int x, int y) to check if inputs x and y has equal digit sums.
The function returns 1, if x and y have same digit sum, and 0, otherwise.
The second function should call the first function.
Write a main function that does the following:

i. Read in an integer n (< 100). Read n integers and store them in an array.
ii. In a nested loop, check all pair of array elements if they have equal digit sum using the function defined above. Print the indices if they have equal digit sum as

you check a pair (one pair in a line without repeating pairs)
Test Example: For the input array A = {1 572, 2113, 2481,
2032}, the equal digit sum pairs are

Solution
Problem 14

Write a program to read a character string and store it in an array.
Suppose the string stores a password.
A strong password should have at least 8 characters.
Also, it should have at least 1 numeral (0-9) and at least 1 upper case alphabet (A-Z).

Print whether the input string is a strong password.

Solution
Problem 15

Write a C function named int OddDigitCount(int n)



 The function takes as parameter one integer n, and returns the number of odd digits in n

 Note that for any integer k, k%10 gives the last digit, and k/10 gives the number without the last digit

 Example, if k = 746, then k%10 = 6 (= last digit), and k/10 = 74 (= 746 without the last digit) in C (and not 74.6 as int/int division truncates in C). You can now do 74%10 = 4 to get the next digit and so on. Extract the digits in a while loop and check them, keeping count of the odd digits found.

Work out by hand with an actual number first to check till how far the loop should go.

 For example, if the parameter passed (n) is 1323, the function returns 3 as there are 3 odd digits, 1, 3, and 3 in 1323. Similarly, if the parameter passed (n) is 46, the function should return 0 as there are no odd digits in 46

 Write a main function that

 Reads in an integer n (assume n < 100)

 Reads in n integers in an array A (in a loop)

 Prints the n integers in the array A (in a separate loop from read)

 Prints the number of odd digits in each number in A and also, the number in A with the maximum number of odd digits

 Use another loop that calls the function OddDigitCount() with each element of the array as parameter, and prints the return value of the function for each element

 Remember the maximum count found while looping, print it after coming out of the loop

 For example, if you read in 34, 1323, 50, 71 in array A, your output should look like (number of spaces can vary):

34 1323 50 71

1 3 1 2

The number with maximum no. of odd digits is 1323

Solution
Problem 16

Write C function

int CheckStr(char S1[ ], char S2[ ])

that takes as parameter two strings S1 and S2 of characters (null terminated, i.e., terminated by ‘\0’), and returns 1 if there exists any combination of two characters in S1 (not necessarily consecutive) such that the sum of their ascii codes is the same as the maximum ascii code of any character in S2.

 Example:

 Suppose S1 = “023AZC” and S2 = “7pq”. Then maximum ascii code of any character in S2 is 113 (the ascii code of ‘q’). Then the function should return 1 as there exists a combination of two characters in S1, ‘0’ (ascii code 48) and ‘A’ (ascii code 65) such that their sum is = 48 + 65 = 113

 Suppose now S1 is “02BZC”. S2 is the same. Then the function should return 0 as there is no such combination of two characters in S1 whose ascii code sum is = 113



 To write CheckStr(), do the following in this order:

 Find the lengths of S1 and S2 and store in two variables. Print the lengths of S1 and S2 with a message

 Find the character with the maximum ascii code value in S2. Print both the character and its ascii code with a message

 Look at all possible pairs of characters in S1 (two nested loops). For each pair considered inside the loop:

 Sum the ascii codes of the two characters, and print the two characters and their ascii code sum

 Then compare the sum with the max ascii code value of S2 found earlier

 If found, return 1 immediately

 If not found, go to the next pair and check

 Return 0 at end of the function after coming out of al loops if no pair is found Write a main function that

 Reads two strings X1 and X2 from the keyboard using %s in scanf. Assume that the strings will be of less than 100 characters each

 Calls the function CheckStr() with X1 and X2 as parameters, and prints a nice message based on the return value

 It is ok to print just whether there exists such a combination, the actual combination need not be printed if the function returns 1

 Do NOT use the ascii code values directly (like 48, 49, 65 etc.) anywhere in your program, 50% penalty if used

 Your don’t need to, as characters are stored inside by their integer ascii codes, and can be compared or added directly (like ‘a’ + ‘c’, if (ch > ‘a’) etc.

Solution
Problem 17

Write a program to take input a series of characters as a string. For each character in the string find out the reverse of the ASCII value of that character and determine whether the reverse value itself forms a valid character or not. Valid characters include special symbols, space, digits and alphabets (both upper and lower case). Corresponding to each character in the original string, print the reverse value character, if it exists, otherwise print ”NA”.



(a) Use a function int reverse(int) to reverse a particular number.

(b) Use another function int isValid(int), that returns ’1’ if the number in its parameter is a valid printable character, otherwise ’0’.



For example, take A as input. ASCII value of ’A’ is 65 and reverse of the ASCII value is 56 which represents number ’8’. Therefore, the character corresponding to the reverse ASCII value is valid.

Solution
Problem 18

Write a recursive function to compute x n based on the following definition using suitable base cases.



x^n ={

x^n/2 ∗ x^x/2

if n is even



x ∗ x^n/2 ∗ x^x/2

if n is odd

}

Solution
Problem 19

Write a program to take input your date of birth as dd-mm-yyyy (all integers) and print the following:

(a) Print the binary equivalent of the day ’dd’.

(b) Print the octal equivalent of the month ’mm’.

(c) Print the hexadecimal equivalent of the year ’yyyy’.

(d) Print the number of days between the day dd and January 1 of that year (consider the case of leap year as well). You have to use separate functions for integer to binary conversion, integer to octal conversion, integer to hexadecimal conversion and to calculate the difference in days that you should call from main().

Solution
Problem 20

User gives a positive integer, n (>2), as input.

Your program should print all the prime numbers up to n.

For this you should write a function isPrime(int k) that will check whether or not a number k is prime, and return 1 or 0 accordingly to main().

This function should be called inside a for loop in main() for k=3 to n.

You can use sqrt function of math.h.



Examples:



Enter a number (n>2): 6

35

Total number of primes = 3



Enter a number (n>2): 20

357 11 13 17 19

Total number of primes = 8



Enter a number (n>2): 100

357 11 13 17 19 23 29 31 37 41 43 47

53 59 61 67 71 73 79 83 89 97

Total number of primes = 25



Enter a number (n>2): 123

357 11 13 17 19 23 29 31 37 41 43 47

53 59 61 67 71 73 79 83 89 97 101 103 107 109 113

Total number of primes = 30

Solution
Problem 21

Given three lines of the form ax+by=c, your program has to compute their points of intersection and hence the area of the triangle described by them. Take the co-efficients (a, b, c) of each line in main().

For this, write a function that takes as input (as parameter) the coefficients (a,b,c) of two lines and computes their intersection point if they are not mutually parallel. Call this function three times from main() and print required results (intersection points, side length, area, messages etc) in main() as shown in examples. You may use an array to pass the intersection points from function to main().

You can use sqrt function of math.h.



Examples:



Enter the coefficients (a, b, c) of three lines:

Line 1: 100

Line 2:01 0

Line 3: 111

Point of intersection between Ll and L2 = (0.000000, -0.000000).

Point of intersection between L2 and L3 = (1.000000, 0.000000).

Point of intersection between L3 and Ll

= (-0.000000, 1.000000).

Side lengths = 1.000000, 1.414214, 1 .OOOOOO.

Area = 0.500000



Enter the coefficients (a, b, c) of three lines:

Line 1: 100

Line 2: 101

Line 3: 111

Lines 1 and 2 are parallel; so no point of intersection.



Enter the coefficients (a, b, c) of three lines:

Line 1: 1 -1 1

Line 2: 1 -2-2

Line 12

Point of intersection between Ll and L2 = (4.000000, 3.000000).

Point of intersection between L2 and L3 = (0.666667, 1.333333).

Point of intersection between L3 and Ll

= (l .500000, 0.500000).

Side lengths = 3.726780, 1.178511, 3.535534.

Area = 2.083334





Enter the coefficients (a, b, c) of three lines:

Line 1: 1 -1 5

Line 2: 1 2-10

Line 1 10

Point of intersection between Ll and

12 = (0.000000, -5.000000).

Point of intersection between L2 and

13 = (30.000000, -20.000000).

Point of intersection between L3 and

Ll = (7.500000, 2.500000).

Side lengths = 33.541019, 31.819805, 10.606602.

Area = 168.749924

Solution