Here is a list of short and simple C programs that can help you..............

bullet /* calculate exp(x) */
# include <stdio.h>
# include <math.h>

main()
{
double e,fact,x,xp;
int i,j;
clrscr();
for(x=.1;x<=1;x=x+.1){
e=1;
for(i=1;i<=15;i++){
fact=1;
xp=1;
for(j=1;j<=i;j++){
fact=fact*j;
xp=xp*x;
}
e=e+xp/fact;
}
printf("exp(%5.1f)=%16.12f\tEXP(%5.1f)=%16.12f\n",x,e,x,exp(x));
}
getch();
}


 
bullet /* calculate ln(x) */
# include <stdio.h>
# include <math.h>

main()
{
double e,x,xp;
int i,j,xx,count=1;
clrscr();
for(xx=1;xx<=50;xx=xx++){
x=xx/20.;
e=0;
for(i=1;i<=29;i+=2){
xp=1;
for(j=1;j<=i;j++)
xp=xp*((x-1)/(x+1));
e=e+(2*xp)/i;
}
printf("ln(%5.2f)=%16.12f\tLN(%5.2f)=%16.12f\n",x,e,x,log(x));
count=count+1;
if((count%20)==0){
printf("Press any key to cont....\n");
getch();
}
}
getch();
}
 

 

bullet /* Sorting Program */

#include<stdio.h>

int swap(int *,int *);

main()
{
int a[10],i,j,n,flag=0,s=0;
float avg;
clrscr();
for(i=0;i<10;i++){
printf("a[%d",i);
printf("]=");
scanf("%d",&a[i]);
}
n=10;
/*sum & average module*/
for(i=0;i<10;i++)
s=s+a[i];
avg=s/10.;

/*sorting module*/

while(flag==0){
flag=1;
n=n-1;
for(i=0;i<n;i++)
if (a[i]>a[i+1]){
swap(&a[i],&a[i+1]);
flag=0 ; }
}
printf("\nSUM=%d\nAVERAGE=%10.2f",s,avg);
printf("\nSORTED LIST\n");
for(i=0;i<10;i++){
printf("\na[%d",i);
printf("]=");
printf("%d",a[i]);
}
getch();
}

int swap(int *i,int *j)
{
int temp;
temp=*i;
*i=*j;
*j=temp;
}

 
bullet /* Matrix */

