Sei sulla pagina 1di 81

/* Assignment : 01*/

/*Program To Find Roots Of The Quadratic Equation Using Switch


Case.*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r,r1,r2,real,img;
int ch;
clrscr();
printf("\n\nEnter The Coefficients Of a,b & c:\t");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
r=-b/c;
printf("\nRoot=%f",r);
printf("\n\nIt Is A Linear Equation.\n");
}
else
{
d=b*b-4*a*c;
if(d==0)
ch=1;
else if(d>0)
ch=2;
else
ch=3;
switch(ch)
{
case 1: printf("\nRoots Are Equal.\n");
r1=-b/(2*a);
r2=r1;
printf("\nRoot1 = %.2f,\nRoot2 = %.2f.",r1,r2);
break;
case 2: printf("\nRoots Are Real & Distinct.\n");
r1=-b+sqrt(d)/(2*a);
r2=-b-sqrt(d)/(2*a);
printf("\nRoot1 = %f,\nRoot2 = %f.",r1,r2);
break;
case 3: printf("\nRoots Are Complex.\n");
real=-b/(2*a);
img=(sqrt(abs(d)))/(2*a);
printf("\nRoot1= %.2f+%.2fi,\n",real,img);
printf("\nRoot2= %.2f-%.2fi.",real,img);
break;
default:printf("\nInvalid Choice.");
}
}
getch();
}

/* Output*/

Enter The Coefficients Of a,b & c: 023

Root=-0.666667

It Is A Linear Equation.

Enter The Coefficients Of a,b & c: 123

Roots Are Complex.

Root1= -1.00+1.41i,

Root2= -1.00-1.41i.

Enter The Coefficients Of a,b & c: 8 12 0

Roots Are Real & Distinct.

Root1 = -11.250000,

Root2 = -12.750000.

Enter The Coefficients Of a,b & c: 8 12 0

Roots Are Real & Distinct.

Root1 = -11.250000,

Root2 = -12.750000.
/* Assignment : 02*/

/*Program To Find The Number Of Vowels,Consonants,Special


Character & White Spaces In Given String.*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,v=0,c=0,len=0,sp=0,wh=0;
char str[50];
clrscr();
printf("\nEnter The String.\t");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
str[i]=tolower(str[i]);
if(str[i]>='a' && str[i]<='z')
{
if(str[i]=='a' || str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u')
v++;
else
c++;
}
else if((str[i]==' ') || (str[i]=='\t'))
wh++;
else
sp++;
}
printf("\n\nNumber Of Vowels : %d",v);
printf("\n\nNumber Of Consonants : %d",c);
printf("\n\nNumber Of White Spaces : %d",wh);
printf("\n\nNumber Of Special Characters : %d",sp);
getch();
}
/*Output*/

Enter The String. abcDe *&^% jkg

Number Of Vowels : 2

Number Of Consonants : 6

Number Of White Spaces : 2

Number Of Special Characters : 4


/* Assignment : 03 */

/* Program To Find The Sum Of Digits Without Using % & / Operator


Until A Single Digit Is Obtained. */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,s=0,dig;
clrscr();
printf("Enter Any Number:\t");
scanf("%d",&n);
do
{
s=0;
while(n>0)
{
a=n*0.1;
dig=n-(a*10);
s=s+dig;
n=a;
}
printf("Sum Of Digits Is: %d\n",s);
n=s;
}while(n>9);
getch();
}

/* Output */

Enter Any Number: 456

Sum Of Digits Is: 15

Sum Of Digits Is: 6


/* Assignment : 04 */

/*Program To Find The Square Root,Cube Root & Fifth Root Of A


Number Without Using Built Function.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float s,c,f;
float sqr(int);
float cur(int);
float fr(int);
clrscr();
printf("\n\nEnter the Number:\t");
scanf("%d",&n);
s=sqr(n);
printf("\n\nSquare Root = %.3f",s);
c=cur(n);
printf("\n\nCube Root = %.3f",c);
f=fr(n);
printf("\n\nFifth Root = %f",f);
getch();
}
float sqr(int x)
{
float r=0.000001;
while((r*r)<=x)
r+=0.000001;
return r;
}
float cur(int x)
{
float r=0.000001;
while((r*r*r)<=x)
r+=0.000001;
return r;
}
float fr(int x)
{ float r=0.000001;
while((r*r*r*r*r)<=x)
r+=0.000001;
return r;
}
/* Output */

Enter the Number: 225

Square Root = 15.000

Cube Root = 6.082

Fifth Root = 2.954


/* Assignment : 05 */

/*Program For Addition, Subtraction & multiplication Of Matrices


Using Function.*/

#include<stdio.h>
#include<conio.h>
int m1,n1,m2,n2,i,j,k;
int a[20][20],b[20][20],c[20][20];
void read();
void add();
void sub();
void mul();
void disp();
void main()
{
clrscr();
printf("\nEnter The Order Of Matrix A:\t");
scanf("%d%d",&m1,&n1);
printf("\nEnter The Order Of Matrix B:\t");
scanf("%d%d",&m2,&n2);
read();
printf("\nMatrix A Is:\n\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nMatrix B Is:\n\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
if(m1==m2 && n1==n2)
{
printf("\nAddition Of A & B Is:\n\n");
add();
}
else
printf("\nAddition Not Possible.");

if(m1==m2 && n1==n2)


{
printf("\nSubtraction Of A & B Is:\n\n");
sub();
}
else
printf("\nSubtraction Not Possible.");
if(n1==m2)
{
printf("\nMultiplication Of A & B Is:\n\n");
mul();
}
else
printf("\nMultiplication Not Possible.");
getch();
}
void read()
{
printf("\nEnter The Elements Of Matrix A.");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&a[i][j]);
}

}
printf("\nEnter The Elements Of Matrix B.");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",&b[i][j]);
}

}
}
void add()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
disp();
}
void sub()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]-b[i][j];

}
}
disp();
}
void mul()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)

