Problems on Basic Algorithms
For all the following problems you should check your algorithm for a range of known (simple if possible) inputs which contain all possibilities which your algorithm covers. For the solutions look here:
Simple Decision-Making (i.e. Branching)
Q1) Write an algorithm to check whether a given number is even or odd.
Q2) Write an algorithm to check whether a given number (say, a) is divisible by another given number (say b).
Q3) Calculate the sum of squares and the square of the sum of numbers starting from 1 to N, where N is specified by the user. Hence comment as to which is greater.
Q4) Write an algorithm to calculate the n-th term of the following series:, where n is given by the user.
Q5) Write an algorithm to calculate the j-th term of the following series:, where j is given by the user. Hence or otherwise calculate the sum of all the terms up to the j-th.
Q6) Write an algorithm to calculate the n-th term of the following series:, where n is given by the user. Hence and otherwise calculate the sum of terms starting from the X-th term to the Y-th term, both of which are inputted by the user.
Q7) Write an algorithm to exchange the values of two variables
a & b. i.e. if input is a = 2 & b = 3, then the output should be a = 3 & b = 2. You will find that you have to use a dummy variable, i.e. a third variable to actually conduct the interchange.
Q8) Write a program to interchange (clockwise) the values of a series of n numbers. Say if n = 3 then create 4 variables n1, n2, n3 & dummy to switch the values of n1ΰn2, n2ΰn3, n3ΰn1 using the dummy. (You may try to write an algorithm without using dummies, here) Also try to generalize your algorithm for a larger n.
Limited loops for manipulating numbers
Q9) How do you determine the number of digits for a given number, algorithmically? (e.g. If you are given as input - 5423, it contains 4 digits.) You may want to limit the inputs to a definite number of digits, i.e. you may want to allow only a maximum of up to 6 digits as input. This will help you avoid the use of arrays.
Q10) How do you reverse the digits in a given number, without using arrays or strings? Again you may wish to restrict the size of the number inputted.
Q11) How do you count the number of digits in a given number, without using strings or arrays?
Generation / Verification of complex Series
Q12) Calculate the factorial of a given number.
Q13) Calculate the n-th term of the Fibonacci sequence. (Exclude the generators 0 & 1)
Q14) Calculate the Sine of a given angle (the angle may be taken in degrees) using the series expansion:
Q15) Calculate the Cosine of a given angle (the angle may be taken in degrees) using the series expansion:
Number Systems Manipulations
Q16) You are given a number in the decimal number system. Write an algorithm to convert the number into binary. i.e. (123)10 ΰ (1111011)2.
Q17) Given a number in the binary system convert it into decimal. (1111)2 ΰ (15)10
Q18) Given a number in a number system with base b convert to decimal. b is inputted by user.
Q19) Given a number in decimal convert to a number system with base b. b is inputted by user.