#include<stdio.h>
#include<conio.h>
main()
{
int ar2[2][2];
int i,j,a=0,k=0,b;
int ar[2][2]={2,3,4,6};
int ar1[2][2]={4,6,7,8};
clrscr();

for(i=0;i!=2;i++)
{
for(j=0;j!=2;j++)
{
a= a +(ar[j][i]) * (ar1[i][j]);

}
/*printf("\n a value %d",a);
printf("\n b value %d",b);
getch();*/
}
ar2[k][i]=a;
k=1;
for(k=1;k!=2;k++)
{
b=b+ (ar[k][i]
printf("\n%d",a);
getch();
a=0;
b=0;
}

}

 

bullet /* calculation of the binomial series (1+x)^p */
#include<stdio.h>
#include<math.h>

main()
{
double x=0,p=0,s=0,fact=0,p1=0,p2=0,xp=0,x1=0;
int i,j,count=0,xx,ip;
clrscr();
for(xx=-9;xx<=9;xx++){
x=xx*.1;
x1=1+x;
for(ip=1;ip<=10;ip++){
p=ip*.1;
s=1.0;
for(i=1;i<=35;i++){
fact=1;
p1=1;
p2=p;
xp=1;
for(j=1;j<=i;j++){
fact=fact*j;
p1=p1*p2;
p2=p2-1;
xp=xp*x;
}
s=s+p1*xp/fact;
}
printf("\n%5.2f",x1);
printf("^%5.2f",p);
printf("=%16.12f\t%5.2f",s,x1);
printf("^%5.2f",p);
printf("=%16.12f",pow(x1,p));
count=count+1;
if((count%20)==0){
printf("\nPress any key to cont.......\n");
getch();
}
}
}
}

 
bullet /* Concordance Analysis */
#include<stdio.h>

main()
{
int nl=0,nv=0,nc=0,ns=0,nw=0,flag=1;
char c;
clrscr();
gotoxy(10,13);
printf("Enter your sentence=");
while((c=getchar())!='\n'){
if((c>=65 && c<=90)||(c>=97 && c<=122))
nl++;
if(c=='A'|| c=='E' ||c=='I' ||c=='O' ||c=='U' ||c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
nv++;
if(c==' ')
ns++;
if((c>65 && c<=90)||(c>=97 && c<=122) || c=='-'||c==39){
if(flag==1)nw++;
putchar(c);
flag=0;
}
else if(flag==0){
printf("\n");
flag=1;
}
}
nc=nl-nv;
printf("\nNumber of letters=%d",nl);
printf("\nNumbers of vowels=%d",nv);
printf("\nNumbers of consonants=%d",nc);
printf("\nNumbers of spaces=%d",ns);
printf("\nNumbers of words=%d",nw);
printf("\n\nPress any key to exit the program");
getch();
clrscr();
}


 
bullet /*various string functions*/

void getline(char s[])
{
int n=0;
char c;
do{
c=getch();
putchar(c);
if(c!=13) s[n++]=c;
}while(c!=13);
s[n]='\0';
}

int strlen(char s[])
{
int n;
for(n=0;s[n]!='\0';n++);
return n;
}

void strcopy(char s1[],char s2[])
{
int i;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
s2[i]='\0';
}

void strconcat(char s1[],char s2[],char s3[])
{
int i,j;
for(i=0;s1[i]!='\0';i++)
s3[i]=s1[i];
for(j=0;s2[j]!='\0';j++)
s3[i++]=s2[j];
s3[i]='\0';
}

void strreverse(char s[])
{
int i,l;
char c;
for(l=0;s[l]!='\0';l++);
i=0;
l=l-1;
while(i<=l){
c=s[i];
s[i]=s[l];
s[l]=c;
l--;
i++;
}
}

void tolower(char s[])
{
int i;
char c;
for(i=0;s[i]!='\0';i++){
c=s[i];
if((c>='A')&&(c<='Z'))
s[i]=c+32;
}
}


void toupper(char s[])
{
int i;
char c;
for(i=0;s[i]!='\0';i++){
c=s[i];
if((c>='a')&&(c<='z'))
s[i]=c-32;
}
}

int strword(char s[])
{
int i,flag=1,nword=0;
char c;
for(i=0;s[i]!='\0';i++){
c=s[i];
if((c>='A' && c<='Z')||(c>='a' && c<='z')||(c=='-')||(c=='_')||(c=='\'')){
printf("%c",c);
if(flag==1) nword++;
flag=0;
}
else if(flag==0){
printf("\n");
flag=1;
}
}
return nword;
}
 

 

bullet /*CRAMER'S RULE*/

#include<stdio.h>
#include<math.h>

/*double deter(*a[3][3])
{
det=a[1][1]*a[2][2]*a[3][3]+a[1][2]*a[2][3]*a[3][1]+a[1][3]*a[3][2]*a[2][1]-(a[1][1]*a[2][3]*a[3][2]+a[1][2]*a[2][1]*a[3][3]+a[1][3]*a[3][1]*a[2][2])
return det;
}
*/
main()
{
double det=0,det1=0,detx1=0,detx2=0,detx3=0,x1=0,x2=0,x3=0;
int i,j,a[3][3],b[3][3],d[3];
clrscr();
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("b(%d",i);
printf(",%d",j);
printf(")=");
scanf("%d",&b[i][j]);
}
printf("d(%d",i);
printf(")=");
scanf("%d",&d[i]);
}
for(i=1;i<=3;i++)
for(j=i;j<=3;j++)
a[i][j]=b[i][j];

/*deter(&a[3][3]);*/
det=a[1][1]*a[2][2]*a[3][3]+a[1][2]*a[2][3]*a[3][1]+a[1][3]*a[3][2]*a[2][1]-(a[1][1]*a[2][3]*a[3][2]+a[1][2]*a[2][1]*a[3][3]+a[1][3]*a[3][1]*a[2][2]);
det1=det;
a[1][1]=d[1];
a[2][1]=d[2];
a[3][1]=d[3];
for(i=1;i<=3;i++)
for(j=2;j<=3;j++)
a[i][j]=b[i][j];

/*deter(&a[3][3]);*/
det=a[1][1]*a[2][2]*a[3][3]+a[1][2]*a[2][3]*a[3][1]+a[1][3]*a[3][2]*a[2][1]-(a[1][1]*a[2][3]*a[3][2]+a[1][2]*a[2][1]*a[3][3]+a[1][3]*a[3][1]*a[2][2]) ;
detx1=det;
x1=detx1/det1;
a[1][2]=d[1];
a[2][2]=d[2];
a[3][2]=d[3];
for(i=1;i<=3;i++)
for(j=1;j<=3;j=j+2)
a[i][j]=b[i][j];
/* deter(&a[3][3]);*/
det=a[1][1]*a[2][2]*a[3][3]+a[1][2]*a[2][3]*a[3][1]+a[1][3]*a[3][2]*a[2][1]-(a[1][1]*a[2][3]*a[3][2]+a[1][2]*a[2][1]*a[3][3]+a[1][3]*a[3][1]*a[2][2]);
detx2=det;
x2=detx2/det1;
a[1][3]=d[1];
a[2][3]=d[2];
a[3][3]=d[3];
for(i=1;i<=3;i++)
for(j=1;j<=2;j++)
a[i][j]=b[i][j];

/* deter(&a[3][3]); */
det=a[1][1]*a[2][2]*a[3][3]+a[1][2]*a[2][3]*a[3][1]+a[1][3]*a[3][2]*a[2][1]-(a[1][1]*a[2][3]*a[3][2]+a[1][2]*a[2][1]*a[3][3]+a[1][3]*a[3][1]*a[2][2]);
detx3=det;
x3=detx3/det1;
printf("x1=%f\tx2=%f\tx3=%f",x1,x2,x3);
getch();

}

 
bullet /*program to convert c to f */
#include <stdio.h>
main()
{
float c,f;
char choice='y';
do {
clrscr();
gotoxy(10,13);
printf("enter c=");
scanf("%f",&c);
if(c<0 || c>100){
printf("invalid data terminate the process \n");
break;
}
f=9*c/5.+32;
printf("c=%6.1f\tf=%6.1f\n",c,f);
printf("do you want to continue(y/n)");
choice=getch();
}while((choice=='y')||(choice=='Y'));
}


 
bullet /* calculate sin(x) */
# include <stdio.h>
# include <math.h>