{
c[i][j]=0;
for(k=0;k<n1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
disp();
}
void disp()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

/* Output */

Enter The Order Of Matrix A: 2 2

Enter The Order Of Matrix B: 2 2

Enter The Elements Of Matrix A.1 2 3 4

Enter The Elements Of Matrix B.4 5 6 7


Matrix A Is:

1 2
3 4

Matrix B Is:

4 5
6 7

Addition Of A & B Is:

5 7
9 11

Subtraction Of A & B Is:

-3 -3
-3 -3

Multiplication Of A & B Is:

16 19
36 43
/* Assignment : 06 */

/*Program To Find First & Second Largest Number In 2 Arrays &


Find First & Second Largest Element In The Merged Array. */

#include<stdio.h>
#include<conio.h>
void larsma(int,int []);
int big,sbig;
void main()
{
int s,m,n,a[50],b[50],i,j;
clrscr();
printf("\n\nEnter The Size Of First Array.\t");
scanf("%d",&m);
printf("\n\nEnter The Elements Of First Array.\t");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
larsma(m,a);
printf("\n\nEnter The Size Of Second Array:\t");
scanf("%d",&n);
printf("\n\nEnter The Elements Of Second Array.\t");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
larsma(n,b);

for(i=m,j=0;j<n;i++,j++)
a[i]=b[j];
printf("\n\nThe Merged Array Is:\n");
for(i=0;i<m+n;i++)
printf("\t%d\n",a[i]);
s=m+n;
larsma(s,a);
getch();
}
void larsma(int m,int c[])
{ int i;
if(c[0]>c[1])
{
big=c[0];
sbig=c[1];
}
else
{
big=c[1];
sbig=c[0];
}
for(i=2;i<m;i++)
{
if(c[i]>big)
{
sbig=big;
big=c[i];
}
else if(c[i]>sbig)
sbig=c[i];
}
printf("\nFirst Largest Number = %d,Second Largest Number =
%d",big,sbig);
}

/* Output */

Enter The Size Of First Array. 5

Enter The Elements Of First Array. 10 20 30 40 50

First Largest Number = 50,Second Largest Number = 40

Enter The Size Of Second Array: 7

Enter The Elements Of Second Array. 60 80 100 110 120 1 2

First Largest Number = 120,Second Largest Number = 110

The Merged Array Is:


10
20
30
40
50
60
80
100
110
120
1
2

First Largest Number = 120,Second Largest Number = 110


/*Assignment : 07*/

/*Program To Convert Decimal Number To Binary, Octal &


Hexadecimal Number.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,p,i,j,bin[10],k,ch;
int value(int);
clrscr();
printf("\n\nEnter The Decimal Number.\t");
scanf("%d",&n);
printf("\n\n1.Decimal To Binary,\n\n2.Decimal To Octal,\n\n3.Decimal
To Hexadecimal.");
printf("\n\nEnter The Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\nDecimal To Binary Conversion:\t");
printf("\n\nBinary Equivalent = \t");
i=n;
j=0;
while(i>1)
{
p=i%2;
i=i/2;
bin[j]=p;
j++;
}
bin[j]=i;
for(k=j;k>=0;k--)
printf("%d",bin[k]);
break;
case 2: printf("\n\nDecimal To Octal Conversion:\t");
printf("\n\nOctal Equivalent =%o\n",n);
break;
case 3: printf("\n\nDecimal To Hexadecimal Conversion:\t");
printf("\n\nHexadecimal Equivalent= %x\n",n);
break;
default:printf("\n\nInvalid Choice.\t");
}
getch();
}
/* Output */

Enter The Decimal Number. 10

1.Decimal To Binary,

2.Decimal To Octal,

3.Decimal To Hexadecimal.

Enter The Choice: 1

Decimal To Binary Conversion:

Binary Equivalent = 1010

Enter The Decimal Number. 10

1.Decimal To Binary,

2.Decimal To Octal,

3.Decimal To Hexadecimal.

Enter The Choice: 2

Decimal To Octal Conversion:

Octal Equivalent =12

Enter The Decimal Number. 10

1.Decimal To Binary,

2.Decimal To Octal,

3.Decimal To Hexadecimal.

Enter The Choice: 3

Decimal To Hexadecimal Conversion:

Hexadecimal Equivalent= a
/* Assignment : 08 */

/* Program To Convert Binary, Octal & Hexadecimal Number


Into Decimal Number Using Case.*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void main()
{
int bin=0,i=0,r,n,ch,oct=0,j,k,len,dec=0,d;
char hexa[20];
clrscr();
printf("\n\n1.Binary To Decimal,\n\n2.Octal
ToDecimal,\n\n3.Hexadecimal To Decimal.");
printf("\n\nEnter The Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\nEnter The Number To Convert.\t");
scanf("%d",&n);
j=n;
r=n%10;
if(r==0 || r==1)
{
printf("\n\nConversion From Binary To Decimal.");
while(n>0)
{
r=n%10;
{
bin=bin+(r*pow(2,i));
i=i+1;
n=n/10;
}
}
printf("\n\nDecimal Equivalent Of %d = %d",j,bin);
}
else
printf("\n\nEnter Binary Number InTerms Of 0 &
1.");
break;
case 2: printf("\n\nEnter The Number To Convert.\t");
scanf("%d",&n);
j=n;
r=n%10;
if(r<0 || r>7)
printf("\n\nUnit Place Should Be Between 0 To
7.");
else
{
printf("\n\nConversion From Octal To Decimal.");
while(n>0)
{
r=n%10;
oct=oct+(r*pow(8,i));
i=i+1;
n=n/10;
}
printf("\nDecimal Number Of %d = %d",j,oct);
}
break;
case 3: printf("\nHexadecimal to Decimal conversion\n");
printf("\nEnter Hexadecimal number:");
scanf("%s",hexa);
k=strlen(hexa);
len=k;
for(i=0; i<len; i++)
{
if(isdigit(hexa[i]))
{
d=hexa[i]-48;
dec+=d*(pow(16,k-1));
}
else if(hexa[i]=='A' || hexa[i]=='a')
dec+=10*(pow(16,k-1));
else if(hexa[i]=='B' || hexa[i]=='b')
dec+=11*(pow(16,k-1));
else if(hexa[i]=='C' || hexa[i]=='c')
dec+=12*(pow(16,k-1));
else if(hexa[i]=='D' || hexa[i]=='d')
dec+=13*(pow(16,k-1));
else if(hexa[i]=='E' || hexa[i]=='e')
dec+=14*(pow(16,k-1));
else if(hexa[i]=='F' || hexa[i]=='f')
dec+=15*(pow(16,k-1));
k--;
}
printf("Decimal Eqiuvalent = %d",dec);
break;
default:printf("\n\nInvalid Choice.");
}
getch();
}
/* Output */

1.Binary To Decimal,

2.Octal To Decimal,

3.Hexadecimal To Decimal.

Enter The Choice: 1

Enter The Number To Convert. 1010

Conversion From Binary To Decimal.

Decimal Equivalent Of 1010 = 10

1.Binary To Decimal,

2.Octal To Decimal,

3.Hexadecimal To Decimal.

Enter The Choice: 2

Enter The Number To Convert. 12

Conversion From Octal To Decimal.

Decimal Number Of 12 = 10

1.Binary To Decimal,

2.Octal To Decimal,

3.Hexadecimal To Decimal.

Enter The Choice: 3

Hexadecimal to Decimal conversion

Enter Hexadecimal number:A

Decimal Eqiuvalent = 10
/*Assignment : 09 */

/* Program To Find The Sum Of Sine,Cosine And Exponential Series.


*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n,ch;
float sum,sign,f,x1,x;
clrscr();
printf("Enter the number of terms:\n");
scanf("%d",&n);
printf("Enter the degrees:\n");
scanf("%f",&x);
printf("\n Menu \n");
printf("1.Sum of cosine series\n");
printf("2.Sum of sine series\n");
printf("3.Sum of exponential series\n");
printf("Enter your choice:\t");
scanf("%d",&ch);
x1=x*(3.142/180.0);
switch(ch)
{
case 1: sum=1;
sign=-1;
for(i=2;i<=n;i=i+2)
{
f=1;
for(j=1;j<=i;j=j+1)
{
f=f*j;
}
sum=sum+(pow(x1,i)/f)*sign;
sign=sign*(-1);
}
printf("\nThe sum of cosine series is %8.4f:\n",sum);
printf("The value of cos(x) using library function=
%8.4f\n",sum);
break;
case 2: sum=x1;
sign=-1;
for(i=3;i<=n;i=i+2)
{
f=1;
for(j=1;j<=i;j=j+1)
{
f=f*j;
}
sum=sum+(pow(x1,i)/f)*sign;
sign=sign*(-1);
}
printf("\nThe sum of sine series is %8.4f\n",sum);
printf("The value of sine(x) using library function=
%8.4f\n",sum);
break;
case 3: sum=1;
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
sum=sum+(pow(x1,i)/f);
}
printf("\nThe sum of exponential series is
%8.4f:\n",sum);
printf("The value of exp(x) using library function=
%8.4f\n",sum);
break;
}
getch();
}
/* Output */

Enter the number of terms:


5
Enter the degrees:
60

Menu
1.Sum of cosine series
2.Sum of sine series
3.Sum of exponential series
Enter your choice: 1

The sum of cosine series is 0.5017:


The value of cos(x) using library function= 0.5017

Enter the number of terms:


5
Enter the degrees:
60

Menu
1.Sum of cosine series
2.Sum of sine series
3.Sum of exponential series
Enter your choice: 2

The sum of sine series is 0.8664


The value of sine(x) using library function= 0.8664
Enter the number of terms:
5
Enter the degrees:
60

Menu
1.Sum of cosine series
2.Sum of sine series
3.Sum of exponential series
Enter your choice: 3

The sum of exponential series is 2.8479:


The value of exp(x) using library function= 2.8479
/* Assignment : 10 */

/* Program Accept 'N' Numbers And Print The Numbers In Wordings.


*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char w1[20][10]={"ZERO","ONE","TWO","THREE","FOUR","FIVE",
"SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN",
"TWELVE","THIRTEEN","FOURTEEN","FIFTEEN",
"SIXTEEN","SEVENTEEN","EIGHTEEN","NINTEEN"};
char w2[10]
[10]={"ZERO","TEN","TWENTY","THIRTY","FOURTY",
"FIFTY","SIXTY","SEVENTY","EIGHTY","NINTY"};
int n,r;
clrscr();
printf("\n Enter the number between 0 to 10000:\t");
scanf("%d",&n);
if((n>=0) && (n<=10000))
{
if((n<=10000) && (n>=1000))
{
r=n/1000;
printf("%s THOUSAND",w1[r]);
n%=1000;
}
if((n<=1000) && (n>=100))
{
r=n/100;
printf("%s HUNDRED",w1[r]);
n%=100;
}
if((n<=100) && (n>=20))
{
r=n/10;
printf(" %s",w2[r]);
n%=10;
}
if((n<=20)&&(n>0))
{
r=n;
printf("%s",w1[r]);
}
}

else
printf("\n Enter Number With In The Range.\n");
getch();
}

/* Output */

Enter the number between 0 to 10000: 1000

ONE THOUSAND

Enter the number between 0 to 10000: -100

Enter Number With In The Range.


/* Assignment : 11 */

/* Program To Convert Given Number Into Equivalent Roman


Number(Upto 3000) */

#include<stdio.h>
#include<conio.h>
void main()
{
char a1[20]
[25]={"0","I","II","III","IV","V","VI","VII","VIII","IX","X","XI",
"XII","XIII","XIV","XV","XVI","XVII","XVIII","XIX"};
char a2[10]
[15]={"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
char a3[10]
[15]={"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
char a4[5][5]={"0","M","MM","MMM"};
int n,a;
clrscr();
x:printf("Enter Any Number:\t");
scanf("%d",&n);
if(n>3000 || n<0)
{
printf("Enter Number Between 0 to 3000.\n");
goto x;
}
else
{

if(n==0)
{
a=n;
printf("\n\nRoman Equivalent : %s\n",a4[a]);
}
else
{
if(n>=1000)
{
a=n/1000;
printf("\n\nRoman Equivalent : %s\n",a4[a]);
n=n-(a*1000);
}
if(n>=100)
{
a=n/100;
printf("\n\nRoman Equivalent : %s\n",a3[a]);
n=n-(a*100);
}
if(n>=20)
{
a=n/10;
printf("\n\nRoman Equivalent : %s\n",a2[a]);
n=n-(a*10);
}
if(n>0)
{
a=n;
printf("\n\nRoman Equivalent : %s\n",a1[a]);
}
}
}
getch();
}

/* Output */

Enter Any Number: 100

Roman Equivalent : C

Enter Any Number: -100

Enter Number Between 0 to 3000.

Enter Any Number: 100

Roman Equivalent : C

/* Assignment : 12 */
/* Program To Perform Addition, Subtraction & Multiplication Of
Complex Numbers Using Structure*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
struct comp
{
float re,im;
};
struct comp x,y,z,sum,dif,mul;
void main()
{
clrscr();
printf("\n Enter The Real Part And Imaginary Part Of X:\t");
scanf("%f%f",&x.re,&x.im);
printf("Enter The Real Part And Imaginary Part Of Y:\t");
scanf("%f%f",&y.re,&y.im);
printf("\n The Given Complex Numbers Are:\n");
printf("%4.2f+i%4.2f",x.re,x.im);
printf("\n %4.2f+i%4.2f",y.re,y.im);
sum.re=x.re+y.re;
sum.im=x.im+y.im;
printf("\n Sum=%4.2f+i%4.2f",sum.re,sum.im);
dif.re=x.re-y.re;
dif.im=x.im-y.im;
printf("\n Difference=%4.2f+i%4.2f",dif.re,dif.im);
mul.re=(x.re*y.re)-(x.im*y.im);
mul.im=(x.re*y.im)+(y.re*x.im);
printf("\n Product=%4.2f+i%4.2f",mul.re,mul.im);
getch();
}

/* Output */
Enter The Real Part And Imaginary Part Of X: 2 3

Enter The Real Part And Imaginary Part Of Y: 2 1

The Given Complex Numbers Are:

2.00+i3.00
2.00+i1.00

Sum=4.00+i4.00

Difference=0.00+i2.00

Product=1.00+i8.00

/* Assignment : 13 */
/* Program To Generate Fibonacci & Non-fibonacci Series Of 'N'
Terms Using Case.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f1,f2,f3,ch,j,i;
clrscr();
printf("\nEnter The N Value:\t");
scanf("%d",&n);
printf("\n\n1.Fibonacci Series,\n\n2.Nonfibonacci Series.\n");
printf("\nEnter The Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Fibonacci Series:\n");
f1=f3=0;f2=1;
printf("%d\n%d",f1,f2);
for(i=3;i<=n;i++)
{
f3=f2+f1;
printf("\n%d",f3);
f1=f2;
f2=f3;
}
break;
case 2: printf("\nNonfibonacci Series:\n");
f1=0;f2=1;f3=0;
for(i=0,j=0;i<n;j++)
{
while((j!=f3) && (i<n))
{
printf("\n%d",j);
i++;
j++;
}
f3=f1+f2;
f1=f2;
f2=f3;
}
break;
default: printf("\nInvalid Choice.");
}
getch();
}

/* Output */
Enter The N Value: 6

1.Fibonacci Series,

2.Nonfibonacci Series.

Enter The Choice: 1

Fibonacci Series:
0
1
1
2
3
5

Enter The N Value: 5

1.Fibonacci Series,

2.Nonfibonacci Series.

Enter The Choice: 2

Nonfibonacci Series:

4
6
7
9
10

/* Assignment : 14 */
/* Program To Find Row Sum, Column Sum, Primary & Secondary
Diagonal Sum Of A Square Matrix. */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10][10],i,j,r,c,rsum=0,csum=0,psum=0,ssum=0;
clrscr();
printf("\n Enter The Order Of Matrix:\t");
scanf("%d%d",&r,&c);
printf("\n\nEnter The Element Of Matrix:\t");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\nElement Are:\n\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n\n");
}
for(i=1;i<=r;i++)
{
rsum=0;
for(j=1;j<=c;j++)
rsum=rsum+a[i][j];
printf("\n\n Sum Of %d Row Is : %d",i,rsum);
}
for(i=1;i<=c;i++)
{
csum=0;
for(j=0;j<=r;j++)
csum=csum+a[j][i];
printf("\n\n Sum Of %d Column Is:%d",i,csum);
}

if(r!=c)
{
printf("\n\nPrimary & Secondary Diagonal Sum is Not Possible!");
getch();
exit();
}
else
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
pd+=a[i][j];
}
}
}
}
printf("\n\nPrincipal Diagonal Sum Is: %d",pd);
for(i=0;i<r;i++)
{
sd+=a[i][r-i-1];
}
printf("\n\nSecondary Diagonal Sum Is: %d",sd);
getch();
}

