Vous êtes sur la page 1sur 9

1 /* A C program to print fibonacci series, developed and executed by Farhana

K on 21/7/14*/
2 #include<stdio.h>
3 void main()
4 {
5
int a=0,b=1,n,i,c;
6
printf("\nEnter limit: ");
7
scanf("%d",&n);
8
printf("\n %d %d",a,b);
9
for(i=0;i<n-2;i++)
10
{
11
c=a+b;
12
a=b;
13
b=c;
14
printf(" %d ",c);
15
}
16
printf("\n\n");
17 }
18
19
OUTPUT
20
-------21 Enter limit: 8
22
23
0 1 1 2 3 5 8 13
24
25
26 /*A C program to find factorial of a number using recursion, developed and
executed by Farhana K on 04/08/2014*/
27 #include<stdio.h>
28 int factorial(int);
29
30 void main()
31 {
32
int f,num;
33
printf("\nEnter a no. : ");
34
scanf("%d",&num);
35
f=factorial(num);
36
printf("\nFactorial of %d = %d",num,f);
37 }
38
39 int factorial(int n)
40 {
41
int fact;
42
if(n==1|n==0)
43
return(1);
44
else
45
{
46
fact=n*factorial(n-1);
47
return(fact);
48
}
49 }
50
51
OUTPUT
52
-------53 Enter a no. : 5
54
55 Factorial of 5 = 120
56
57 /*A C program to find roots of quadratic equation, developed and executed by
Farhana K on 04/08/2014*/
58 #include<stdio.h>
59 #include<math.h>
60 void main()
61 {
62
char ch;
63
do

64
{
65
int a,b,c,d,p,q,r,r1,r2;
66
printf("\nEnter coefficients:\n");
67
scanf("%d%d%d",&a,&b,&c);
68
d=(b*b)-(4*a*c);
69
if(d==0)
70
{
71
printf("Roots are real and equal. ");
72
r=-b/(2*a);
73
printf("\nRoot is %d",r);
74
}
75
else if(d>0)
76
{
77
printf("Roots are real and unequal. ");
78
r1=(-b+sqrt(d))/(2*a);
79
r2=(-b-sqrt(d))/(2*a);
80
printf("\nRoots are %d %d",r1,r2);
81
}
82
else if(d<0)
83
{
84
printf("Roots are imaginary and unequal. ");
85
p=(sqrt(-d))/(2*a);
86
r1=-b/(2*a);
87
printf("\nRoot1 = %d+i%d\nRoot2 = %d-i%d",r1,p,r1,p);
88
}
89
printf("\nWould you like to continue?");
90
scanf(" %c",&ch);
91
}while(ch=='Y'|ch=='y');
92 }
93
94 /*A C program to print HCF and LCM of 2 numbers, developed and executed by
Farhana K on 04/08/2014 */
95 #include<stdio.h>
96 void main()
97 {
98
int a,b,t,x,y,lcm,hcf;
99
printf("\nEnter 2 nos: \n");
100
scanf("%d%d",&x,&y);
101
a=x;
102
b=y;
103
while (b!=0)
104
{
105
t=b;
106
b=a%b;
107
a=t;
108
}
109
hcf=a;
110
lcm=(x*y)/hcf;
111
printf("\n HCF of %d and %d is :%d",x,y,hcf);
112
printf("\n LCM of %d and %d is :%d",x,y,lcm);
113
printf("\n\n");
114 }
115
116
OUTPUT
117
-----118 Enter 2 nos:
119 10
120 5
121
122 HCF of 10 and 5 is :5
123 LCM of 10 and 5 is :10
124
125 /*A C program to print prime series, developed and executed by Deepthi
Prabhakaran on 04/08/2014*/
126 #include<stdio.h>
127 void main()

