Functions and Recursions – Algebra – Problem #4
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
Write a C program to check if the two numbers form a Zazzy pair or not. Two numbers are said to be Zazzy pair, if they satisfy the following criteria as given below: (a) Both the numbers should have the same number of digits and their length (number of digits) is even. (b) The number formed from the digits of the first half of the first number (MSB side) is equal to the reverse of the number formed from the second half of the digits of the second number (LSB side). You must use two functions for solving this question, (a) int checkDigit(int,int) - to check (i) whether two numbers have the same number of digits or not and (ii) Whether their length is even or not (b) int isZazzy(int,int) - to check whether the two numbers form a zazzy pair or not. [30] Example 1: Input: Number 1 = 123456 Number 2 = 675321 Output: Zazzy pair [Reason: The number formed from the digits of the first half of the first number, i.e ‘123’ is equal to the reverse of the number formed from the digits of the last half of the second number, i.e ‘321’] Example 2: Input: Number 1= 123456 Number 2= 321654 Output: Not a Zazzy pair Example 3: Input: Number 1 = 12345 Number 2 = 54321 Output: Not a Zazzy pair Hint: To provide you with an additional help as to how to frame and formulate your code using functions for this question, consider the code snippet below. Make careful observations as to how you call the appropriate functions at the appropriate places and handle the return values, if any. 1 #include<stdio.h> 2 3 int checkDigit(int,int); //Function declaration 4 int isZazzy(int,int); //Function declaration 5 6 int main() 7 { //First, input the two numbers, say the input ←- are stored in variables n and m respectively. 8 //Next proceed to compute for Zazzy pair if and ←- only if the two numbers satisfy conditions a ←- and b of the question. 9 10 int flag = 0; 11 12 if(checkDigit(n,m) == 1) 13 { //Now check whether the two numbers are Zazzy or ←- not 14 if(isZazzy(n,m) == 1) 15 { 16 flag = 1; 17 } 18 else 19 { 20 flag = 0; 21 } 22 } 23 24 else 25 { 26 flag = 0; 27 } //Now if flag == 1, then numbers are Zazzy, so ←- print accordingly, else print not Zazzy. 28 } //end of main() 29 30 int checkDigit(int n, int m) 31 { //Returns 1 if two numbers passed as parameters ←- are of equal length and even number of digits, 0 ←- otherwise 32 } 33 34 int isZazzy(int n, int m) 35 { //Returns 1 if n and m passed as parameters form ←- a Zazzy pair, 0 otherwise 36 }