/* Output */
Enter The Order Of Matrix: 33

Enter The Element Of Matrix: 123456789

Element Are:

1 2 3

4 5 6

7 8 9

Sum Of 1 Row Is : 6

Sum Of 2 Row Is : 15

Sum Of 3 Row Is : 24

Sum Of 1 Column Is:12

Sum Of 2 Column Is:15

Sum Of 3 Column Is:19

Sum Of Primary Diagonal Element Is: 15

Sum Of Secondary Diagonal Element Is 15

/* Assignment : 15 */
/*Program To Compute Difference Between Two Dates*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter First Day, Month, Year\nYou Can Use SPACE
Instead/:\t");
scanf("%d%d%d",&day1,&mon1,&year1);
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2<year1)
ref = year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i<year1;i++)
{
if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("\n\nNo. Of Days Of First Date From The Jan 1 %d=
%d\n",year1,dd1);
dd2=0;
for(i=ref;i<year2;i++)
{
if(i%4==0)
dd2+=1;
}
dd2=func1(mon2)+dd2+day2+((year2-ref)*365);
printf("\n\nNo. Of Days From The Reference Year's First Jan =
%d\n",dd2);
printf("\n\nTherefore, Difference Between The Two Dates Is %d Days
",abs(dd2-dd1));
getch();
}

int func1(x)
{
int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120;break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Error encountered"); exit(1);
}
return(y);
}

/* Output */