128 {
129
int n=2,m;
130
printf("\nEnter limit: ");
131
scanf("%d",&m);
132
printf("2 3 5 7 ");
133
while(n<m)
134
{
135
if((n%2!=0)&(n%3!=0)&(n%5!=0)&(n%7!=0))
136
printf("%d ",n);
137
n++;
138
}
139 }
140
141
142 /*A C program to print Armstrong numbers, developed and executed by Deepthi
Prabhakaran on 18/08/2014*/
143 #include<stdio.h>
144 #include<math.h>
145 void main()
146 {
147
int l,a=1,y;
148
printf("Enter limit: ");
149
scanf("%d",&l);
150
printf("Armstrong numbers are: ");
151
do
152
{
153
int s=0,x,n;
154
y=a;
155
while(a>0)
156
{
157
n=a%10;
158
x=n*n*n;
159
s=s+x;
160
a=a/10;
161
}
162
if(s==y)
163
{
164
printf("%d ",y);
165
}
166
y++;
167
a=y;
168
}while(y<l);
169
}
170
171
OUTPUT
172
-------173
174 Enter limit: 500
175 Armstrong numbers are: 1 153 370 371 407
176
177
178 /*A C program to find sum of two matrices, developed and executed by Farhana
K on 04/08/2014*/
179 #include<stdio.h>
180 void main()
181 {
182
int i,j,p,q,r,c,a[10][10],b[10][10],s[10][10];
183
printf("\nEnter no. of rows and columns of a: ");
184
scanf("%d%d",&r,&c);
185
printf("\nEnter no. of rows and columns of b: ");
186
scanf("%d%d",&p,&q);
187
188
if((r!=p)||(c!=q))
189
{
190
printf("\naddition not possible");
191
}

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

else
{
printf("\nEnter the elements of matrix a: ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter the elements of matrix b: ");
for(i=0;i<r;i++)
{
for (j=0;j<c;j++)
scanf("%d",&b[i][j]);
}
printf("\nMatrix a:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nMatrix b:\n");
for(i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nsum of matrices:\n");
for(i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
s[i][j]=a[i][j]+b[i][j];
printf("%d\t",s[i][j]);
}
printf("\n");
}
}
}

OUTPUT
-------Enter no. of rows and columns of a: 2
3
Enter no. of rows and columns of b: 2
3
Enter the elements of matrix a: 1
2
3
4

258 5
259 6
260
261 Enter the elements of matrix b: 1
262 2
263 3
264 4
265 5
266 6
267
268 Matrix a:
269 1
2
3
270 4
5
6
271
272 Matrix b:
273 1
2
3
274 4
5
6
275
276 sum of matrices:
277 2
4
6
278 8
10
12
279
280
281 /*A C program to find product of two matrices, developed and executed by
Deepthi Prabhakaran on 18/08/2014*/
282 #include<stdio.h>
283 void main()
284 {
285
int i,j,p,q,r,c,k,a[10][10],b[10][10],s[10][10];
286
printf("\nEnter no. of rows and columns of a: ");
287
scanf("%d%d",&r,&c);
288
printf("\nEnter no. of rows and columns of b: ");
289
scanf("%d%d",&p,&q);
290
291
if(c==p)
292
{
293
printf("\nEnter the elements of matrix a: ");
294
for(i=0;i<r;i++)
295
{
296
for(j=0;j<c;j++)
297
scanf("%d",&a[i][j]);
298
}
299
300
printf("\nEnter the elements of matrix b: ");
301
for(i=0;i<p;i++)
302
{
303
for (j=0;j<q;j++)
304
scanf("%d",&b[i][j]);
305
}
306
307
printf("\nMatrix a:\n");
308
for(i=0;i<r;i++)
309
{
310
for(j=0;j<c;j++)
311
{
312
printf("%d\t",a[i][j]);
313
}
314
printf("\n");
315
}
316
317
printf("\nMatrix b:\n");
318
for(i=0;i<p;i++)
319
{
320
for (j=0;j<q;j++)
321
{
322
printf("%d\t",b[i][j]);

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

}
printf("\n");
}
printf("\nproduct of matrices:\n");
for(i=0;i<r;i++)
{
for (j=0;j<q;j++)
{
s[i][j]=0;
for(k=0;k<c;k++)
{
s[i][j]=s[i][j]+(a[i][k]*b[k][j]);
}
}
}

for(i=0;i<r;i++)
{
for (j=0;j<q;j++)
{
printf("%d\t",s[i][j]);
}
printf("\n");
}
}
}
OUTPUT
-------Enter no. of rows and columns of a: 3
2
Enter no. of rows and columns of b: 2
2
Enter the elements of matrix a: 1
2
3
4
5
6
Enter the elements of matrix b: 1
3
5
7
Matrix a:
1
2
3
4
5
6
Matrix b:
1
3
5
7
product of matrices:

