Friday 30 December 2011

C program to check whether a number is a strong number



#include<stdio.h>
int fact(int);
main()
{
int n,t,r,s;
printf("Enter any number\n");
scanf("%d",&n);
t=n;
s=0;
while(n>0)
{
r=n%10;
s=s+fact(r);
n=n/10;
}
if(t==s)
printf("\n%d is a strong number",t);
else
printf("\n%d is not a strong number",t);
printf("\n");
}
int fact(int a)
{
int f,g;
f=1;
for(g=a;g>0;g--)
f=f*g;
return(f);
}



C program to find the upper right and lower left triangles of a matrix



#include<stdio.h>
main()
{
int a[10][10],i,j,r,c;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
if(r==c)
{
printf("\nUpper right triangle\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(j>=i)
printf("%d\t",a[i][j]);
else
printf("\t");
}
printf("\n");
}
printf("\nLower left triangle\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(j<=i)
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
else
printf("\nNot possible to print triangles");
printf("\n");
}




Thursday 29 December 2011

C program to find the transpose of a matrix



#include<stdio.h>
main()
{
int a[10][10],b[10][10],i,j,r,c,t;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
printf("Transpose of matrix is\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}




C program to find the sum of the diagonal elements(Trace) of a matrix



#include<stdio.h>
main()
{
int a[10][10],s,i,j,r,c;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
s=0;
if(r==c)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
s=s+a[i][j];
}
}
printf("\nSum of diagonal elements = %d",s);
}
else
printf("\nNo diagonal elements");
printf("\n");
}



Wednesday 28 December 2011

C program to print the Pascal's Triangle



#include<stdio.h>
main()
{
int b,p,q,r,x;
printf("Enter the number of rows\n");
scanf("%d",&r);
b=1;
q=0;
printf("\nPascal's Triangle\n");
while(q<r)
{
for(p=30-3*q;p>0;p--)
printf(" ");
for(x=0;x<=q;x++)
{
if(x==0||q==0)
b=1;
else
b=(b*(q-x+1)/x);
printf("%6d",b);
}
printf("\n");
q++;
}
}



C program to find the factorial of a number using recursion



#include<stdio.h>
int fact(int);
main()
{
int n,temp;
printf("Enter a number\n");
scanf("%d",&n);
if(n>=0)
{
temp=fact(n);
printf("Factorial is %d",temp);
}
else
printf("Cannot find the factorial");
}

int fact(int c)
{
if(c==0)
return(1);
else
return(c*fact(c-1));
}



Tuesday 27 December 2011

C program to find the sum of digits of a number



#include<stdio.h>
main()
{
int r,s;
long n;
printf("Enter any number\n");
scanf("%ld",&n);
s=0;
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("The sum of digits is %d",s);
}



C program to check whether a number is Palindrome or not



#include<stdio.h>
main()
{
int n,t,s,r;
printf("Enter any number\n");
scanf("%d",&n);
t=n;
s=0;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
if(t==s)
printf("%d is a Palindrome number",t);
else
printf("%d is not a Palindrome number",t);
printf("\n");
}



C program to check whether a number is Armstrong or not



#include<stdio.h>
main()
{
int n,t,s,r;
printf("Enter any number\n");
scanf("%d",&n);
t=n;
s=0;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(t==s)
printf("%d is an Armstrong number",t);
else
printf("%d is not an Armstrong number",t);
printf("\n");
}



C program to find the product of two matrices



#include<stdio.h>
main()
{
int i,j,k,r1,c1,r2,c2,a[10][10],b[10][10],c[10][10];
printf("Enter the no. of rows and columns of matrix 1 : ");
scanf("%d%d",&r1,&c1);
printf("Enter the no. of rows and columns of matrix 2 : ");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("Enter the matrix 1\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the matrix 2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Product of matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication not possible");
printf("\n");
}



C program to find the determinant and inverse of two matrices using recursion