Enter First Day, Month, Year


You Can Use SPACE Instead/: 21 9 2008
21 10 2008

No. Of Days Of First Date From The Jan 1 2008= 264

No. Of Days From The Reference Year's First Jan = 294

Therefore, Difference Between The Two Dates Is 30 Days

/* Assigment : 16 */
/*Program to Convert Given String To Upper Case & Check For It Is
Palindrome Or Not Without Using Built-in Function.*/

#include<stdio.h>
#include<conio.h>
void main()
{
char a[80],rev[30]={'\0'};
int i,len=0,flag=0;
clrscr();
printf("\n\nInput The String.\t");
gets(a);
for(i=0;a[i]!='\0';i++)
{
len++;
if(a[i]>='a' && a[i]<='z')
a[i]=a[i]-32;
}
for(i=len-1;i>=0;i--)
{
rev[len-i-1]=a[i];
}
for(i=0;i<=len;i++)
{
if(rev[i]==a[i])
flag=1;
else
{
flag=0;
break;
}
}
printf("\n\nConverted String Is:%s\t",a);
if(flag==1)
printf("\n\n%s Is A Palindrome.\n",a);
else
printf("\n\n%s Is Not A Palidrome.\n",a);
getch();
}

/* Output */
Input The String. gadag

Converted String Is: GADAG

GADAG Is A Palindrome.

Input The String. hubli

Converted String Is: HUBLI

HUBLI Is Not A Palindrome.

/* Assignment : 17 */
/* Program To Find Factorial Using Recursive & Non Recursive
Function */

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,fact1=1,ch;
int fact(int);
clrscr();
printf("\nEnter Number:\t");
scanf("%d",&n);
printf("\n\n1.Factorial Using Recursive function,\n2.Factorial Using
Non Recursive Function.");
printf("Enter The Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nFactorial Of %d Using Recursive Function =
%d",n,fact(n));
break;
case 2:while(i<=n)
{
fact1*=i;
i++;
}
printf("\n\nFactorial Of %d Without Using Recursive
Function = %d",n,fact1);
break;
default:printf("Invalid Choice.");
}
getch();
}
int fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}

/* Output */
Enter Number: 6

1.Factorial Using Recursive function,


2.Factorial Using Non Recursive Function.

Enter The Choice: 1

Factorial Of 6 Using Recursive Function = 720

/* Assignment : 18 */
/*Program For Computing Area & Perimeter Of Rectangle Using
Function & Switch Case*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
float a,b,c,s,area,peri,vol;
void area1(float,float);
void read();
void perimeter(float,float);
void main()
{
int ch;
clrscr();
printf("\t\n1.Area,\n\n2.Perimeter.\t");
printf("\t\nEnter The Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nEnter 2 Sides Of Rectangle:\t");
read();
area1(a,b);
break;
case 2: printf("\n\nEnter 2 Sides To Calculate Perimeter:\t");
read();
perimeter(a,b);
break;
default:printf("\n\nInvalid choice.");
}
getch();
}
void read()
{
scanf("%f%f",&a,&b);
}
void area1(float a,float b)
{
area=a*b;
printf("\n\nArea Of Rectangle = %.2f",area);
}

void perimeter(float a,float b)


{
peri=2*(a+b);
printf("\n\nPerimeter Of Rectangle = %.2f",peri);
}

/* Output */