389 11
17
390 23
37
391 35
57
392
393 /*A C program to find inverse of a matrix, developed and executed by Deepthi
Prabhakaran on 25/08/2014*/
394 #include<stdio.h>
395 float determinant(float z[10][10],int m);
396 void adj(float z[10][10],int m);
397 void trans(float z[10][10],float t[10][10],int m);
398 void main()
399 {
400
int i,n,j;
401
float d,a[10][10];
402
printf("\nEnter no. of rows and columns of square matrix: ");
403
scanf("%d",&n);
404
printf("\nEnter the elements of matrix a: ");
405
for(i=0;i<n;i++)
406
{
407
for(j=0;j<n;j++)
408
scanf("%f",&a[i][j]);
409
}
410
printf("\nMatrix a is:\n");
411
for(i=0;i<n;i++)
412
{
413
for(j=0;j<n;j++)
414
{
415
printf("%.2f\t",a[i][j]);
416
}
417
printf("\n");
418
}
419
d=determinant(a,n);
420
printf("\ndeterminant is: %.2f",d);
421
if (d==0)
422
{
423
printf("The inverse does not exist");
424
}
425
else
426
{
427
adj(a,n);
428
}
429 }
430
431
432 float determinant(float f[10][10],int x)
433 {
434
int s,r,j,p,q,y,k;
435
float det, b[10][10],c[100];
436
if(x==2)
437
{
438
det=(f[0][0]*f[1][1]-f[0][1]*f[1][0]);
439
return (det);
440
}
441
else
442
{
443
for(j=0;j<x;j++)
444
{
445
r=0,s=0;
446
for(p=0;p<x;p++)
447
{
448
for(q=0;q<x;q++)
449
{
450
if(p!=0 && q!=j)
451
{
452
b[r][s]=f[p][q];
453
s++;

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

if(s>(x-2))
{
r++;
s=0;
}
}
}
}
for(k=0,y=1;k<j+2;k++)
{
y=-1*y;
}
c[j]=y*determinant(b,x-1);
}
for(det=0,j=0;j<x;j++)
{
det=det+(c[j]*f[0][j]);
}
return (det);
}
}
void adj(float f[10][10],int x)
{
int r,s,i,j,p,k,y,d,q;
float b[10][10],c[10][10];
d=determinant(f,x);
if (x==2)
{
b[0][0]=f[1][1]/d;
b[0][1]=f[0][1]/d;
b[1][0]=f[1][0]/d;
b[1][1]=f[0][0]/d;
printf("\n\nThe inverse of the matrix is: \n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%.2f\t",b[i][j]);
}
printf("\n");
}
}
else
{
for(j=0;j<x;j++)
{
r=0,s=0;
for(p=0;p<x;p++)
{
for(q=0;q<x;q++)
{
if(p!=i && q!=j)
{
b[r][s]=f[p][q];
s++;
if(s>(x-2))
{
r++;
s=0;
}
}
}
}
for(k=0,y=1;k<i+j+2;k++)
{

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

y=-1*y;
}
c[i][j]=y*determinant(b,x-1);
}
}
trans(f,c,x);
}
void trans(float h[10][10],float l[10][10],int x)
{
int i,j;
float d,b[10][10];
d=determinant(h,x);
for (i=0;i<x;i++)
{
for (j=0;j<x;j++)
{
b[i][j]=l[j][i]/d;
}
}
if (x!=2)
{
printf("\n\nThe inverse of the matrix is: \n");
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%.2f\t",b[i][j]);
}
printf("\n");
}
}
}

Vous aimerez peut-être aussi