#include<stdio.h>
#include<math.h>
float detrm(float[25][25],float);
void cofact(float[25][25],float);
void trans(float[25][25],float[25][25],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("Enter the order of the matrix\n");
scanf("%f",&k);
printf("Enter the elements of the matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("Deteminant = %f",d);
if(d==0)
printf("\nMatrix is not inversible\n");
else
cofact(a,k);
}

float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}

void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}

void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("\nThe inverse of the matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inv[i][j]);
}
printf("\n");
}
}




C program to search and delete a substring from a string



#include<stdio.h>
#include<string.h>
main()
{
char a[50],b[10];
int i,j,temp,p[10]={0},ch,x,pos;
printf("Enter a string\n");
gets(a);
printf("Enter a substring\n");
gets(b);
printf("\n1.Searching\n2.Deletion\nEnter your choice\n");
scanf("%d",&ch);
for(i=0,x=0;i<strlen(a);i++)
{
j=0;
temp=0;
if(a[i]==b[0])
{
pos=i;
while(a[i]==b[j])
{
temp=1;
i++;
j++;
}
}
if((j>=strlen(b))&&(temp==1))
{
p[x]=pos+1;
x++;
}
}
if(ch==1)
{
if(p[0]==0)
printf("\nThe substring not found\n");
else
{
printf("\nSubstring is found at the position:");
for(i=0;i<x;i++)
printf(" %d",p[i]);
}
printf("\n");
exit(0);
}
else if(ch==2)
{
if(p[0]!=0)
{
printf("\nString after deletion\n");
pos=0;
for(x=0;p[x]!=0;x++)
{
for(i=pos;i<p[x]-1;i++)
printf("%c",a[i]);
pos=p[x]-1+strlen(b);
}
for(i=pos;a[i]!='\0';i++)
printf("%c",a[i]);
}
else
printf("\nDeletion is not possible\n");
}
else
printf("\nInvalid choice\n");
printf("\n");
}




Monday 26 December 2011

C program to sort the given set of strings(Lexicographic sorting)



#include<stdio.h>
#include<string.h>
main()
{
char str[10][20],temp[20];
int i=0,j=0,n;
printf("Enter the number of items\n");
scanf("%d",&n);
printf("Enter the items\n");
while(i<n)
scanf("%s",str[i++]);
for(i=1;i<n;i++)
{
for(j=1;j<=n-i;j++)
{
if(strcmp(str[j-1],str[j])>0)
{
strcpy(temp,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],temp);
}
}
}
printf(" \nSorted Items\n");
for(i=0;i<n;i++)
printf(" %s\n",str[i]);
}



C program to find the lcm and hcf of given numbers using Euclid's Algorithm



#include<stdio.h>
int hcff(int,int);
int lcmf(int,int);
main()
{
int i,a[100],n,lcm,hcf;
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
hcf=a[0];
for(i=1;i<n;i++)
hcf=hcff(hcf,a[i]);
printf("HCF = %d\n",hcf);
lcm=a[0];
for(i=1;i<n;i++)
lcm=lcmf(lcm,a[i]);
printf("LCM = %d",lcm);
}

int hcff(int m,int n)
{
int r,t;
if(m<n)
{
t=m;
m=n;
n=t;
}
while(1)
{
r=m%n;
if(r==0)
return n;
else
m=n;
n=r;
}
}

int lcmf(int m,int n)
{
int lcm;
lcm=m*n/hcff(m,n);
return lcm;
}



C program to convert a binary number to decimal,octal and hexadecimal number and back



