The Programming Project: Code
Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Thursday, April 17, 2014

Binomial Coefficient using recursion in Python and C language





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)} */

Tuesday, September 24, 2013

Combination in Lexicographical Order




#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int main()
{
char alpha[]={'?','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,pos,r,tmp,j,flag=1;
long int tot=0;
char *combination;
printf("\n Enter the number of objects (max value is 26):");
scanf("%d",&n);
printf("\n Enter the value of r ( 1<= r <= n):");
scanf("%d",&r);
combination =(char *)malloc(n*sizeof(char));
for(i=1;i<=n;i++)
combination[i]=alpha[i];
printf("\n The combinations are (is):\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot=1;
while(flag)
{
for(i=r;i>=1;i--)
{
if(combination[i]-64==n-r+i)
{
}
else
{
pos=i;
break;
}
}
if(i==0)
{
flag=0;
break;
}
for(j=1;j<=26;j++)
if(combination[pos]==alpha[j])
break;
combination[pos]=alpha[j+1];
for(i=pos+1;i<=r;i++)
combination[i]=alpha[(combination[i-1]-64)+1];
printf("\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot++;
}
printf("\n Total number of combination is: %ld",tot);
return 0;
}


Monday, September 16, 2013

Intersection of Sets



To find intersection of two sets through C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void inter_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
inter_set(s1,s2,n,m);
return 0;
}
void inter_set(int *s1,int*s2,int n,int m)
{
int *s,i=0,j,k;
int flag=0;
s=(int *)malloc((m<n ? m: n)*sizeof(int));
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s1[k])
{
flag=1;
break;
}
}

if(flag==1)
{
s[i]=s2[j];
         i++;
flag=0;
}
 if(flag==0)
continue;
 }
 if(i==0)
printf("\n Intersection is Null:");
else
{
  printf("\n Intersection of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);
}


return;
}

Tuesday, August 27, 2013

Parentheses Checking

Given an expression string exp, write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[","]” are correct in exp. For example, the program should print true for exp = “[a+{b-(c-(d-e)*d)+w)}+q]” and false for exp = “[(])”



 import java.util.*;

public class Parentheses
   {
   public static void main(String[] args)
{
int n,i,pos,posm,t;
boolean flag=true;
char[] symb={'(','{','[',']','}',')'};
char tmp;
Scanner in = new Scanner(System.in);
String expression;
System.out.println(" Enter the expression:");
expression=in.nextLine();
System.out.println(" Entered expression is:"+expression);
n=expression.length();
Stack s = new Stack(n);
System.out.println("");
for(i=0;i<n;i++)
{
pos=0;
posm=0;
if(expression.charAt(i)==symb[0] || expression.charAt(i)==symb[1] || expression.charAt(i)==symb[2] )
{
s.push(expression.charAt(i));

}
if(expression.charAt(i)==symb[3] || expression.charAt(i)==symb[4] || expression.charAt(i)==symb[5] )
{
if(s.isempty()==true)
{
flag=false;
break;
}
else
{
tmp=s.pop();
for(t=0;t<3;t++)
if(symb[t]==tmp)
pos=t;
for(t=3;t<6;t++)
if(symb[t]==expression.charAt(i))
posm=t;
if(pos+posm!=5)
{
flag=false;
break;
}
}
}
 

}
if(s.isempty()==false)
flag=false;
if(flag==true)
System.out.println(" Valid Expression:");
else
System.out.println(" Invalid Expression:");
in.close();
}

  }
class Stack
{
public Stack(int a)
{
top=-1;
n=a;
stack = new char[n];
}
public void push(char x)
{
stack[++top]=x;
}
public char pop()
{
return (stack[top--]);
}
public char stacktop()
{
return (stack[top]);
}
public boolean isempty()
{
return ( top==-1 ? true: false);
}
private int top;
private int n;
private char[] stack;

}