1.Area,

2.Perimeter.

Enter The Choice: 1

Enter 2 Sides Of Rectangle: 12 12

Area Of Rectangle = 144.00

1.Area,

2.Perimeter.

Enter The Choice. 2

Enter 2 Sides To Calculate Perimeter: 12 12

Perimeter Of Rectangle = 48.00

/* Assignment : 19 */
/* Program To Find The Determinant Of The Matrix.*/

#include<stdio.h>
#include<conio.h>
int i,j,k,m,n,l;
float a[10][10],par, psum, nsum, sum=0,b[20][20];
void disp();
void main()
{
clrscr();
printf("\n\nEnter The Order Of The Matrix:\t");
scanf("%d%d",&m,&n);
if(m!=n)
printf("\n\nEnter Square Matrix Order.");
else
{
printf("\n\nEnter The Elements Of The Matrix:\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("\n\nEntered Matrix Is:\n\n");
disp();
if(m<3 && n<3)
sum=a[0][0]*a[1][1]-a[0][1]*a[1][0];
else
{
for(k=0;k<m;k++)
{
for(l=0;l<n;l++)
b[k][l]=a[k][l];
}
for(k=0;k<n;k++)
{
for(l=n;l<(2*n-1);l++)
b[k][l]=b[k][l-n];
}
for(j=0;j<n;j++)
{
par=1;
for(i=0;i<m;i++)
par*=b[i][i+j];
psum+=par;
}
for(j=n-1;j<(2*n-1);j++)
{
par=1;
for(i=0;i<n;i++)
par*=b[i][j-i];
nsum+=par;
}
sum=psum-nsum;
}

}
printf("Determinant = %.2f",sum);
getch();
}
void disp()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%.2f\t",a[i][j]);
}
printf("\n");
}
}

/* Output */

Enter The Order Of The Matrix: 3 3

Enter The Elements Of The Matrix: 91 92 93 94 95 96 97 98 99

Entered Matrix Is:

91.00 92.00 93.00


94.00 95.00 96.00
97.00 98.00 99.00

Determinant = 0

/* Assignment: 20 */
/* Program To Create A Structure Within The Structure With Data
Fields Emp Number, Emp Name, Date Of Birth And Basic Salary*/

#include<stdio.h>
#include<conio.h>
typedef struct
{
int day,mon,year;
}date;
typedef struct
{
int no;
char name[20];
date dob;
float bs;
}info;
info emp[10];
void main()
{
int n,i;
float da[10],hra[10],grossal[10];
clrscr();
printf("\nHow many employes:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the number :\t");
scanf("%d",&emp[i].no);
printf("\nEnter the name :\t");
scanf("%s",&emp[i].name);
printf("\nEnter the date of birth :\t");
scanf("%d%d%d",&emp[i].dob.day,&emp[i].dob.mon,
&emp[i].dob.year);
printf("\nEnter the basic salary: \t");
scanf("%f",&emp[i].bs);
if(emp[i].bs<=5000)
da[i]=5000*.1;
else if(emp[i].bs>5000 && emp[i].bs<=10000)
da[i]=500+(.15*emp[i].bs);
else if(emp[i].bs>1000)
da[i]=1250+(.2*emp[i].bs);
hra[i]=.25*emp[i].bs;
grossal[i]=emp[i].bs+da[i]+hra[i];
}

printf("Emp No\t Name\t DOB\t Basic Salary\t DA\t HRA\t


Grossal\n");
printf("--------------------------------------------------------------------------------\n
");
for(i=1;i<=n;i++)
printf("%4d %9s %2d-%d-%2d %10.2f %10.2f %12.2f
%10.2f\n",emp[i].no,
emp[i].name,emp[i].dob.day,emp[i].dob.mon,emp[i].dob.year,
emp[i].bs,da[i],hra[i],grossal[i]);
} getch();

/* Output */

How many employes: 1

Enter the number : 12

Enter the name : abc

Enter the date of birth : 1 2 1960

Enter the basic salary: 10000

Emp No Name DOB Basic Salary DA HRA Grossal


-------------------------------------------------------------------------------------

12 abc 1-2-1960 10000.00 2000.00 2500.00 14500.00

/* Assignment: 21 */
/*Program To Dispaly The Student Record Using Structure By Passing
The Strucure To The Function. */

#include<stdio.h>
#include<conio.h>
typedef struct
{
char name[25];
int rno;
float m1,m2,m3;
}stud;

void main()
{
stud s[10];
int i,n;
void read(stud s[],int),printline(char,int),display(stud s[],int);
clrscr();
printf("How Many Student:\t");
scanf("%d",&n);
printf("\n Entre The Student Information:\t");
read(s,n);
clrscr();
printf("\n\t\t*****\t\tStudent Information \t\t*****\n");
printline('-',75);
printf("Roll number\t Name\t\t Marks\t\t Average\n");
printline('-',75);
display(s,n);
printline('=' ,75);
getch();
}
void read(stud s[10],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("\n Enter The Name Of Student %d:",i);
scanf("%s",s[i].name);
printf("\n Enter The Roll Number :");
scanf("%d",&s[i].rno);
printf("\n Enter The Marks Of Three Subjects\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
}
}

void display(stud s[10],int n)


{
int i;
float avg[10];
for(i=1;i<=n;i++)
{
avg[i]=(s[i].m1+s[i].m2+s[i].m3)/3.0;
printf("%5d%15s\t%6.2f%6.2f%6.2f\t%5.2f\n",s[i].rno,s[i].name,
s[i].m1,s[i].m2,s[i].m3,avg[i]);
}
}
void printline(char c,int n)
{
int i;
for(i=0;i<n;i++)
printf("%c",c);
printf("\n");
}

/* Output */

***** Student Information *****


--------------------------------------------------------------------------
Roll number Name Marks Average
--------------------------------------------------------------------------
65 Diya 80.00 85.00 83.00 82.67
4 Nisha 85.00 88.00 89.00 87.33
============================================

/*Assignment : 22*/
/*Program To Find GCD & LCM Of More Than Two Numbers Using
Recursive Function*/

#include<stdio.h>
#include<conio.h>
int fgcd(int,int);
int flcm(int,int);
void main()
{
int i,n,a[10],gcd,lcm;
clrscr();
printf("Enter The Range:\t");
scanf("%d",&n);
printf("\nEnter %d Numbers:\t",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
gcd=fgcd(a[0],a[1]);
lcm=flcm(a[0],a[1]);
for(i=2;i<n;i++)
{
gcd=fgcd(gcd,a[i]);
lcm=flcm(lcm,a[i]);
}
printf("\nGCD=%d\n\nLCM=%d",gcd,lcm);
getch();
}
int fgcd(int u,int v)
{
if(v==0)
return u;
else
return(fgcd(v,u%v));
}
int flcm(int u,int v)
{
int lcm;
lcm=(u*v)/fgcd(u,v);
return lcm;
}

/* Output */
Enter The Range: 4

Enter 4 Numbers: 2468

GCD=2

LCM=24

/* Assignment : 23 */
/* Program To Generate Pascal Triangle. */
#include<stdio.h>
#include<conio.h>
void main()
{
int m=0,n,x,i,binom;
clrscr();
printf("Enter The Number Of Terms:\t");
scanf("%d",&n);
do
{
x=0;
binom=1;
for(i=n;i>m;i--)
printf(" ");
while(x<=m)
{
if(m==0||x==0)
printf("%4d",binom);
else
{
binom=binom*(m-x+1)/x;
printf("%4d",binom);
}
x++;
}
printf("\n\n");
m++;
}
while(m<=n);
getch();
}

/* Output */
Enter The Number Of Terms: 5
1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

/* Assignment : 24 */
/* Program to print the primes, odd, even, perfect squares */

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

void main()
{
int i,n,sqr,j,flag=0;
clrscr();
printf("Enter a limit.:\t");
scanf("%d",&n);
if(n<=1)
{
printf("\nInvalid Limit!");
exit(1);
}
printf("\nPrime Numbers are:\n");
for(i=1;i<=n;i=i+2)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\n",i);
}
}
printf("\nPerfect Squares Are:\n");
for(i=1;i<=n;i++)
{
sqr=sqrt(i);
if(pow(sqr,2)==i)
printf("%d\n",i);
}
printf("\n Odd Numbers\tEven Numbers\n");
for(i=1;i<=n;i++)
{
if(i%2==0)
printf("%d\n",i);
else
printf("%d\t\t\t",i);
}
getch();
}