#include<stdio.h>
main()
{
int n,z,r,b,b1,b2,d,d1,o,o1,h,i,j,y1[10]={0},y2[10]={0},y3[10]={0},y4[10]={0};
char a,x[20];
printf("\tConversions\n1.Binary to Decimal\n2.Decimal to Binary\n3.Binary to Octal\n4.Octal to Binary\n5.Binary to Hexadecimal\n6.Hexadecimal to Binary\n7.Exit");
while(1)
{
printf("\nEnter you choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter the Binary number\n");
scanf("%d",&b);
z=1,d=0;
while(b>0)
{
r=b%10;
if(r>1)
{
printf("Invalid Entry");
exit(0);
}
d=d+(r*z);
z=z*2;
b=b/10;
}
printf("Decimal equivalent = %d",d);
break;

case 2:
printf("Enter the Decimal number\n");
scanf("%d",&d1);
j=0;
while(d1>0)
{
r=d1%2;
y1[j]=r;
j++;
d1=d1/2;
}
printf("Binary equivalent =");
for(i=j-1;i>=0;i--)
printf("%d",y1[i]);
break;

case 3:
printf("Enter the Binary number\n");
scanf("%d",&b1);
z=1,o=0,j=0;
while(b1>0)
{
r=b1%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
o=o+(r*z);
z=z*2;
b1=b1/10;
}
while(o>0)
{
r=o%8;
y2[j]=r;
j++;
o=o/8;
}
printf("Octal equivalent = ");
for(i=j-1;i>=0;i--)
printf("%d",y2[i]);
break;

case 4:
printf("Enter the Octal number\n");
scanf("%d",&o1);
z=1,b=0,j=0;
while(o1>0)
{
r=o1%10;
if(r>7)
{
printf("Invalid entry");
exit(0);
}
b=b+(r*z);
z=z*8;
o1=o1/10;
}
while(b>0)
{
r=b%2;
y3[j]=r;
j++;
b=b/2;
}
printf("Binary equivalent = ");
for(i=j-1;i>=0;i--)
printf("%d",y3[i]);
break;

case 5:
printf("Enter the Binary number\n");
scanf("%d",&b2);
z=1,h=0,j=0;
while(b2>0)
{
r=b2%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
h=h+(r*z);
z=z*2;
b2=b2/10;
}
while(h>0)
{
r=h%16;
y4[j]=r;
j++;
h=h/16;
}
printf("Hexadecimal equivalent = ");
for(i=j-1;i>=0;i--)
{
switch(y4[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",y4[i]);
break;
}
}
break;

case 6:
printf("Enter the Hexadecimal number\n");
scanf("%s",x);
printf("Binary equivalent = ");
for(i=0;x[i]!='\0';i++)
{
a=x[i];
switch(a)
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
printf("1010");
break;
case 'B':
printf("1011");
break;
case 'C':
printf("1100");
break;
case 'D':
printf("1101");
break;
case 'E':
printf("1110");
break;
case 'F':
printf("1111");
break;
default:
printf("Invalid entry");
break;
}
}
break;

case 7:
exit(0);
}
}
}




C program to find the mean,median and mode of a set of numbers



#include<stdio.h>
main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);

for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}




Monday 12 December 2011

C program to print diamond pattern



#include<stdio.h>

main()
{
int i,n,j,x;
printf("Enter a Number:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(x=0;x<n-i;x++)
{
printf("\t");
}
for(j=0;j<=i;j++)
{
printf("*\t\t");
}
printf("\n\n");
}
for(i=1;i<n;i++)
{
printf("\t");
for(x=1;x<=i;x++)
{
printf("\t");
}
for(j=n;j>i;j--)
{
printf("*\t\t");
}
printf("\n\n");
}
}




C program to find the permutation of a given string using recursion



#include<stdio.h>
#include<string.h>

void perm(char * , int , int );
char temp,str[20];
int l;
int main()
{
printf("Enter the string : ");
scanf("%s",str);
printf("The permutation is\n");
l=strlen(str);
perm(str,0,l);
}