main()
{
double e,rad,fact,x,xp;
int i,j,k,count=1;
clrscr();
for(x=0;x<=360;x=x+5){
rad=(3.1415926/180)*x;
e=rad;
k=-1;
for(i=3;i<=27;i+=2){
fact=1;
xp=1;
for(j=1;j<=i;j++){
fact=fact*j;
xp=xp*rad;
}
e=e+(xp/fact)*k;
k=-k;
}
printf("sin(%5.1f)=%16.12f\tSIN(%5.1f)=%16.12f\n",x,e,x,sin(rad));
count++;
if((count%20)==0){
printf("Press any key to con....\n");
getch();
}

}
getch();

}



 
bullet /* calculate cos(x) */
# include <stdio.h>
# include <math.h>

main()
{
double e,rad,fact,x,xp;
int i,j,k,count=1;
clrscr();
for(x=0;x<=360;x=x+5){
rad=(3.1415926/180)*x;
e=0;
k=1;
for(i=0;i<=22;i+=2){
fact=1;
xp=1;
for(j=1;j<=i;j++){
fact=fact*j;
xp=xp*rad;
}
e=e+(xp/fact)*k;
k=-k;
}
printf("cos(%5.1f)=%16.12f\tCOS(%5.1f)=%16.12f\n",x,e,x,cos(rad));
count=count+1;
if((count%20)==0){
printf("Press any key to cont....\n");
getch();
}
}
getch();
}



 
bullet /* calculate tan(x) */
# include <stdio.h>
# include <math.h>

