Saturday, January 11, 2014

MCA C Programming Practical University of Madras

MCA C Programming Practical University of Madras 


/*Prime Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,c,i;
clrscr();
printf("Enter the value:");
scanf("%d",&a);
for(i=2;i<a;i++)
{
if(a%i==0)
{
c=1;
}
}
if(c!=1)
printf("The given no. %d is Prime",a);
else
printf("The given no. %d is Not prime",a);
getch();
}


OUTPUT:

Enter the value: 23
The given no. 23 is Prime

Enter the value: 48
The given no. 48 is Not prime

















/*Pascal Triangle*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,flag,k=0,x=40,y=4;
clrscr();
printf("Enter the Number:");
scanf("%d",&m);
for(i=1;i<m;i++)
{
flag=0;
n=1;
x=40-i;
gotoxy(x,y);
for(j=1;j<=i+k;j++)
{
printf("%d",n);
if(i==n)
flag=1;
if(flag)
n--;
else
n++;
}
y++;
k++;
}
getch();
}


















OUTPUT:

Enter the number: 9

1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321












/*String Manipulation*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100],c[100],d[100];
int length;
clrscr();
printf("Enter a string:\n");
scanf("%s",&a);
length=strlen(a);
printf("Length the entered string is = %d\n",length);
printf("You entered the string %s \n",a);
printf("Enter the first string:\n");
scanf("%s",&a);
printf("Enter the second string:\n");
scanf("%s",&b);
if(strcmp(a,b)==0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
printf("Copy string\n");
strcpy(c,a);
printf("After copy string c=%s\n",c);
printf("String Concatnation Operation\n");
strcat(a,b);
printf("%s\n",a);
printf("String Reverse\n");
strrev(c);
printf("Reversing the string %s\n",c);
printf("Enter the string to check if it is a palindrome\n");
scanf("%s",&a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("Entered String is a palindrome\n");
else
printf("Entered string is not a palindrome\n");
getch();
}






OUTPUT:
Enter the string:
Mahindra
Length of the entered string is = 8
You entered the string Mahindra
Enter the first string:
Volks
Enter the second string:
Wagen
Entered strings are not equal.
Copy string
After  copy string c = Volks
String Concatnation Operation
VolksWagen
String Reverse
Reversing the string sklov
Enter the string to check if it is a palindrome
Malayalam
Entered string is a palindrome





/*Matrix Multiplication*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[3][3],b[3][3],c[3][3];
clrscr();
printf("Enter the elements of first array:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second array:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
c[i][j]=0;
for(k=0;k<=2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The Matrix of A is:\n");
for(i=0;i<=2;i++)
{
printf("\n");
for(j=0;j<=2;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n\n");
printf("The Matrix of B is:\n");
for(i=0;i<=2;i++)
{
printf("\n");
for(j=0;j<=2;j++)
{
printf("%d\t",b[i][j]);
}
}
printf("\n\n");
printf("The Result is:\n");
for(i=0;i<=2;i++)
{
printf("\n");
for(j=0;j<=2;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}







OUTPUT:
Enter the elements of first array: 1
2
3
4
5
6
7
8
9
Enter the elements of second array: 9
8
7
6
5
4
3
2
1

The Matrix of A is:
1                  2                  3
4                  5                  6
7                  8                  9
The Matrix of B is:
9                  8                  7
6                  5                  4
3                  2                  1
The  Result is:
30                24                18
84                69                54
138              114              90
















/*Determinant of a matrix*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],i,j;
long deter;
clrscr();
printf("Enter the 9 elements of matrix:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is \n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
deter=a[0][0]*((a[1][1]*a[2][2])-(a[2][1]*a[1][2]))-a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2])+a[0][2]*(a[1][0]*a[2][1]-a[2][0]*a[1][1]);
printf("\nDeterminant of 3x3 matrix: %d",deter);
getch();
return 0;
}
OUTPUT:
Enter the 9 elements of matrix: 1
5
7
8
7
9
1
2
7

The Matrix is
1                  5                  7
8                  7                  9
1                  2                  7
Determinant of the 3 x 3 matrix: -141








/*Tautologies and Contradictions*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a=1,b=0,c;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
c=i|!j&k;
a=a&c;
b=b|c;
}
}
}
printf("\n\t\t\t TAUTOLOGY AND CONTRADICTION\n");
printf("\t\t\t*****************************");
printf("\n\n\tGiven Funtion: a+b'.c");
printf("\n\n\tResult:");
printf("\n\n\tThe Given Function is ");
if(a==1)
printf("Tautology");
else if(b==0)
printf("Contradiction");
else
printf("Neither Tautology Nor Contradiction");
getch();
}















OUTPUT:

TAUTOLOGY AND CONTRADICTION
************************************

Given Function: a+b’.c
 Result:
The Given Function is Neither Tautology nor Contradiction
















/*Euclidean’s Algorithm for Finding GCD*/