void perm(char *str,const int strt, int len)
{
int i,j;
for(i=strt;i<len-1;++i)
for(j=i+1;j<len;++j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
perm(str , i+1 ,len);
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
printf("%s\n",str);
}



C program to show acromatic string



#include<stdio.h>

main()
{
char ch[100];
int i;
printf("Enter any string : ");
  gets(ch);
i=0;
printf("%c",ch[0]);
while(ch[i]!='\0')
{
if(ch[i]==' ')
{
i++;
printf("%c",ch[i]);
}
i++;
}
printf("\n");
}



C program to find the number of days between two dates



#include<stdio.h>
#include<conio.h>

void days(int,int,int,int,int,int);
int month(int,int);
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};

 main()
  {
  int a1,b1,c1,a2,b2,c2;
  clrscr();
  printf("Enter first date(dd mm yyyy) : ");
  scanf("%d%d%d",&a1,&b1,&c1);
  printf("\nEnter second date(dd mm yyyy) : ");
  scanf("%d%d%d",&a2,&b2,&c2);
  if(c2>=c1)
  days(c1,c2,b1,b2,a1,a2);
  else
  days(c2,c1,b2,b1,a2,a1);
  getch();
  }

 void days(int y1,int y2,int m1,int m2,int d1,int d2)
  {
  int count=0,i;
  for(i=y1;i<y2;i++)
  {
  if(i%4==0)
  count+=366;
  else
  count+=365;
  }
  count-=month(m1,y1);
  count-=d1;
  count+=month(m2,y2);
  count+=d2;
  if(count<0)
  count=count*-1;
  printf("The no. of days b/w the 2 dates = %d days",count);
  }

 int month(int a,int yy)
  {
  int x=0,c;
  for(c=0;c<a-1;c++)
  {
  if(c==1)
    {
    if(yy%4==0)
    x+=29;
    else
    x+=28;
    }
  else
  x+=mon[c];
  }
  return(x);
  }




C program to check whether a number is prime or not


#include<stdio.h>

main()
 {
  int n,i,r,f=0;
  printf("Enter a number\n");
  scanf("%d",&n);
  if(n==1)
  printf("1 is neither prime nor composite\n");
  else
  {
  for(i=2;i<n;i++)
{
r=n%i;
if(r==0)
 {
 f=1;
 break;
 }
else
f=0;
}
  if(f==0)
  printf("It is a prime number\n");
  else
  printf("It is not a prime number\n");
  }
 }



Saturday 1 October 2011

C program to find the taylor series expansion of sin x,cos x and e^x


#include<stdio.h>
#include<math.h>
#define PI 3.1415

float exp_x( int, int );
double sin_x( int, int );
double cos_x( int, int );
int fact( int );

int main()
{
    int choice, x, n;

    do
        {
            printf( "\nMenu\n[1] e^x)\n[2] Sin(x)\n[3] Cos(x)\n[4] Exit\n" );
            scanf( "%d", &choice );

            switch ( choice )
                {

                case 1:  // to find exponential value
                    printf( "e^x\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "e^%d(%d) = %f\n", x, n, exp_x( x, n ) );
                    break;

                case 2:  // to find sinx
                    printf( "sin(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\ncos(%d)(%d) = %f\n", x, n, sin_x( x, n ) );
                    break;

                case 3:  // to find cosx
                    printf( "cos(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\ncos(%d)(%d) = %f\n", x, n, cos_x( x, n ) );
                    break;

                case 4:  // exit
                    break;

                default: // wrong choice
                    printf( "Wrong choice" );
                    break;
                }
}
    while ( choice != 4 );
}

float exp_x( int x, int n )
{
    int i = 1;
    float ex = 1;

    while ( i < n )
        {
            ex += ( float ) pow( x, i ) / fact( i );
            ++i;
        }

    return ex;
}

double sin_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 0.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
        {
            value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}

double cos_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 1.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )
        {
            value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}

int fact( int num )
{
    int f = 0;

    if ( num == 1 )
        return 1;
    else
f = num * fact( num - 1 );

    return f;
}