main()
{
double e,ee,eee,rad,fact,x,xp;
int i,j,k,kk,count=1;
clrscr();
for(x=0;x<=360;x=x+5){
rad=(3.1415926/180)*x;
e=rad;
k=-1;
for(i=3;i<=27;i+=2){
fact=1;
xp=1;
for(j=1;j<=i;j++){
fact=fact*j;
xp=xp*rad;
}
e=e+(xp/fact)*k;
k=-k;
}
printf("sin(%5.1f)=%16.12f\tSIN(%5.1f)=%16.12f\n",x,e,x,sin(rad));
/*if((count%20)==0){
printf("Press any key to con....\n
");
getch();
} */
ee=0;
kk=1;
for(i=0;i<=22;i+=2){
fact=1;
xp=1;
for(j=1;j<=i;j++){
fact=fact*j;
xp=xp*rad;
}
ee=ee+(xp/fact)*kk;
kk=-kk;
}
printf("cos(%5.1f)=%16.12f\tCOS(%5.1f)=%16.12f\n",x,ee,x,cos(rad));
/*
if((count%20)==0){
printf("Press any key to cont....\n");
getch();
} */
eee=e/ee;
printf("tan(%5.1f)=%16.12f\tTAN(%5.1f)=%16.12f\n",x,eee,x,tan(rad));
count++;
if((count%9)==0){
printf("Press any key to proceed....\n");
getch();
}
}
getch();
}


 
bullet /* calculate ln(1+x) */
#include<stdio.h>
#include<math.h>
main()
{
double x=0,xp=0,xx=0,e;
int i,j,count=1,k=1;
clrscr();
for(xx=-19;xx<=20;xx++){
x=xx/20.;
e=0;
for(i=1;i<=50;i++){
xp=1;
for(j=1;j<=i;j++)
xp=xp*x;
e=e+xp*k/i;
k=-k;
}
printf("ln(1+%5.2f)=%16.12f\tLN(%5.2f)=%16.12f\n",x,e,(1+x),log(1+x));
count=count+1;
if((count%20)==0){
printf("Press any key to cont....\n");
getch();
}
}

getch();
}

 
bullet /* calculate arctan(x) */
#include<stdio.h>
#include<math.h>
main()
{
double x=0,xp=0,xx=0,e;
int i,j,count=1,k=1;
clrscr();
for(xx=-20;xx<=20;xx++){
x=xx/20.;
e=0;
for(i=1;i<=51;i=i+2){
xp=1;
for(j=1;j<=i;j++)
xp=xp*x;
e=e+xp*k/i;
k=-k;
}
printf("arctan(%5.2f)=%16.12f\tatan(%5.2f)=%16.12f\n",x,e,x,atan(x));
count=count+1;
if((count%20)==0){
printf("Press any key to cont....\n");
getch();
}
}
getch();
}


 
bullet /*calculation of pie*/
#include<stdio.h>
#include<math.h>

main()
{
double p=0,s=0,sf=0,pi=0;
int i;
clrscr();
s=1;
sf=-1;
pi=3;
p=1.73205080756588*2;
for(i=3;i<=35;i=i+2){
s=s+sf/(i*pi);
sf=-sf;
pi=pi*3;
}
s=s*p;
printf("Value of pie is =%16.12f",s);
getch();
}
 

 

bullet /* Gauss-Jordan method */
#include<stdio.h>

main()
{
float a[10][20],b[10],x[10],t1,t2;
int i,j,k,n,n1,n2,flag=0;
clrscr();
gotoxy(10,13);
printf("Enter no of simultaneous equation ");
scanf("%d",&n);
/* To set all elements of matrix a to zero*/
for(i=0;i<10;i++)
for(j=0;j<20;j++)
a[i][j]=0;
n1=n;
n2=2*n;
for(i=0;i<n;i++)
a[i][n+i]=1;
/*to input*/
clrscr();
for(i=0;i<n;i++)
for(j=0;j<n;j++){
printf("a[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
/*inputarray-b*/
for(i=0;i<n;i++){
printf("b[%d]=",i);
scanf("%f",&b[i]);
}
/*gauss-jordan method starts*/
for(k=0;k<n;k++){
t1=a[k][k];
if(t1==0){
printf("inversion matrix not possible");
flag=1;
break;
}
for(j=0;j<n2;j++)
a[k][j]=a[k][j]/t1;
for(i=0;i<n;i++){
if(i!=k){
t2=a[i][k];
for(j=0;j<n2;j++)
a[i][j]=a[i][j]-t2*a[k][j];
}
}
}
/*calculation*/
for(i=0;i<n;i++){
x[i]=0;
for(j=n1;j<n2;j++)
x[i]=x[i]+a[i][j]*b[j-n];
}
if(flag==0){
clrscr();
printf("\nInverse matrix of a\n");
for(i=0;i<n;i++){
for(j=n;j<n2;j++)
printf("%10.4f",a[i][j]);
printf("\n");
}
printf("\nRoots:");
for(i=0;i<n;i++)
printf("x[%d]=%10.4f\n",i,x[i]);
}
getch();

}
 

Hosted by www.Geocities.ws

1