#include<stdio.h>
#include<conio.h>
void hanoi(int,char,char);
char getp(char,char);
void hanoi(int n,char from,char to)
{
char temp;
if(n==0)
return;
temp=getp(from,to);
hanoi(n-1,from,temp);
printf("Move disk %d from %c to %c \n",n,from,to);
hanoi(n-1,temp,to);
}
char getp(char x,char y)
{
if((x=='A'&&y=='B')||(x=='B'&&y=='A'))
return 'C';
if((x=='A'&&y=='C')||(x=='C'&&y=='A'))
return 'B';
return 'A';
}
int main(void)
{
int n;
clrscr();
printf("Enter how many disk:\n");
scanf("%d",&n);
hanoi(n,'A','C');
getch();
return 0;
}














OUTPUT:

Enter how many disk:
3
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C












/* Creating database for telephone number and related operations, Using File concepts*/

#include<stdio.h>
#include<conio.h>
void main()
{
struct telephone
{
char name[30];
long int phoneno;
};
int i,n,s,nn;
struct telephone t;
FILE *fp;
fp=fopen("tel.dbf","w");
printf("Enter how many records to write");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name:");
scanf("%s",t.name);
printf("Enter the phoneno.");
scanf("%d",&t.phoneno);
fwrite(&t,sizeof(t),1,fp);
}
fclose(fp);
printf("Reading File content\n");
fp=fopen("tel.dbf","r");
while(fread(&t,sizeof(t),1,fp)==1)
{
printf("\n%s %d",t.name,t.phoneno);
}
fclose(fp);
s=0;
fp=fopen("tel.dbf","r");
printf("\nEnter the number to search");
scanf("%d",&nn);
while(fread(&t,sizeof(t),1,fp)==1)
{
if(nn==t.phoneno)
{
printf("\nNumber Found\n");
printf("\n%s %d",t.name,t.phoneno);
s=1;
}
}
if(s==0)
printf("Number Not Found\n");
}
OUTPUT:
Enter how many records to write 2
Enter the name: Hemanth
Enter the phoneno. 9841
Enter the name: Raj
Enter the phoneno. 1591
Reading File content

Hemanth      9841
Raj               1591
Enter the number to search 1591

Number Found
Raj               1591









/* Creating database for mailing addresses and related operations, Using Structures */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct mail
{
char name[30];
char mailid[30];
};
char md[30];
int i,n,s,nn;
struct mail t[100];
clrscr();
printf("Enter how many records to write");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name:");
scanf("%s",t[i].name);
printf("Enter the MailID:");
scanf("%s",t[i].mailid);
}
printf("Reading File Content\n");
for(i=0;i<n;i++)
{
printf("\nThe Name:");
printf("%s",t[i].name);
printf("\nThe Mail ID:");
printf("%s",t[i].mailid);
}
s=0;
printf("\nEnter the mailid to search:");
scanf("%s",&md);
for(i=0;i<n;i++)
{
if(strcmp(md,t[i].mailid)==0)
{
printf("\nMail ID Found\n");
printf("\n%s %s",t[i].name,t[i].mailid);
s=1;
}
}
if(s==0)
printf("\nMail ID Not Found\n");
}


OUTPUT:
Enter how many records to write 2
Enter the name: Hemanth
Enter the MailID: hemanth@yahoo.com
Enter the name: Raj
Enter the MailID: raj@gmail.com
Reading File content

The Name: Hemanth
The Mail ID: hemanth@yahoo.com
The Name: Raj
The Mail ID: raj@gmail.com
Enter the number to search 1591

Mail ID Found

Raj               raj@gmail.com               






