CO34563 Assignment 1
CO34563 Assignment 1
1. For the following problems, write pseudocode solutions. After that implement the algorithms
in any programming language and draw a graph beetween size of input and time taken of yours
algorithm. and also mention the growth rate of the algorithms.
Problem 1: Write a C program to take Input a 4-digit integer through keyboard, print the sum
of product of even position digits and odd position digits. For example, if the integer is 2345,
then the sum of the product will be 2*4+3*5=23.
Problem 2: Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3,
5, 8, 13, 21 ... By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent
number is the sum of the previous two numbers. Write a program to compute nth number in
this series for given input n.
Problem 3: Print a triangle of ’*’s of height ’r’ rows. Now modify your program to print it
upside down of given size ’r’, where r represents the no. of rows in the triangle.
Problem 4: Implement a C program that finds all the numbers between 01 and 1000 such that
the number itself minus the number reversed is equal to the sum of its digits. For example:
54 is such a number because 54-45 (which is 9) is same as the sum of its digits (5+4 = 9).
Problem 5: User provides two unsorted 1-D arrays of sizes m and n, write a C program that
merges the two into another 1-D array of size m + n such that this new array becomes sorted.
The sizes m and n and values in the arrays are also provided by user.
Problem 6: Write a C program that take 2 integer sets A[] and b[] as input and prints results
of following set operations:
(a) A union B (Write function setunion())
(b) A intersection B (Write function setintersection())
(c) A-B and B-A (Write function setdifference())
Problem 7: Write a function (function name: distance) to compute the distance between two
points and use it to develop another function (function name: area) that will compute the
area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these to
develop a function functions (function name: tritest) which returns a value 1 if the point (x,
y) is inside the triangle ABC, otherwise a value 0 for N points, where N points are entered
through the keyboard.
Problem 8: An array of integers is said to be a straight-K, if it contains K elements that are
K consecutive numbers. For example, the array 6, 1, 9, 5, 7, 15, 8 is a straight because it
contains 5, 6, 7, 8, and 9 for K=5. Write a program to finds the maximum value of K for the
given number of integers.
Problem 9: Write a program to find the factorial of the given number (1 ≤ n ≤ 10,000,000,000).
Problem 10: An array of integers is said to be a straight-K, if it contains K elements that are
K consecutive numbers. For example, the array 6, 1, 9, 5, 7, 15, 8 is a straight because it
contains 5, 6, 7, 8, and 9 for K=5. Write a program to finds the maximum value of K for the
given number of integers.
Problem 11: Write a C program to print the all possible circular rotation of elements of array.
For example if input array=3,5,2,6,1 then output=52613, 26135, 61352, 13526 and 35261.
1
Problem 12: A powerful number is a positive integer m such that for every prime number p
dividing m, p2 also divides m. Equivalently, a powerful number is the product of a square and
a cube, that is, a number m of the form m = a2b3, where a and b are positive integers. For
example the 21600 decimal number a powerful number because 21600= 25x33x52=102x63.
Write a function to check whether a given number is a powerful number or not.
Problem 13: Consider a positive integer n of type int. The next higher permutation of n is the
smallest integer greater than n which is formed by permuting the digits of n. For example,
the next higher permutation for n=1209861 is 1210689, the next higher permutation for
n=1421731 is 1423117. Note that next higher permutation may not exist for every number.
Write a program to find the next higher permutation of the given number.
Problem 14: Write a program to display the given number after eliminating the duplicate digits
from it. For example: for a given number 245265 display the number 2456.
Problem 15: 2. A number n is called left trunckable prime if n and all numbers obtained
by successively removing its left most digits are prime.(Similarly right truncatable prime is
defined) Ex 313 is a left truncatable prime – 313 is prime, 13 is prime, 3 is prime. 313 is also
right truncatable – 313 is prime, 31 is prime, 3 is prime. Write a C program using prime()
function, which takes a number n as input and then tells whether it is left truncatable, right
truncatable or both.
Problem 16: 5. Write a program to multiply two arrays of integers. For example a[]=1,2,3,4,5
and b[]=9,8,7,6 then output is c[]=1,2,1,9,1,9,2,2,0.
Problem 17: Write a function void partition (int a[], int left, int right)which selects the first
element in the array as pivot, rearranges the array elements, such that the pivot element goes
to the new position middle between left and right, so that all left side elements are less then
middle element and all right side elements are grater then middle element.
Problem 18: A number is circular prime if it is prime and all its cyclic rotations are also prime.
e.g. The number 1193 is a circular prime number because it is prime and all its cyclic
rotations 1931, 9311, 3119 are prime. Write a program that takes an integer n as input and
prints whether it is circular prime or not. Your program has to work for all values of n which
can be stored in data type int.
Problem 19: Write a program to rotate a given square matrix of size NxN by 45 degrees clock-
wise. Print both the original and rotated matrices. For Example: if N=3 Original Matrix:
123
456
789
Rotated Matrix:
1
4 2
7 5 3
8 6
9
Problem 20: Write a program to rotate a given square matrix of size NxN by 45 degrees anti-
clockwise. Print both the original and rotated matrices. For Example: if N=3 Original
Matrix:
123
456
789
Rotated Matrix:
3
2 6
1 5 9
4 8
7
Problem 21: For given integer numbers a and b, write a program to print all palindromic prime
numbers that are smaller than or equal to b and greater than or equal to a.
2
Problem 22: A special number is a number in which the sum of the factorial of each digit is
equal to the number itself. For example: 145 = 1! + 4! + 5! = 1 + 24 + 120 . Write a
program to check, if a given number is a special number or not.