/* Output */

Enter a limit.: 10

Prime Numbers are:


1
3
5
7

Perfect Squares Are:


1
4
9

Odd Numbers Even Numbers


1 2
3 4
5 6
7 8
9 10

/* Assignment : 25 */
/* Program of using Command line arguments to copy the content of
one file into another */

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

void main(int argc, char *argv[])


{
FILE *fs,*ft;
char ch;

if(argc!=3)
{
printf("destination file name missing");
exit();
}

fs=fopen(argv[1],"r");
if(fs==NULL)
{
puts("cannot open source file");
exit();
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
puts("cannot open target file");
fclose(fs);
exit();
}

while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
putc(ch,ft);
}

fclose(fs);
fclose(ft);
}

/* Output */
C:\TC\BIN>cmd1 source.c dest.c

C:\TC\BIN>type source.c
asdgfyughvjn;oivjnvodfivheiogoi
C:\TC\BIN>type dest.c
asdgfyughvjn;oivjnvodfivheiogoi

/* Assignment : 26 */
/* Program to print the table of BINOMIAL CO-EFFICIANT
for m = 10 */

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

#define MAX 10

void main()
{
int m,x,binom;
clrscr();
printf("mx |");
for(m=0;m<=10;++m)
{
printf("%4d",m);
}
printf("\n--------------------------------------------------\n");
m=0;
do
{
printf("\n%2d |",m);
x=0;binom=1;
while(x<=m)
{
if(m==0 || x==0)
printf("%4d",binom);
else
{
binom=binom*(m-x+1)/x;
printf("%4d",binom);
}
x=x+1;
}
printf("\n");
m=m+1;
}while(m<=MAX);
printf("\n---------------------------------------------------\n |");
getch();
}

/* Output */
mx | 0 1 2 3 4 5 6 7 8 9 10
-----------------------------------------------------

0| 1

1| 1 1

2| 1 2 1

3| 1 3 3 1

4| 1 4 6 4 1

5 | 1 5 10 10 5 1

6 | 1 6 15 20 15 6 1

7 | 1 7 21 35 35 21 7 1

8 | 1 8 28 56 70 56 28 8 1

9 | 1 9 36 84 126 126 84 36 9 1

10 | 1 10 45 120 210 252 210 120 45 10 1

-------------------------------------------------------

/* Assignment: 27 */
/* Program to create a data file with student information name, roll
number & marks of 3subjects & read the file created to display the
student result */

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

