The Programming Project

Thursday, April 17, 2014

Binomial Coefficient using recursion in Python and C language





Post by Maths.


C Code
#include<stdio.h>
long int Binomial(long int, long int);
int main()
    {
    long int n,k;
    printf("\n Enter the value of n and k, n is greater than k:");
    scanf("%ld %ld",&n,&k);
    while( k < 0 || n < 1 || k > n) {
        printf("\n Wrong input, enter again:");
        scanf("%ld %ld",&n,&k);
        }
    printf("\n Value of %ldC%ld: %ld \n", n,k,Binomial(n,k));
    return 0;
    }
long int Binomial(long int n, long int k)
    {
    return (k <= 1 ? (k == 0 ? 1 : n) : (n*Binomial(n-1,k-1))/k);
    } 


Python Code
 
#File: binomial.py
#A simple program to calculate nCk using recursion
def main():
    print "Binomial Coefficient"
    n = input("Enter the value of n:")
    k = input("Enter the value of k:")
    x,y= n,k
    def binomial(n,k):
        if k < 2:
            if k == 1:
                return n
            else:
                return 1
        else:
            return (n*binomial(n-1,k-1))/k
   
    print "Value of nCk is",binomial(x,y)
   
main()


OUTPUT:

administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import binomial
Binomial Coefficient
Enter the value of n:1200
Enter the value of k:600
Value of nCk is 396509646226102014036716014862605729531608195706791651137874888645453416610941265150218719101931467943643355545203450497992759241509133380338379948816055787676920090991204851973167965845932778899299658455186568000803781988756668261491937388063732393433822461878746138860879613812823280622769290795808335597761702284943981759657834318699226167559487050708295600
>>>

/* To calculate the Binomial Coeffcient using recursion */
/* We have use the formula nCk = (n/k)*{(n-1)C(k-1)} */

Wednesday, April 16, 2014

Kaprekar's Number in JAVA: ISC Computer Science Practicals 2010


Post by Maths.


Given the two positive integers p and q, where p < q. Write a program to determine how
many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and
output them.
About 'kaprekar' number:

SAMPLE DATA:

INPUT:
p=1
Q=1000

OUTPUT:

THE KAPREKAR NUMBERS ARE:
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS:8

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2010 */

Tuesday, April 15, 2014

ISC COMPUTER SCIENCE PRACTICALS, 2013


/*
Write a program to declare a square matrix A[][] of order (M X M) where 'M'
is the number of rows and the number of columns such that M must be greater
than 2 and less than 20. Allow the user to input integers into this matrix.
Display appropriate error message for an invalid input.
Perform the following tasks:
(a) Display the input matrix.
(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.
Test your program for the following data and some random data:
Example 1
INPUT : M = 3
4 16 12
8 2 14
6 1 3
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
6 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 6
Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE
*/
/* ISC COMPUTER SCIENCE PRACTICALS, 2013 */

ISC Computer Science Practicals, 2012


/* A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a palindrome.
Given two positive integers m and n, where m < n, write a program to determine how many prime-palindrome integers
are there in the range between m and n (both inclusive) and output them.
The input contains two positive integers m and n where m < 3000 and n < 3000.
Display the number of prime palindrome integers in the specified range along with their values in the format specified below:

Test your program with the sample data and some random data:

Example 1:
INPUT: m=100
N=1000

OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15

Example 2:
M=100
N=5000

OUTPUT: Out of range */

/* ISC COMPUTER SCIENCE PRACTICALS,2012 Solved */

CODE


Tags: ISC, Computer Science, Practicals, 2012, Prime, String.valueOf(), Palindrome, Integer to String, Prime Palindrome

Monday, April 14, 2014

ISC Computer Science Practicals Solved, 2011


/*
Encryption is a technique of coding messages to maintain their secrecy.
A String array of size 'n' where 'n' is greater than 1 and less than 10,
stores single sentences (each sentence ends with a full stop) in each row of the array.
Write a program to accept the size of the array.
Display an appropriate message if the size is not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise.
Change the sentence of the odd rows with an encryption of two characters ahead of the original character. Also change the sentence of the even rows by storing the sentence in reverse order.
Display the encrypted sentences as per the sample data given below.
Test your program on the sample data and some random data.
Input: n=4
IT IS CLOUDY. IT MAY RAIN. THE WEATHER IS FINE. IT IS COOL.
Output: KV KU ENQWFA. RAIN MAY IT. VJG YGCVJGT KU HKPG. COOL IS IT.
Input: n=13
Output: INVALID ENTRY
*/
/* ISC Computer Science Practicals, 2011 */ 