/*Bisection method*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) x*x*x-x-11
void main()
{
float x[5],fx;
int i;
clrscr();
printf("Program for Bisection method");
printf("\n Enter two initial values:");
scanf("%f%f",&x[0],&x[1]);
x[2]=(x[0]+x[1])/2;
printf("\n\t\tx[0]\tx[1]\tx[2]\tF(x[2])\n");
while(fabs(x[0]-x[1])>0.0001)
{
fx=F(x[2]);
printf("\n\t\t%f %f %f %f",x[0],x[1],x[2],fx);
if(fx<0)
x[0]=x[2];
else
x[1]=x[2];
x[2]=(x[0]+x[1])/2;
}
printf("\n\t\t%f %f %f %f",x[0],x[1],x[2],fx);
printf("\nThe root of the given equation is %f",x[2]);
getch();
}


















OUTPUT:
Program for Bisection method
  Enter two initial values: 2
3
                    x[0]              x[1]              x[2]              F(x[2])
                    2.000000     3.000000     2.500000     2.125000
                    2.000000     2.500000     2.250000     -1.859375
                    2.250000     2.500000     2.375000     0.021484
                    2.250000     2.375000     2.312500     -0.946045
                    2.312500     2.375000     2.343750     -0.469147
                    2.343750     2.375000     2.359375     -0.225559
                    2.359375     2.375000     2.367188     -0.102471
                    2.367188     2.375000     2.371094     -0.040602
                    2.371094     2.375000     2.373047     -0.009586
                    2.373047     2.375000     2.374023     0.005942
                    2.373047     2.374023     2.373535     -0.001823
                    2.373535     2.374023     2.373779     0.002059
                    2.373535     2.373779     2.373657     0.000118
The  root  of  the  given  equation  is  2.373657





/*Newton – Raphson Method */

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define F(x) (x*x*x+2*x*x+10*x-20)
#define DF(x) (3*x*x+4*x+10)
void main()
{
int i;
float x[5],fx,dx,r,y;
clrscr();
printf("Newton Raphson Method");
printf("\nEnter the Initial value");
scanf("%f",&x[1]);
printf("\nF(x)\t\t\td(x)\t\t\tr\n");
do
{
fx=F(x[1]);
dx=DF(x[1]);
r=x[1]-(fx/dx);
printf("\n%f\t\t%f\t\t%f",fx,dx,r);
y=x[1];
x[1]=r;
}while(fabs(r-y)>0.00001);
printf("\nThe root of the equation %f",r);
getch();
}




















OUTPUT:
Newton Raphson Method
Enter the Initial value 2
F(x)                                 d(x)                                 r
16.000000                       30.000000                       1.466667
2.123852                         22.320000                       1.371512
0.057088                         21.129185                       1.368810
0.000044                         21.096165                       1.368808
The  root  of  the  equation  1.368808















/* Secant method */

#include<stdio.h>
#include<math.h>
#include<conio.h>
float f(float);
void main()
{
float z,x[50];
int k=0,i;
clrscr();
printf("Secant Method\n");
printf("\nF(x)=3*x-1-cos(x)");
printf("\nEnter the initial values:\n");
scanf("%f",&x[0]);
scanf("%f",&x[1]);
printf("\tx\t\tf(x)\n");
printf("\t%f\t%f\n",x[0],f(x[0]));
printf("\t%f\t%f\n",x[1],f(x[1]));
do
{
k++;
x[k+1]=(x[k-1]*f(x[k])-x[k]*f(x[k-1]))/(f(x[k])-f(x[k-1]));
printf("\t%f\t%f\n",x[k+1],f(x[k+1]));
}while(fabs(x[k+1]-x[k])>0.0001);
printf("\nThe root is %f",x[k+1]);
getch();
}
float f(float y)
{
return(3*y-1-cos(y));
}
















OUTPUT:
Secant Method

F(x)=3*x-1-cos(x)
Enter the initial values:
0
1
                    x                                      F(x)
                    0.000000                         -2.000000
                    1.000000                         1.459698
                    0.578085                         -0.103255
                    0.605959                         -0.004081
                    0.607105                         0.000014
                    0.607102                         -0.000000
The  root  is  0.607102









/* Gauss Elimination Method */

#include<stdio.h>
#include<conio.h>
void main()
{
float u,ne,a[50][50],i,j,k,x1[90],su;
int s;
clrscr();
printf("\n\n\n\n\t\t\t GAUSS ELIMINATION METHOD\n");
printf("\n\n\n\tEnter the no. of equations:");
scanf("%f",&ne);
printf("\n\n");
printf("\tEnter the elements        :   \n\n");
for(i=1;i<=ne;i++)
for(j=1;j<=ne+1;j++)
scanf("%f",&a[i][j]);
for(k=1;k<=ne-1;k++)
{
for(i=k+1;i<=ne;i++)
{
u=(a[i][k]/a[k][k]);
for(j=k;j<=ne+1;j++)
a[i][j]=(a[i][j]-(u*a[k][j]));
}
}
for(i=1;i<=ne;i++)
x1[i]=0.0;
for(i=ne;i>=1;i--)
{
su=0;
for(j=ne;j>=1;j--)
{
if(i!=j)
su=(su+(a[i][j]*x1[j]));
}
x1[i]=((a[i][ne+1]-su)/a[i][i]);
}
printf("\n\n\t THE RESULTANT VALUES IS \n");
for(s=1;s<=ne;s++)
printf("\n\t\tx[%d]=\t%f",s,x1[s]);
getch();
}





OUTPUT:
GAUSS ELIMINATION METHOD
          Enter  the  no.  of  equations: 3
          Enter  the  Elements         :
1                  -1                 1                  1
-3                 2                  -3                 -6
2                  -5                 4                  5

          THE  RESULTANT  VALUES  IS
                             
                              x[1]=           -2.000000
                              x[2]=           3.000000
                              x[3]=           6.000000









No comments: