C Programs
C Programs
S.NO
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
NOS
CONTENTS
AREA OF TRIANGLE
AREA OF CIRCLE
SIMPLE INTEREST
ODD OR EVEN
POSITIVE OR NEGATIVE
BIGGEST NUMBER
ROOTS OF QUADRATIC EQN
CALCULATOR(+,-,*,/)
SUM OF N NATURAL NOS
FIBONACCI NUMBERS
GCD AND LCM
SUM OF N ODD AND EVEN NOS
PALINDROME NUMBER
SIN(X)
COS(X)
CHECK IF PRIME NUMBER
PRINT PRIME NUMBERS
RANGE DIVISIBLE BY 5
ARRAY,SUM OF POSITIVE,NEGATIVE
AND AVERAGE
LINEAR SEARCH
ASCENDING ORDER USING BUBBLE
20.
21.
SORT
22.
SORTING AND BINARY SEARCH
23.
MEAN,VARIANCE AND STANDARD
DEV
24.
AVG OF 2 LARGEST NOS WITHOUT
SORTING
25.
POLYNOMIAL
26.
MATRICES-SUM,DIFFERENCE AND
TRACE
27.
28.
29.
30.
LETTERS
TRANSPOSE OF MATRIX
PALINDROME STRING
CONCATENATE STRINGS
REVERSE LOWER & UPPER CASE
31.
NO. OF VOWELS AND CONSONANTS
32.
SORTING NAMES IN ALPHABETICAL
ORDER
33.
SELECTION SORT
34.
FUNCTIONS AND PRODUCT OF
MATRIX
35.
SUM OF ALL,ROW & COLUMN
ELEMENTS
36.
SWAPPING USING FUNCTIONS
37.
BINARY TO DECIMAL CONVERSION
38.
ARRAY-COPYING ODD AND EVEN
NOS
39.
FIBONACCI SEQUENCE
40.
INSERTING AN ELEMENT INTO AN
ARRAY
41.
REVERSE AN INTEGER
42.
PASSWORD IS HIDDEN BY *
43.
SUBSTRING IN STRING OR NOT
44.
SURFACE AREA AND VOLUME OF
CUBE
45.
DECIMAL TO BINARY CONVERSION
46.
LEAP YEAR OR NOT
47.
SWAP USING BITWISE XOR
OPERATION
48.
CONVERT A DATE IN YR WEEK &
DAYS TO
DAYS
49.
50.
51.
USING
61.
62.
ARRAY
63.
64.
NOS IN
STRING FUNCTION
52.
NO. OF THE PRESENT IN A TEXT
53.
COMPUTE X ^ N
54.
SUM AND DIFFERENCE OF TWO
MATRICES
55.
CHECK IF 2 MATRICES ARE EQUAL
56.
CHECK IF IDENTITY MATRIX
57.
INTERCHANGE ANY 2 ROWS AND
COLUMNS
58.
BITWISE SHIFT OPERATION
59.
CYCLIC PERMUTATION OF ARRAYS
60.
DESCENDING ORDER
70.
AREAS OF DIFFERENT GEOMETRIC
SHAPES
71.
ACCEPT GRADE AND CATEGORIZE
72.
FACTORIAL
73.
SUM OF ALL DIGITS PRESENT IN A
STRING
74.
SUM OF DIGITS IN AN INTEGER
75.
SUM OF ROW AND COLUMN
ELEMENTS
76.
SUM OF DIAGONAL ELEMENTS
77.
TRACE AND NORMAL OF MATRIX
78.
SPARSE MATRIX
79.
SORT ROWS IN ASCENDING AND
COLUMNS
IN DESCENDING ORDER
1.
/* Write a C program to find the area of a
triangle, given three sides*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int s, a, b, c, area;
clrscr();
/* compute s*/
s = (a + b + c) / 2;
/*----------------------------Output
2.
/* Write a C program to find the area of a
circl, given the Radius*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.142
void main()
{
float radius, area;
clrscr();
area);
/*----------------------------Output
RUN1
Enter the radius of a circle
3.2
Area of a circle = 32.17
RUN 2
Enter the radius of a circle
6
Area of a circle = 113.11
------------------------------*/
3. /* Write a C program to find the simple
interest , given principle, *
* rate of interest and times*/
#include <stdio.h>
#include <conio.h>
void main()
{
float p, r, si;
int t;
clrscr();
10
si = (p * r * t)/ 100.0;
/*----------------------------Output
Enter the values of p,r and t
2000
11
8
3
Amount = Rs. 2000.00
Rate = Rs. 8.00%
Time = 3 years
Simple interest = 480.00
------------------------------*/
4. /* Write a C program to check whether a
given integer is odd or even*/
#include <stdio.h>
#include <conio.h>
void main()
{
int ival, remainder;
12
clrscr();
remainder = ival % 2;
if (remainder == 0)
printf ("%d, is an even integer\n",
ival);
else
printf ("%d, is an odd integer\n", ival);
}
/*----------------------------Output
13
RUN1
RUN2
Enter an integer :24
24, is an even integer
---------------------------------*/
#include <stdio.h>
14
#include <conio.h>
void main()
{
int number;
clrscr();
printf("Enter a number\n");
scanf ("%d", &number);
if (number > 0)
printf ("%d, is a positive number\n",
number);
else
printf ("%d, is a negative number\n",
number);
15
/*----------------------------Output
Enter a number
-5
-5, is a negative number
RUN2
Enter a number
89
89, is a positive number
------------------------------*/
6. /* Write a C program to find the biggest of
three numbers*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
16
void main()
{
int a, b, c;
clrscr();
c\n");
if ( a > b)
{
if ( a > c)
17
{
printf ("A is the greatest
among three\n");
}
else
{
three\n");
}
}
else if (b > c)
{
printf ("B is the greatest among
three\n");
}
else
printf ("C is the greatest among
three\n");
18
/*----------------------------Output
Enter the values of a,b and c
23 32 45
a = 23 b = 32 c = 45
C is the greatest among three
RUN2
Enter the values of a,b and c
234
678
195
a = 234 b = 678 c = 195
19
RUN3
Enter the values of a,b and c
30 20 10
a = 30 b = 20 c = 10
A is the greatest among three
------------------------------*/
7. /* write a C program to find and output all
the roots of a *
* quadratic equation, for non-zero
coefficients. In case
*
* of errors your program should report
suitable error message*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
20
#include <math.h>
int main()
{
float A, B, C, root1, root2;
float realp, imagp, disc;
//clrscr();
/* If A = 0, it is not a quadratic
equation */
21
22
{
printf("Roots are real and
equal\n");
root1 = -B/(2.0*A);
root2 = root1;
printf("Root1 = %f \n",root1);
printf("Root2 = %f \n",root2);
}
else if(disc > 0 )
{
distinct\n");
}
}
23
return 0;
} /* End of main() */
/*--------------------------Output
RUN 1
Enter the values of A, B and C
321
Imaginary Roots
Root1 = -0.333333 +i 0.471405
Root2 = -0.333333 -i 0.471405
RUN 2
Enter the values of A, B and C
121
Roots are real and equal
24
Root1 = -1.000000
Root2 = -1.000000
RUN 3
Enter the values of A, B and C
352
Roots are real and distinct
Root1 = -0.666667
Root2 = -1.000000
---------------------------------*/
8. /* Write a C program to simulate a simple
calculator to perform *
* arithmetic operations like addition,
subtraction,multiplication *
* and division only on integers. Error
message should be repoetrd *
* if any attempt is made to divide by zero
*/
25
#include <stdio.h>
#include <conio.h>
void main()
{
char oper;
operator to be selected */
/* oper is an
clrscr();
26
fflush (stdin);
switch (oper)
{
case '+': result = n1 + n2;
break;
case '-': result = n1 - n2;
break;
case '*': result = n1 * n2;
break;
case '/': result = n1 / n2;
break;
27
}
/*----------------------------Output
Simulation of Simple Calculator
28
RUN2
Simulation of Simple Calculator
RUN3
Simulation of Simple Calculator
29
RUN4
Simulation of Simple Calculator
30
------------------------------*/
9. /* Write a C program to find the sum of 'N'
natural numbers*/
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N, sum = 0;
clrscr();
31
/*---------------------------------------Output
RUN1
32
RUN2
#include <stdio.h>
void main()
33
{
int fib1=0, fib2=1, fib3, N, count=0;
34
fib1 = fib2;
fib2 = fib3;
}
}
/* End of main() */
35
13
21
34
-------------------------------*/
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, gcd, lcm, remainder,
numerator, denominator;
clrscr();
36
37
numerator = denominator;
denominator = remainder;
remainder = numerator %
denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d \n",
num1,num2,gcd);
printf("LCM of %d and %d = %d \n",
num1,num2,lcm);
}
/* End of main() */
/*-----------------------Output
RUN 1
Enter two numbers
5
15
38
GCD of 5 and 15 = 5
LCM of 5 and 15 = 15
------------------------------*/
12. /* Write a C program to find the sum of
odd numbers and *
* sum of even numbers from 1 to N. Output
the computed *
* sums on two different lines with suitable
headings */
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N, oddSum = 0, evenSum = 0;
clrscr();
39
40
/*----------------------------Output
RUN1
RUN2
Enter the value of N
50
Sum of all odd numbers = 625
Sum of all even numbers = 650
------------------------------*/
41
#include <stdio.h>
#include <conio.h>
void main()
{
int num, temp, digit, rev = 0;
clrscr();
printf("Enter an integer\n");
scanf("%d", &num);
*/
42
temp = num;
stored at temp */
/* original number is
while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
if(temp == rev )
printf("Number is a palindrome\n");
else
43
printf("Number is not a
palindrome\n");
}
/*-----------------------Output
RUN 1
Enter an integer
12321
Given number is = 12321
Its reverse is = 12321
Number is a palindrome
RUN 2
Enter an integer
3456
Given number is = 3456
Its reverse is = 6543
44
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float acc, term, den, x, sinx=0, sinval;
45
clrscr();
x1 = x;
x = x*(3.142/180.0);
sinval = sin(x);
term = x;
46
sinx = term;
n = 1;
do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;
= %f\n",
47
/*End of main() */
/*-----------------------------Output
Enter the value of x (in degrees)
30
Enter the accuary for the result
0.000001
Sum of the sine series
= 0.500059
RUN 2
Enter the value of x (in degrees)
45
Enter the accuary for the result
0.0001
Sum of the sine series
= 0.707215
48
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float acc, term, den, x, cosx=0, cosval;
49
clrscr();
x1 = x;
x = x*(3.142/180.0);
cosval = cos(x);
50
cosx = term;
n = 1;
do
{
den = 2*n*(2*n-1);
term = -term * x * x / den;
cosx = cosx + term;
n = n + 1;
} while(acc <= fabs(cosval - cosx));
= %f\n",
/*End of main() */
/*-----------------------------Output
51
= 0.865991
RUN 2
Enter the value of x (in degrees)
45
Enter the accuary for the result
0.0001
Sum of the cosine series
= 0.707031
52
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
int num, j, flag;
clrscr();
printf("Enter a number\n");
scanf("%d", &num);
53
if ( num <= 1)
{
num);
flag = 0;
54
if(flag == 0)
printf("%d is a prime
number\n",num);
else
printf("%d is not a prime number\n",
num);
}
/*-----------------------Output
RUN 1
Enter a number
34
34 is not a prime number
RUN 2
Enter a number
55
29
29 is a prime number
-----------------------------*/
17. /* Write a C program to generate and
print prime numbers in a given *
* range. Also print the number of prime
numbers
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
int M, N, i, j, flag, temp, count = 0;
clrscr();
56
if(N < 2)
{
N);
}
printf("Prime numbers are\n");
temp = M;
if ( M % 2 == 0)
{
M++;
}
57
58
}
}
printf("Number of primes between %d and
%d = %d\n",temp,N,count);
}
/*--------------------------------Output
Enter the value of M and N
15 45
Prime numbers are
17
19
23
29
31
37
41
59
43
Number of primes between 15 and 45 = 8
-------------------------------------------*/
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N1, N2, count = 0, sum = 0;
60
clrscr();
61
}
}
} /* End of main()*/
/*----------------------------Output
Enter the value of N1 and N2
2
27
Integers divisible by 5 are
5, 10, 15, 20, 25,
62
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10
63
void main()
{
int array[MAXSIZE];
int i, N, negsum=0, posum=0;
float total=0.0, averg;
clrscr();
64
fflush(stdin);
}
/* Summing begins */
for(i=0; i< N ; i++)
{
if(array[i] < 0)
{
negsum = negsum + array[i];
}
65
averg = total / N;
printf("\nSum of all negative numbers
%d\n",negsum);
printf("Sum of all positive numbers
%d\n", posum);
66
/*End of main()*/
/*------------------------------------Output
Enter the value of N
5
Enter 5 numbers (-ve, +ve and zero)
5
-3
0
-7
6
Input array elements
+5
-3
+0
67
-7
+6
= -10
= 11
#include <stdio.h>
#include <conio.h>
*/
68
void main()
{
int array[10];
int i, N, keynum, found=0;
clrscr();
69
70
if ( found == 1)
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");
/* End of main */
/*-----------------------------------Output
RUN 1
Enter the value of N
5
Enter the elements one by one
23
12
56
43
71
89
Input array is
23
12
56
43
89
Enter the element to be searched
56
SUCCESSFUL SEARCH
RUN 2
Enter the value of N
3
Enter the elements one by one
456
213
72
879
Input array is
456
213
879
Entee the element to be searched
1000
Search is FAILED
--------------------------------------*/
73
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10
int main()
{
int array[MAXSIZE];
int i, j, N, temp;
//clrscr();
74
scanf("%d",&array[i]);
}
printf("Input array is\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
for(i=0; i< N ; i++)
{
for(j=0; j< (N-i-1) ; j++)
{
if(array[j] > array[j+1])
{
temp
= array[j];
array[j] = array[j+1];
75
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
return 0;
} /* End of main*/
/*---------------------------------Output
Enter the value of N
5
Enter the elements one by one
76
390
234
111
876
345
Input array is
390
234
111
876
345
Sorted array is...
111
234
345
390
77
876
---------------------------*/
#include <stdio.h>
#include <conio.h>
void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;
78
clrscr();
79
= array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
80
low=1;
high=N;
do
{
mid= (low + high) / 2;
if ( keynum < array[mid] )
high = mid - 1;
else if ( keynum > array[mid])
81
low = mid + 1;
} while( keynum!=array[mid] && low <=
high); /* End of do- while */
} /* End of main*/
/*---------------------------------Output
Enter the value of N
82
4
Enter the elements one by one
3
1
4
2
Input array elements
3
1
4
2
Sorted array is...
1
2
3
4
Enter the element to be searched
83
4
SUCCESSFUL SEARCH
---------------------------*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
84
int i, n;
float avrg, var, SD, sum=0, sum1=0;
clrscr();
85
{
sum = sum + x[i];
}
avrg = sum /(float) n;
86
= %.2f\n",
} /*End of main()*/
/*-------------------------Output
Enter the value of N
6
Enter 6 real numbers
12
34
10
50
42
33
Average of all elements = 29.66
Varience of all elements = 213.89
87
Standard deviation
= 14.62
-------------------------------------*/
#include <stdio.h>
#include <conio.h>
#define MAX 4
void main()
{
88
clrscr();
printf("\n");
89
for (i=2;i<4;i++)
{
if (a[i] >= l1)
{
90
l2 = l1;
l1 = a[i];
}
else if(a[i] > l2)
{
l2= a[i];
}
}
}
/*----------------------------------Output
91
RUN 1
Enter 4 integer numbers
45
33
21
10
Input interger are
45 33 21 10
Average of 45 and 33 = 39
RUN 2
Enter 4 integer numbers
12
92
90
54
67
Input interger are
12 90 54 67
Average of 90 and 67 = 78
RUN 3
Enter 4 integer numbers
100
200
300
93
400
Input interger are
100 200 300 400
------------------------------------*/
25. /* Write a C program to evaluate the
given polynomial *
* P(x)=AnXn + An-1Xn-1 + An-2Xn-2+...
+A1X + A0, by
*
* reading its coefficents into an array.
[Hint:Rewrite *
* the polynomial as
94
* P(x) = a0 +
x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan))))
*
* and evalate the function starting from the
inner loop]*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 10
voidmain()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;
clrscr();
95
printf("Enter %d coefficients\n",N+1);
for (i=0;i <= N;i++)
{
scanf("%d",&a[i]);
}
polySum = a[0];
96
power = N;
/*power--;*/
97
/*-----------------------------------------------------
98
Output
RUN 1
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6
Given polynomial is:
+ 3x^2 + 2x^1 + 6x^0
Sum of the polynomial = 22.00
RUN 2
Enter the order of the polynomial
99
4
Enter the value of x
1
Enter 5 coefficients
3
-5
6
8
-9
Given polynomial is:
+ 3x^4 - 5x^3 + 6x^2 + 8x^1 - 9x^0
Sum of the polynomial = 3.00
-----------------------------------------------------*/
100
#include <stdio.h>
#include <conio.h>
void main()
{
int A[10][10], B[10][10], sumat[10][10],
diffmat[10][10];
int i, j, M,N, option;
101
clrscr();
printf("MATRIX A is\n");
102
103
printf("MATRIX B is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",B[i][j]);
}
printf("\n");
}
switch (option)
104
{
case 1: for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
sumat[i][j] = A[i][j] + B[i][j];
}
}
105
printf("\n");
}
106
/* End of main() */
107
{
int i, j, trace = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if (i==j)
{
trace = trace + arr[i][j];
}
}
}
printf ("Trace of the resultant matrix is =
%d\n", trace);
}
/*-----------------------------------
108
109
Sum matrix is
4 4
6 6
Trace of the resultant matrix is = 10
---------------------------------------------*/
27. /* Write a C program to read A (MxN), find
the transpose *
* of a given matrix and output both the input
matrix and*
* the transposed matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,M,N;
*/
110
clrscr();
111
printf("Matrix A is\n");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}
112
{
B[i][j] = A[j][i];
}
}
/*End of main()*/
/*---------------------------------------
113
Output
Enter the order of matrix A
33
Enter the elements of matrix
1
2
3
4
5
6
7
8
9
MatrixxA is
1 2 3
4 5 6
114
7 8 9
Its Transpose is
1 4 7
2 5 8
3 6 9
-----------------------------*/
28. /* Write a C program to read a string and
check whether it is *
* a palindrome or not (without using library
functions). Output *
* the given string along with suitable
message
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
115
{
char string[25], revString[25]={'\0'};
int i,length = 0, flag = 0;
clrscr();
fflush(stdin);
printf("Enter a string\n");
gets(string);
*/
}
/*character of
/*till its end
116
117
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome\n",
string);
else
string);
} /*End of main()*/
/*---------------------------------------------------Output
RUN 1
Enter a string
madam
madam is a palindrome
118
RUN 2
Enter a string
Madam
Madam is not a palindrome
RUN 3
Enter a string
good
good is not a palindrome
----------------------------------------------------------*/
119
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string1[20], string2[20];
int i,j,pos;
120
121
pos = i;
122
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
*/
123
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i+
+)
{
;
}
sentence[i]='\0';
124
} /*End of main()*/
/*-----------------------------Output
Enter a sentence
Mera Bharat Mahan
The given sentence is : Mera Bhaaat Mahan
125
#include <stdio.h>
#include <conio.h>
void main()
{
char sentence[80];
int i, vowels=0, consonants=0, special = 0;
clrscr();
126
printf("Enter a sentence\n");
gets(sentence);
127
consonants = consonants + 1;
}
if (sentence[i] =='\t' ||sentence[i] =='\0'
|| sentence[i] ==' ')
{
special = special + 1;
}
}
}
/*---------------------------------------Output
128
Enter a sentence
Good Morning
No. of vowels in Good Morning = 4
No. of consonants in Good Morning = 7
-----------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
*/
129
void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;
clrscr();
130
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
131
/* End of main() */
/*-------------------------------Output
Enter the value of N
3
Enter 3 names
Monica
Laxmi
Anand
132
---------------------------------------Input Names
Sorted names
---------------------------------------Monica
Anand
Laxmi
Laxmi
Anand
Monica
------------------------------------------------------------------------------- */
33. /* Write a C program to sort given N
elements using SELECTION sort method *
* using functions
*
* a) To find maximum of elements
*
* b) To swap two elements
*/
133
#include <stdio.h>
#include <conio.h>
void main()
{
int array[10];
int i, j, N, temp;
clrscr();
/* function
134
135
/* End of main*/
136
}
}
return(max);
}
big = findmax(b,j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
137
}
return;
}
/*----------------------------Output
Enter the value of N
5
Enter the elements one by one
45
12
90
33
78
Input array elements
45
12
138
90
33
78
Sorted array is
12
33
45
78
90
-----------------------------------*/
34. /* Develop functions
*
* a) To read a given matrix
* b) To output a matrix
139
*/
#include <stdio.h>
#include <conio.h>
#define MAXROWS 10
#define MAXCOLS 10
void main()
{
int A[MAXROWS][MAXCOLS], B[MAXROWS]
[MAXCOLS], C[MAXROWS][MAXCOLS];
int M, N;
140
/*Function declarations*/
clrscr();
141
productMatrix(A,B,C, M,N);
142
143
}
printf("\n");
}
}
/* Multiplication of matrices */
void productMatrix(int A[][MAXCOLS], int B[]
[MAXCOLS], int C[][MAXCOLS],
int M, int N)
{
int i, j, k;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
C[i][j] = 0 ;
for (k=0; k < N; k++)
144
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
}
/*--------------------------------------------Output
Enter the value of M and N
33
Enter matrix A
111
222
333
Matrix A
1 1 1
145
2 2 2
3 3 3
Enter matrix B
123
456
789
Matrix B
1 2 3
4 5 6
7 8 9
The product matrix is
12 15 18
24 30 36
36 45 54
--------------------------------*/
35. /* Write a C program to read a matrix A
(MxN) and to find the *
146
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum,sumall=0;
147
/* Function declaration */
int Addrow(int A[10][10], int k, int c);
int Addcol(int A[10][10], int k, int c);
clrscr();
148
149
150
151
/*---------------------------Output
Enter the order of the matrix
3
3
Enter the elements of the matrix
123
152
456
789
Input matrix is
1 2 3
4 5 6
7 8 9
Sum of row 1 = 6
Sum of row 2 = 15
Sum of row 3 = 24
Sum of column 1 = 12
Sum of column 2 = 15
Sum of column 3 = 18
Sum of all elements of matrix = 45
--------------------------------------*/
153
*/
#include <stdio.h>
#include <conio.h>
void main()
{
float M,N;
154
/* End of main() */
155
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
} /* End of Function */
/* ---------------------------------------Output
Enter the values of M and N
32 29
Before Swapping:M = 32.00
After Swapping:M = 29.00
N = 29.00
N = 32.00
------------------------------------------*/
156
#include <stdio.h>
void main()
{
int num, bnum, dec = 0, base = 1, rem ;
bnum = num;
/*maximum five
157
/* End of main() */
/*--------------------------------------------Output
Enter a binary number(1s and 0s)
10101
158
#include <stdio.h>
void main()
{
long int ARR[10], OAR[10], EAR[10];
int i,j=0,k=0,n;
159
160
}
else
{
OAR[k] = ARR[i];
k++;
}
}
161
printf("%ld\n", EAR[i]);
}
}
/*End of main()*/
/*------------------------------------Output
Enter the size of array AR
6
Enter the elements of the array
12
345
678
899
900
111
The elements of OAR are
345
162
899
111
The elements of EAR are
12
678
900
---------------------------------------*/
39. /* Write a C program to generate
Fibonacci sequence *
* Fibonacci sequence is 0 1 1 2 3 5 8 13
21 ... */
#include <stdio.h>
void main()
{
int fib1=0, fib2=1, fib3, limit, count=0;
163
164
/* End of main() */
165
* in a given array
*/
#include <stdio.h>
#include <conio.h>
void main()
{
int x[10];
int i, j, n, m, temp, key, pos;
clrscr();
166
167
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
168
m = n - pos + 1 ;
169
x[pos] = key;
/* End of main() */
/*------------------------------------Output
Enter how many elements
5
Enter the elements
2
14
170
67
83
29
Input array elements are
2
14
67
83
29
Sorted list is
2
14
29
67
83
Enter the element to be inserted
34
171
Final list is
2
14
29
34
67
83
-------------------------------------------------*/
41. /* Write a C program to accept an integer
and reverse it */
#include <stdio.h>
void main()
{
long num, rev = 0, temp, digit;
172
temp = num;
while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
173
/* --------------------------Output
Enter the number
123456
Given number = 123456
Its reverse is = 654321
------------------------------*/
42. /* This program is to illustrate how user
authentication *
* is made before allowing the user to access
the secured *
* resources. It asks for the user name and
then the
*
* password. The password that you enter will
not be
*
* displayed, instead that character is
replaced by '*' */
174
#include <stdio.h>
#include <conio.h>
void main()
{
char pasword[10],usrname[10], ch;
int i;
clrscr();
for(i=0;i<8;i++)
{
175
ch = getch();
pasword[i] = ch;
ch = '*' ;
printf("%c",ch);
}
pasword[i] = '\0';
for(i=0;i<8;i++)
{
printf("%c",pasword[i]);
}
}
176
/*----------------------------------------------Output
Enter User name: Latha
Enter the password <any 8 characters>:
********
Your password is :Wipro123
-----------------------------------------------*/
43. /* Write a C program to accept a string
and a substring and
* check if the substring is present in the
given strig */
#include<stdio.h>
#include<conio.h>
void main()
{
177
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
clrscr();
puts("Enter a string:");
gets(str);
while (str[count1]!='\0')
count1++;
while (search[count2]!='\0')
178
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
179
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}
/*-----------------------------Output
Enter a string:
Hello how are you?
Enter search substring:
how
SEARCH SUCCESSFUL!
------------------------------*/
44. /* Write a c program to compute the
surface area and *
*
volume of a cube
*/
180
#include <stdio.h>
#include <math.h>
void main()
{
float side, surfArea, volume;
181
/*-----------------------------------------Output
Enter the llngth of a side
4
Surface area = 96.00 and Volume = 64.00
RUN2
Enter the length of a side
12.5
Surface area = 937.50 and Volume =
1953.12
------------------------------------------*/
182
#include <stdio.h>
void main()
{
long num, dnum, rem, base = 1, bin = 0,
no_of_1s = 0;
dnum = num;
183
{
no_of_1s++;
}
bin = bin + rem * base;
num = num / 2 ;
base = base * 10;
}
printf("Input number is
dnum);
printf("Its Binary equivalent is
%ld\n", bin);
= %d\n",
=
/* End of main() */
/*-------------------------------------------------Output
184
= 75
= 1001011
RUN2
Enter a decimal integer
128
Input number is
Its Binary equivalent is
= 128
= 10000000
-----------------------------------------------------*/
185
*/
#include <stdio.h>
void main()
{
int year;
printf("Enter a year\n");
scanf("%d",&year);
if ( (year % 4) == 0)
printf("%d is a leap year",year);
else
printf("%d is not a leap
year\n",year);
186
/*-----------------------------------------Output
Enter a year
2000
2000 is a leap year
RUN2
Enter a year
2007
2007 is not a leap year
------------------------------------------*/
187
#include <stdio.h>
void main()
{
long i,k;
188
i = i^k;
k = i^k;
i = i^k;
/*-----------------------------------------Output
Enter two integers
23 34
189
*/
#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;
190
year = ndays/365;
week = (ndays % 365)/DAYSINWEEK;
days = (ndays%365) % DAYSINWEEK;
/*----------------------------------------------Output
Enter the number of days
375
375 is equivalent to 1 years, 1 weeks and 3
days
191
423
423 is equivalent tt 1 years, 8 weeks and 2
days
#include<stdio.h>
#include<conio.h>
192
void main()
{
int count1=0,count2=0,flag=0,i;
char str1[10],str2[10];
clrscr();
puts("Enter a string:");
gets(str1);
193
count2++;
i=0;
194
{
flag = -1;
break;
}
if (str1[i] > str2[i])
{
flag = 1;
break;
}
}
if (flag==0)
printf("Both strings are equal\n");
if (flag==1)
printf("String1 is greater than
string2\n", str1, str2);
if (flag == -1)
195
RUN2
Enter a string:
Hello
Enter another string:
Hello
196
RUN3
Enter a string:
gold
Enter another string:
silver
String1 is less than string2
----------------------------------------*/
50. /* Program to accept two strings and
concatenate them
*
* i.e.The second string is appended to the
end of the first string */
#include <stdio.h>
#include <string.h>
197
void main()
{
char string1[20], string2[20];
int i,j,pos;
198
pos = i;
199
/*--------------------------------------Output
Enter the first string :CDEnter the second string:ROM
First string = CDSecond string = ROM
Concatenated string = CD-ROM
----------------------------------------*/
200
#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string\n");
gets(string);
201
{
the string */
length++;
*/
/*character of
/*till its end
}
printf("The length of a string is the
number of characters in it\n");
printf("So, the length of %s =%d\n",
string, length);
}
/*---------------------------------------------------Output
Enter a string
hello
The length of a string is the number of
characters in it
So, the length of hello = 5
202
RUN2
Enter a string
E-Commerce is hot now
The length of a string is the number of
characters in it
So, the length of E-Commerce is hot now =21
----------------------------------------------------------*/
#include<stdio.h>
#include<conio.h>
void main()
{
int count=0,i,times=0,t,h,e,space;
203
char str[100];
clrscr();
puts("Enter a string:");
gets(str);
204
h=(str[i+1]=='h'||str[i+1]=='H');
e=(str[i+2]=='e'||str[i+2]=='E');
space=(str[i+3]==' '||str[i+3]=='\0');
if ((t&&h&&e&&space)==1)
times++;
}
printf("Frequency of the word \'the\' is
%d\n",times);
getch();
}
/*----------------------------------------------------Output
Enter a string:
The Teacher's day is the birth day of
Dr.S.Radhakrishnan
Frequency of the word 'the' is 2
---------------------------------------------------------*/
205
#include <stdio.h>
#include <math.h>
void main()
{
long int x,n,xpown;
long int power(int x, int n);
206
/* if n is odd*/
207
25
X to the power N = 32
RUN2
Enter the values offX and N
44
X to the power N ==256
RUN3
Enter the values of X and N
52
X to the power N = 25
RUN4
Enter the values of X and N
10 5
208
#include <stdio.h>
#include <stdlib.h>
void main()
{
/* Function declarations */
209
void readmatA();
void printmatA();
void readmatB();
void printmatB();
void sum();
void diff();
A\n");
210
211
sum();
diff();
}
}
/* main() */
212
213
214
printf("%3d",B[i][j]);
}
printf("\n");
}
}
/*Function to find the sum of elements of
matrix A and Matrix B*/
void sum()
{
for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
sumat[i][j] = A[i][j] + B[i][j];
}
}
printf("Sum matrix is\n");
215
216
217
/*--------------------------------------------------Output
Enter the order of the matrix A
22
Enter the order of the matrix B
22
Enter the elements of matrix A
1
2
3
4
MATRIX A is
1 2
3 4
Enter the elements of matrix B
218
2
4
6
8
MATRIX B is
2 4
6 8
Sum matrix is
3 6
9 12
Difference matrix is
-1 -2
-3 -4
-----------------------------------------------------*/
55. /* Write a C Program to accept two
matrices and check if they are equal */
219
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int A[10][10], B[10][10];
int i, j, R1, C1, R2, C2, flag =1;
220
221
printf("MATRIX A is\n");
for(i=0; i<R1; i++)
{
for(j=0; j<C1; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}
printf("MATRIX B is\n");
for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
printf("%3d",B[i][j]);
222
}
printf("\n");
}
223
break;
}
}
}
}
else
{ printf(" Cannot be compared\n");
exit(1);
}
if(flag == 1 )
printf("Two matrices are equal\n");
else
printf("But,two matrices are not
equal\n");
224
/*-----------------------------------------------------Output
Enter the order of the matrix A
22
Enter the order of the matrix B
22
Enter the elements of matrix A
12
34
Enter the elements of matrix B
12
34
MATRIX A is
1 2
3 4
MATRIX B is
225
1 2
3 4
Matrices can be compared
Two matrices are equal
-------------------------------------------------------*/
56. /* Write a C Program to check if a given
matrix is an identity matrix */
#include <stdio.h>
void main()
{
int A[10][10];
int i, j, R, C, flag =1;
226
227
printf("\n");
}
228
if(flag == 1 )
printf("It is identity matrix\n");
else
printf("It is not a identity matrix\n");
}
/*-----------------------------------------Output
Run 1
Enter the order of the matrix A
22
Enter the elements of matrix A
22
12
MATRIX A is
2 2
229
1 2
It is not a identity matrix
Run 2
Enter the order of the matrix A
22
Enter the elements of matrix A
10
01
MATRIX A is
1 0
0 1
It is identity matrix
------------------------------------------*/
56. /* Write a C Program to check if a given
matrix is an identity matrix */
230
#include <stdio.h>
void main()
{
int A[10][10];
int i, j, R, C, flag =1;
231
}
}
printf("MATRIX A is\n");
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}
232
if(flag == 1 )
printf("It is identity matrix\n");
else
printf("It is not a identity matrix\n");
}
/*------------------------------------------
233
Output
Run 1
Enter the order of the matrix A
22
Enter the elements of matrix A
22
12
MATRIX A is
2 2
1 2
It is not a identity matrix
Run 2
Enter the order of the matrix A
22
Enter the elements of matrix A
234
10
01
MATRIX A is
1 0
0 1
It is identity matrix
------------------------------------------*/
57. /* Write a C program to accept a matrix
of given order and
* interchnge any two rows and columns in
the original matrix*/
#include <stdio.h>
void main()
{
static int m1[10][10],m2[10][10];
235
int i,j,m,n,a,b,c, p, q, r;
236
for (i=0;i<m;++i)
{
c = m1[a-1][i];
has index is 0 */
/* first row
m1[a-1][i] = m1[b-1][i];
m1[b-1][i] = c;
}
237
{
for (j=0;j<n;++j)
printf (" %d",m2[i][j]);
printf ("\n");
}
for (i=0;i<n;++i)
{
r = m2[i][p-1];
column index is 0 */
/* first
m2[i][p-1] = m2[i][q-1];
m2[i][q-1] = r;
}
238
239
/
*---------------------------------------------------------------------Enter the order of the matrix
33
Enter the co-efficents of the matrix
124
579
306
Enter the numbers of two rows to be
exchnged
12
Enter the numbers of two columns to be
exchnged
23
The given matrix is
124
579
240
306
The matix after interchnging the two rows (in
the original matrix)
579
124
306
The matix after interchnging the two
columns(in the original matrix)
142
597
360
------------------------------------------------------------------------*/
241
#include <stdio.h>
void main()
{
long number, tempnum;
printf("Enter an integer\n");
scanf("%ld",&number);
tempnum = number;
number = number << 2; /*left shift
by two bits*/
printf("%ld x 4 = %ld\n",
tempnum,number);
}
/*-----------------------------Output
242
Enter an integer
15
15 x 4 = 60
RUN2
Enter an integer
262
262 x 4 = 1048
---------------------------------*/
& so on as An contains A1
#include <stdio.h>
243
void main ()
{
int i,n,number[30];
printf("Enter the value of the n = ");
scanf ("%d", &n);
printf ("Enter the numbers\n");
for (i=0; i<n; ++i)
{
scanf ("%d", &number[i]);
}
number[n] = number[0];
244
}
printf ("Cyclically permted numbers
are given below \n");
for (i=0; i<n; ++i)
printf ("%d\n", number[i]);
}
/*------------------------------------Output
Enter the value of the n = 5
Enter the numbers
10
30
20
45
18
Cyclically permted numbers are given below
30
245
20
45
18
10
---------------------------------------------------*/
60. /* Write a C program to accept a set of
numbers and arrange them *
* in a descending order
*/
#include <stdio.h>
void main ()
{
int number[30];
int i,j,a,n;
246
= number[i];
number[i] = number[j];
number[j] = a;
247
}
}
}
/* End of main() */
/*-----------------------------------------------Output
Enter the value of N
6
248
/*------------------------------------------------*/
249
#include <stdio.h>
void main ()
{
int number[30];
int i,j,a,n,counter,ave;
250
= number[i];
number[i] = number[j];
number[j] = a;
}
}
251
252
{
if (ave == number[i])
{
++counter;
}
}
if (counter == 0 )
printf ("The average of %d and %d is
= %d is not in the array\n", number[1],
number[n-2], ave);
else
printf ("The average of %d and %d in
array is %d in numbers\n",number[1],
number[n-2], counter);
}
/* End of main() */
/*------------------------------------------------------Output
Enter the value of N
253
6
Enter the numbers
30
80
10
40
70
90
The numbers arranged in descending order
are given below
90
80
70
40
30
10
The 2nd largest number is = 80
254
-------------------------------------------------------*/
62. /* Write a C program to accept an array of
integers and delete the *
* specified integer from the list
*/
#include <stdio.h>
void main()
{
int vectx[10];
int i, n, pos, element, found = 0;
255
256
if (found == 1)
{
for(i=pos; i< n-1; i++)
257
{
vectx[i] = vectx[i+1];
}
/* End of main() */
/*---------------------------------------------------
258
Output
Run 1
Enter how many elements
5
Enter the elements
30
10
50
20
40
Input array elements are
30
10
50
20
40
259
Run 2
Enter how many elements
4
Enter the elements
23
10
55
81
Input array elements are
23
10
55
260
81
Enter the element to be deleted
55
The resultant vector is
23
10
81
--------------------------------------------------------*/
63. /* Write a C programme (1-D Array) store
some elements in it.Accept key
& split from that point. Add the first
half to the end of second half*/
#include <stdio.h>
void main ()
261
{
int number[30];
int i,n,a,j;
262
number[n] = number[0];
/* End of main() */
/*------------------------------------------------Output
263
264
----------------------------------------------------------*/
64. /* Write a C program to find the frequency
of odd numbers *
* and even numbers in the input of a matrix
*/
#include <stdio.h>
void main ()
{
static int ma[10][10];
int i,j,m,n,even=0,odd=0;
265
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d", &ma[i][j]);
if ((ma[i][j]%2) == 0)
{
++even;
}
else
++odd;
}
}
printf ("The given matrix is\n");
for (i=0;i<m;++i)
{
266
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
/* End of main() */
/*----------------------------------------------------Output
Enter the order ofthe matrix
33
267
-------------------------------------------------------*/
65. /* Write a C program to accept a matrix of
order M x N and store its elements *
268
#include <stdio.h>
void main ()
{
if (m ==n )
269
{
printf ("Enter the co-efficients of the
matrix\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%dx%d",&ma[i][j]);
}
}
270
}
printf ("\n");
}
for (i=0;i<m;++i)
{
a = ma[i][i];
ma[i][i] = ma[i][m-i-1];
ma[i][m-i-1] = a;
}
271
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
}
else
printf ("The givan order is not
square matrix\n");
/* end of main() */
/*---------------------------------------------------Output
Enetr the order of the matix
33
Enter the co-efficients of the matrix
272
123
456
789
The given matrix is
123
456
789
THe matrix after changing the
main diagonal & secondary diagonal
321
456
987
----------------------------------------------------------*/
66. /* Write a C program to find the sum of
first 50 natural numbers *
* using for loop
*/
273
#include <stdio.h>
void main()
{
int num,sum=0;
/*---------------------------
274
Output
Sum = 1275
----------------------------*/
main() */
/* End of
#include <stdio.h>
void main()
{
int m,n;
N\n");
if(m == n )
printf("M and N are equal\n");
275
else
printf("M and N are not equal\n");
} /* End of main() */
/*-----------------------------------output
Enter the values for M and N
34 45
M and N are not equal
-------------------------------------*/
68. //* Write a C program to accept the height
of a person in centermeter and *
* categorize the person based on height as
taller, dwarf and
*
276
#include <stdio.h>
void main()
{
float ht;
277
else
printf("Abnormal height\n");
} /* End of main() */
/*----------------------------------Output
Enter the Height (in centimetres)
45
Dwarf
----------------------------------------*/
69. /* Write a C program to accept a
coordinate point in a XY coordinate system *
* and determine its quadrant
*/
#include <stdio.h>
278
void main()
{
int x,y;
Y\n");
279
} /* End of main() */
/*--------------------------------------------Output
RUN 1
Enter the values for X and Y
35
point (5,3) lies in the First quandrant
RUN 2
Enter the values for X and Y
280
-4
-7
point (-7, -4) lies in the Third quandrant
---------------------------------------------*/
#include <stdio.h>
void main()
{
int
fig_code;
281
float
side,base,length,bredth,height,area,radius;
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
switch(fig_code)
{
case 1: printf("Enter the radius\n");
scanf("%f",&radius);
282
area=3.142*radius*radius;
printf("Area of a circle=
%f\n", area);
break;
283
printf("Area of a
Triangle=%f\n", area);
break;
printf("Area of a Square=
break;
} /* End of switch */
} /* End of main() */
284
/*---------------------------------------------------Output
Run 1
------------------------1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
------------------------Enter the Figure code
2
Enter the bredth and length
2
6
Area of a Reactangle=12.000000
Run 2
285
------------------------------------------------------*/
286
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void main()
{
287
char remark[15];
char grade;
switch(grade)
{
SUPER");
288
/* End of switch */
printf("RESULT : %s\n",remark);
289
/* End of main() */
/*------------------------------------------Output
RUN 1
Enter the grade
s
RESULT : SUPER
RUN 2
Enter the grade
y
RESULT : ABSENT
-----------------------------------------------*/
290
#include <stdio.h>
void main()
{
int i,fact=1,num;
291
fact = fact * i;
}
}
/* End of else */
printf("Factorial of %d =%5d\n",
num,fact);
/* End of main() */
/*------------------------------------------Output
Enter the number
5
Factorial of 5 = 120
---------------------------------------------*/
292
73.
/* Write a C program to accept a string and
find the sum of *
* all digits present in the string
*/
#include <stdio.h>
void main()
{
char string[80];
int count,nc=0,sum=0;
for(count=0;string[count]!='\0';
count++)
{
293
if((string[count]>='0') &&
(string[count]<='9'))
{
nc += 1;
sum += (string[count] - '0');
}
}
/* End of if */
/* End of For */
/* End of main() */
/*----------------------------------------------------Output
Enter the string containing both digits and
alphabet
294
goodmorning25
NO. of Digits in the string=2
Sum of all digits=7
--------------------------------------------------------*/
74. /* Write a C program to accept an integer
find the sum of the digits in it */
#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
295
temp = num;
while(num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
/* End of main()*/
296
123456
Given number =123456
Sum of the digits 123456 =21
----------------------------------------*/
75. /* Write a C program to accept a matrix of
order M x N and find the sum *
* of each row and each column of a matrix
*/
#include <stdio.h>
void main ()
{
static int m1[10][10];
int i,j,m,n,sum=0;
297
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&m1[i][j]);
}
}
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
sum = sum + m1[i][j] ;
}
298
sum=0;
for (j=0;j<n;++j)
{
for (i=0;i<m;++i)
{
sum = sum+m1[i][j];
}
printf ("Sum of the %d column is =
%d\n", j,sum);
sum = 0;
}
299
/*End of main() */
/*--------------------------------------------------Output
Enter the order of the matrix
33
Enter the co-efficients of the matrix
123
456
789
Sum of the 0 row is = 6
Sum of the 1 row is = 15
Sum of the 2 row is = 24
Sum of the 0 column is = 12
Sum of the 1 column is = 15
Sum of the 2 column is = 18
----------------------------------------------------*/
300
#include <stdio.h>
void main ()
{
static int ma[10][10];
int i,j,m,n,a=0,sum=0;
if ( m == n )
301
{
printf ("Enter the co-efficients of the
matrix\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
}
}
302
for (i=0;i<m;++i)
{
sum = sum + ma[i][i];
a = a + ma[i][m-i-1];
}
303
/* End of main() */
/*-------------------------------------------------------Output
Enetr the order of the matix
33
Enter the co-efficients of the matrix
123
456
789
The given matrix is
123
456
789
304
Run 2
Enter the order of the matix
23
The givan order is not square matrix
--------------------------------------------------------*/
77. /* Write a C program to accept a matricx
of order MxN and find the trcae and *
* normal of a matrix HINT:Trace is defined as
the sum of main diagonal
*
* elements and Normal is defined as squre
root of the sum of all
*
* the elements
*/
305
#include <stdio.h>
#include <math.h>
void main ()
{
static int ma[10][10];
int
i,j,m,n,sum=0,sum1=0,a=0,normal;
306
{
scanf ("%d",&ma[i][j]);
a = ma[i][j]*ma[i][j];
sum1 = sum1+a;
}
}
normal = sqrt(sum1);
printf ("The normal of the given matrix
is = %d\n",normal);
for (i=0;i<m;++i)
{
sum = sum + ma[i][i];
}
printf ("Trace of the matrix is =
%d\n",sum);
/*End of main() */
307
/*--------------------------------------------------Output
Enter the order of the matrix
33
Enter the ncoefficients of the matrix
123
456
789
The normal of the given matrix is = 16
Trace of the matrix is = 15
Run 2
Enter the order of the matrix
22
Enter the ncoefficients of the matrix
24
68
308
#include <stdio.h>
void main ()
{
static int m1[10][10];
309
int i,j,m,n;
int counter=0;
310
}
}
}
if (counter>((m*n)/2))
{
printf ("The given matrix is sparse
matrix \n");
}
else
printf ("The given matrix is not a
sparse matrix \n");
/* EN dof main() */
/*---------------------------------------------Output
311
Run 2
Enter the order of the matix
33
Enter the co-efficients of the matix
100
001
010
The given matrix is sparse matrix
There are 6 number of zeros
312
-----------------------------------------------*/
79. /* Write a C program to accept a matric of
order MxN and sort all rows *
* of the matrix in ascending order and all
columns in descendng order */
#include <stdio.h>
void main ()
{
static int ma[10][10],mb[10][10];
int i,j,k,a,m,n;
313
314
}
printf ("\n");
}
315
}
}
}
}
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
316
{
for (i=0;i<m;++i)
{
for (k=i+1;k<m;++k)
{
if (mb[i][j] < mb[k][j])
{
a = mb[i][j];
mb[i][j] = mb[k][j];
mb[k][j] = a;
}
}
}
}
for (i=0;i<m;++i)
{
317
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("\n");
}
/*End of main() */
318
31
52
After arranging rows in ascending order
13
25
After arranging the columns in descending
order
52
31
-----------------------------------------------------*/
319
320