Saturday, April 12, 2014

ISC Computer Science Practicals Solved, 2013


/*
A palindrome is a word that may be read the same way in either direction.
Accept a sentence in UPPER CASE which is terminated by either ".", "?", or "!".
Each word of the sentence is separated by a single blank space.
Perform the following taks:
(a) display the count of palindromic words in the sentence.
(b) Display the palindromic words in the sentence.
Example of palindromic words:
MADAM, ARORA, NOON
Test your program with the sample data and some random data:
Example 1
INPUT : MOM AND DAD ARE COMING AT NOON
OUTPUT : MOM DAD NOON
NUMBER OF PALINDROMIC WORDS : 3
Example 2
INPUT : HOW ARE YOU?
OUTPUT : NO PALINDROMIC WORDS
*/
/* ISC COMPUTER SCIENCE PRACTICAL,2013 */ 

ISC Computer Science Practical Paper-2013

COMPUTER SCIENCE
Paper – 2
(PRACTICAL)
(Reading Time: 15 minutes)
 (Planning Session AND Examination Session: Three Hours)
————————————————————————————————————
The total time to be spent on the Planning and the Examination Session is Three hours.
After completing the Planning Session, the candidate may begin with the Examination Session.
A maximum of 90 minutes is permitted to begin the Examination Session.
(Maximum Marks: 80)
————————————————————————————————————
As it is a practical examination the candidate is expected to do the following:
  1. Write an algorithm for the selected problem. Algorithm should be expressed clearly using ay standard scheme such as pseudo code or in steps which are simple enough to be obviously computable.  [10]
  2. Write a program in JAVA language. The program should follow the algorithm and should be logically and syntactically correct. [20]
  3. Document the program using mnemonic names / comments, identifying and clearly describing the choice of data types and meaning of variables. [20]
  4. Code / Type the program on the computer and get a printout (hard copy). Typically this should be a program that compiles and runs correctly. [10]
  5. Test run the program on the computer using the given sample data and get a printout of the output in the format specified in the problem. [20]
  6. Viva-Voce on the Selected Problem.   [20]
Solve any one of the following Problems:
 Question 1.
An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.
The first nine digits represent the group, publisher and title of the book and the last digit is used to check whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 to 9. Sometimes it is necessary to make the last digit equal to ten. This is done by writing the last digit of the code as X.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third digit and so on until we add 1 time the last digit. If the final number leaves no remainder while divided by 11, the code is a valid ISBN
For example:
0201103311=10*0+9*2+8*0+7*1+6*1+5*0+4*3+3*3+2*1+1*1=55
This is a valid ISBN
007462542X=10*0+9*0+8*7+7*4+6*6+5*2+4*5+3*4+2*2+1*10=176
This is a valid ISBN
Similarly 0112112425 is not a valid ISBN.
 Test Data:
Input code: 0201530821
Output: Sum=99
Leaves no remainder – valid ISBN
Input code: 356680324
Output: Sum=invalid input
Input code: 0231428031
Output: Sum=122
Leaves remainder – invalid ISBN
Question 2:
Write a program to declare a square matrix A[][] of order (MXN) where ‘M’ is the number of rows and the number of columns. ‘M’ should be greater than 2 and less than 20. Allow user to enter integers into this matrix. Display appropriate error message for an invalid input. Perform the following tasks.
1.    Display the input matrix
2.    Create a mirror image of the inputted matrix.
3.    Display the mirror image matrix
Test Data:
Input: M=3
4  16  12
8   2  14
6   1   3
Output:
Original matrix
4  16  12
8   2  14
6   1   3
Mirror image matrix:
12  16  4
14  2    8
3   1    6
Input: M=22
Output: Size out of range
Question 3:
A palindrome is a word that may be read the same in either direction.
Accept a sentence in upper case which is terminated by either ‘.’  ,  ’,’  ,  ‘? ’ ,  ‘!’. Each word in the sentence is separated by a blank space.
Perform the following tasks:
Test Data:
Input: MOM AND DAD ARE COMING AT NOON.
Output: MOM DAD NOON
Number of palindromic words: 3
Input: HOW ARE YOU?
Output: No palindromic words