void main()
{
int n,i,rn=0,c=0,coa=0,dc=0;
float avg=0.0,tot=0.0;
FILE *fp1,*fp2;
char name[20],file1[25],file2[25];
clrscr();
printf("\n\n\tEnter the number of students: ");
scanf("%d",&n);
printf("\n\n\tEnter the input file name: ");
scanf("%s",file1);
fp1=fopen(file1,"w");
printf("\n\n\tInput the student record\n");
for(i=0;i<n;i++)
{
printf("\n\n\tEnter the name of the %d student: ",i+1);
scanf("%s",name);
printf("\n\n\tEnter the roll number of %d student: ",i+1);
scanf("%d",&rn);
printf("\n\n\tEnter the C,COA & DC subject marks of %d
student: ",i+1);
scanf("%d%d%d",&c,&coa,&dc);
fprintf(fp1,"%s\n%d\n%d\n%d\n%d",name,rn,c,coa,dc);
}
fclose(fp1);
fp1=fopen(file1,"r");
fp1=fopen(file1,"r");
printf("\n------------------------- %s --------------------------",file1);
printf("\n\n\tName\tRollnumber\tC\tCOA\tDC");
for(i=0;i<n;i++)
{
fscanf(fp1,"\n\t%s\n%d\n%d\n%d\n
%d\n",name,&rn,&c,&coa,&dc);
printf("\n\t%s\t%d\t%d\t%d\t%d",name,rn,c,coa,dc);
}
printf("\n-----------------------------------------------------");
fclose(fp1);
printf("\n\n\tEnter the output file name: ");
scanf("%s",file2);
fprintf(stdout,"\n\n");
fp2=fopen(file2,"w");
fp1=fopen(file1,"r");
printf("\n=================== %s
===================",file2);

printf("\n\nName\tRollNumber\tC\tCOA\tDC\tTOTAL\tAVERAGE\n");
for(i=0;i<n;i++)
{
fscanf(fp1,"\n%s\n%d\n%d\n%d\n
%d\n",name,&rn,&c,&coa,&dc);
tot=c+coa+dc;
avg=(tot)/3;
fprintf(fp2,"\n%s\t%d\t%d\t%d\t%d\t%.2f\t
%.2f",name,rn,c,coa,dc,tot,avg);
printf("\n%s\t%d\t%d\t%d\t%d\t%.2f\t
%.2f",name,rn,c,coa,dc,tot,avg);

}
fclose(fp1);
fclose(fp2);

getch();
}

/* Output */

Enter the number of students: 3

Enter the input file name: student

Input the student record

Enter the name of the 1 student: Swarit

Enter the roll number of 1 student: 1

Enter the C,COA & DC subject marks of 1 student: 99 77 88

Enter the name of the 2 student: Rajendra

Enter the roll number of 2 student: 2


Enter the C,COA & DC subject marks of 2 student: 88 77 98

Enter the name of the 3 student: Pradnya

Enter the roll number of 3 student: 3

Enter the C,COA & DC subject marks of 3 student: 78 87 97

------------------------- student --------------------------

Name Rollnumber C COA DC


Swarit 1 99 77 88
Rajendra 2 88 77 98
Pradnya 3 78 87 97
-------------------------------------------------------------

Enter the output file name: result

=================== result ===================

Name RollNumber C COA DC TOTAL AVERAGE

Swarit 1 99 77 88 264.00 88.00


Rajendra 2 88 77 98 263.00 87.67
Pradnya 3 78 87 97 262.00 87.33

/* Assignment: 28*/
/* Program to read the customer's no., power consumed & print the
amount to be paid by the customer */

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

void main()
{
int units,custno;
float charges;
clrscr();
printf("\n\n\tEnter the customer's number: ");
scanf("%d",&custno);
printf("\n\n\tEnter the units consumed: ");
scanf("%d",&units);
if(units<=200)
charges=0.5*units;
else
if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.80*(units-400);
else
charges=390+(units-600);
printf("\n\n\t\tCustomer's number: %d\n\n\t\tUnits Consumed:
%d\n\n\t\tCharges Applied: %.2f",custno,units,charges);
getch();
}

/* Output */

Enter the customer's number: 32

Enter the units consumed: 786

Customer's number: 32

Units Consumed: 786

Charges Applied: 576.00

/* Assignment: 29 */
/* Program to print an addition modulo table of a given number */

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

void main()
{
int row,col,sum[20][20];
int i,j,n;
clrscr();
printf("\n\n\tEnter The Limit Of Addition Modulo Table: ");
scanf("%d",&n);
printf("\n\n\tThe Addition Modulo Table\n\n\n");
printf(" ");
for(j=0;j<n;j++)
printf("%5d",j);
printf("\n-----------------------------\n");
for(i=0;i<n;i++)
{
row=i;
printf("\n%2d | ",row);
for(j=0;j<n;j++)
{
col=j;
sum[i][j]=row+col;
if(sum[i][j]>=n)
sum[i][j]-=n;
printf("%4d",sum[i][j]);
}
printf("\n");
}
getch();
}

/* Output */
Enter The Limit Of Addition Modulo Table: 5

The Addition Modulo Table

0 1 2 3 4
-----------------------------

0| 0 1 2 3 4

1| 1 2 3 4 0

2| 2 3 4 0 1

3| 3 4 0 1 2

4| 4 0 1 2 3
/*Assignment : 30*/

/*Program to sort the given set of numbers by bubble sort technique using
function.*/

#include<stdio.h>
#include<conio.h>
void bubble(int a[],int n)
{
int i,j,t;
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(a[i]>=a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
}
void main()
{
int i,n,a[20];
clrscr();
printf("enter limit : ");
scanf("%d",&n);
printf("\n\nenter elements for sorting : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a,n);
printf("\n\nsorted array :\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Output :

Enter limit : 5

Enter elements for sorting : 3 7 1 9 0

Sorted array :

0 1 3 7 9
/*Assignment : 31*/

/*Program to sort a list of n elements of integer type using the Quick sort
algorithm.*/

#include<stdio.h>
#include<conio.h>
int binary(int k,int a[],int l,int h)
{
int m;
if(l>h)
return 0;
m=(l+h)/2;
if(k==a[m])
return m;
else if (k<a[m])
return(binary(k,a,l,m-1));
else
return(binary(k,a,m+1,h));
}
int partition(int a[],int l,int h)
{
int i,j,k,t;
k=a[l];
i=l+1;
j=h;
while(1)
{
while(i<h && k>=a[i])
i++;
while(k<a[j])
j--;
if(i<j)
t=a[i];
a[i]=a[j];
a[j]=t;
else
{
t=a[l];
a[l]=a[j];
a[j]=t;
return j;
}
}
}
void quick(int a[],int l,int h)
{
int j;
if(l<h)
{
j=partition(a,l,h);
quick(a,l,j-1);
quick(a,j+1,h);
}
}
void main()
{
int i,k,n,a[20],p;
clrscr();
printf("enter limit : ");
scanf("%d",&n);
printf("\n\nenter elements for sorting : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\nenter the element to be serched\n");
scanf("%d",&k);
quick(a,0,n-1);
printf("\n\nsorted array :\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n\nafter sorting :\n");
p=binary(k,a,0,n-1);
if(p==0)
printf("\n\n%d is not found in the given array\n",k);
else
printf("\n\n%d is found at %d
positin in the given array\n",k,p);
getch();
}

Output :

enter limit : 5

enter elements for sorting : 5 3 88 04 2

enter the element to be searched


88

sorted array:

2 3 4 5 88

after sorting :

88 is found at 4 position in the given array


/*Assignment : 32*/

/*Program to sort the given n numbers by using Shell sort technique.*/

#include<stdio.h>
#include<conio.h>
void shell(int a[],int n)
{
int i,j,k,item;
for(k=(n-1)/2;k>0;k/=2)
{
for(i=0;i<n;i+=k)
{
item=a[i];
j=i-k;
while(j>=0 && item<a[j])
{
a[j+k]=a[j];
j=j-k;
}
a[j+k]=item;
}
}
}
void main()
{
int i,n,a[20];
clrscr();
printf("enter limit : ");
scanf("%d",&n);
printf("\n\nenter elements for sorting : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
shell(a,n);
printf("\n\nsorted array :\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Output :

Enter limit : 5

Enter elements for sorting : 3 7 1 9 0

Sorted array :

0 1 3 7 9
/*Assignment : 33*/

/*Program to sort the list of elements by using Insertion sort technique.*/

#include<stdio.h>
#include<conio.h>
void insertion(int a[],int n)
{
int i,j,item;
for(i=0;i<n;i++)
{
item=a[i];
j=i-1;
while(j>=0 && item<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=item;
}
}
void main()
{
int i,n,a[20];
clrscr();
printf("enter limit : ");
scanf("%d",&n);
printf("\n\nenter elements for sorting : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertion(a,n);
printf("\n\nsorted array :\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Output :

Enter limit : 5

Enter elements for sorting : 3 7 1 9 0

Sorted array :

0 1 3 7 9
/*Assignment : 34*/

/*Program to sort the list of elements by using Selection sort technique.*/

#include<stdio.h>
#include<conio.h>
void selection(int a[],int n)
{
int i,j,pos,small,t;
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
t=a[pos];
a[pos]=a[i];
a[i]=t;
}
}
void main()
{
int i,n,a[20];
clrscr();
printf("enter limit : ");
scanf("%d",&n);
printf("\n\nenter elements for sorting : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a,n);
printf("\n\nsorted array :\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Output :

Enter limit : 5

Enter elements for sorting : 3 7 1 9 0

Sorted array :

0 1 3 7 9
/*Assignment : 35*/

/*Program to calculate the standard deviation of n items.*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float a,m,x[20],sum=0.0,sum1=0.0,var,sd;
clrscr();
printf("enter limit :\n\n");
scanf("%d",&n);
printf("\nenter %d items\n\n",n);
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
sum+=x[i];
}
m=sum/n;
printf("\nmean = %.2f\n",m);
for(i=1;i<=n;i++)
{
a=(x[i]-m);
sum1+=a*a;
}
var=sum1/n;
printf("\nvariance = %.2f\n",var);
sd=sqrt(var);
printf("\nstandard deviation = %.2f\n",sd);
getch();
}

Output :

enter limit :
3
enter 3 items
5
4
2

mean = 3.67

variance = 1.56

standard deviation = 1.25


/*Assignment : 36*/

/*Program to calculate the area under a curve.*/

#include<stdio.h>
#include<conio.h>
float start_pt,end_pt,total_area;
int numtraps;
void input(void)
{
printf("\nenter lower limit:");
scanf("%f",&start_pt);
printf("\nenter upper limit:");
scanf("%f",&end_pt);
printf("\nenter number of trapezoids:");
scanf("%d",&numtraps);
}
float trap_area(float ht1,float ht2,float base)
{
float area;
area=0.5*(ht1+ht2)*base;
return (area);
}
float fun_x(float x)
{
return(x*x+1);
}
float find_area(float a, float b, int n)
{
float base,low,h1,h2;
base=(b-1)/n;
low=a;
for(low=a;low<=b-base;low=low+base)
{
h1=fun_x(low);
h2=fun_x(low+base);
total_area+=trap_area(h1,h2,base);
}
return (total_area);
}
void main()
{
clrscr();
printf("AREA UNDER A CURVE\n");
input();
total_area=find_area(start_pt,end_pt,numtraps);
printf("\nTOTAL AREA = %.2f",total_area);
getch();
}
Output :

AREA UNDER A CURVE

enter lower limit:5

enter upper limit:10

enter number of trapezoids:4

TOTAL AREA = 252.42


/*Assignment : 38*/

/*Program to read the numbers of file DATA and write all odd numbers to
a file called ODD and all even numbers to a file called EVEN.*/

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int n,num,i;
clrscr();
printf("enter limit\n");
scanf("%d",&n);
f1=fopen("DATA.rec","w");
printf("enter %d integers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&num);
fprintf(f1,"%d\n",num);
}
fclose(f1);
f1=fopen("DATA.rec","r");
f2=fopen("ODD.rec","w");
f3=fopen("EVEN.rec","w");
while((fscanf(f1,"%d",&num))!=EOF)
{
if(num%2 == 0)
fprintf(f3,"%d\n",num);
else
fprintf(f2,"%d\n",num);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD.rec","r");
printf("odd file:\n");
while((fscanf(f2,"%d",&num))!=EOF)
{
printf("%d\n",num);
}
fclose(f2);
f3=fopen("EVEN.rec","r");
printf("even file:\n");
while((fscanf(f1,"%d",&num))!=EOF)
{
printf("%d\n",num);
}
fclose(f3);
getch();
}

Output :

enter limit
5
enter 5 integers
1
2
3
4
5
odd file:
1
3
5
even file:
2
4
/*Assignment : 40*/

/*Program to print all the prime numbers, odd numbers, even numbers,
perfect squares upto a given limit n.*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,sqr,j,flag=0;
clrscr();
printf("Enter a limit.:\t");
scanf("%d",&n);
if(n<=1)
{
printf("\nInvalid Limit!");
exit(1);
}
printf("\nPrime Numbers are:\n");
for(i=1;i<=n;i=i+2)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\n",i);
}
}
printf("\nPerfect Squares Are:\n");
for(i=1;i<=n;i++)
{
sqr=sqrt(i);
if(pow(sqr,2)==i)
printf("%d\n",i);
}
printf("\n Odd Numbers\tEven Numbers\n");
for(i=1;i<=n;i++)
{
if(i%2==0)
printf("%d\n",i);
else
printf("%d\t\t\t",i);
}
getch();
}

Output :

Enter a limit.: 10

Prime Numbers are:


1
3
5
7

Perfect Squares Are:


1
4
9

Odd Numbers Even Numbers


1 2
3 4
5 6
7 8
9 10
/*Assignment : 41*/

/*Program to find transpose of a matrix.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,a[10][10];
clrscr();
printf("enter the order of matrix :\n");
scanf("%d%d",&m,&n);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\ninput matrix :\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",a[i][j]);
printf("\n\n");
}
printf("\ntranspose matrix :\n\n");
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
printf("%3d",a[i][j]);
printf("\n\n");
}
getch();
}
Output :

enter the order of matrix :


2 2
Enter the elements
2
3
4
5
input matrix :
2 3
4 5

transpose matrix :
2 4
3 5
/*Assignment : 42*/

/*Program to input a five-digit number and Calculate the sum of its


digits,Reverse the number,Determine whether the original and reversed
numbers are equal or not,Calculate the sum of the first and last digit.*/

#include<stdio.h>
#include<conio.h>
void main()
{
long int i=0,n,m,sum=0,sum1,rev=0,p,q,rem;
clrscr();
printf("enter a five digit number\n\n");
scanf("%ld",&n);
m=n;
while(n>0)
{
i++;
rem=n%10;
rev=(rev*10)+rem;
sum+=rem;
n=n/10;
}
if(i==5)
{
p=m%10;
q=rev%10;
sum1=p+q;
printf("\n\nsum of its digits = %ld",sum);
printf("\n\nreversed number = %ld",rev);
if(m==rev)
printf("\n\noriginal and
reversed number are equal");
else
printf("\n\noriginal and
reversed number are unequal");
printf("\n\nsum of first and last digit = %ld",sum1);
}
else printf("\n\ninvalid number\n");
getch();
}
Output :

enter a five digit number

34521

sum of its digits = 15

reversed number = 12543

original and reversed number are unequal

sum of first and last digit = 4


/*Assignment : 43*/

/*Program to interchange the contents of two locations C and D.*/

#include<stdio.h>
#include<conio.h>
void interchange(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int C,D;
clrscr();
printf("enter two numbers :\n\n");
scanf("%d%d",&C,&D);
printf("\n\nbefore interchanging content of :\n\n");
printf("C = %d\tD = %d\n",C,D);
interchange(&C,&D);
printf("\n\nafter interchanging content of :\n\n");
printf("C = %d\tD = %d\n",C,D);
getch();
}

Output :

enter two numbers :


2
3

before interchanging content of :


C = 2 D = 3

after interchanging content of :


C = 3 D = 2

Potrebbero piacerti anche