C Programs
C Programs
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int s, a, b, c, area;
clrscr();
ty
/* compute s*/
or
s = (a + b + c) / 2;
ab
area = sqrt ( s * (s-a) * (s-b) * (s-c));
kr
printf ("Area of a triangale = %d\n", area);
} ha
/*-----------------------------
Output
C
Enter the values of a,b and c
3
ar
4
5
um
Area of a triangale = 6
------------------------------*/
#include <stdio.h>
k
#include <conio.h>
#include <math.h>
ee
#define PI 3.142
bh
void main()
{
float radius, area;
A
clrscr();
/*-----------------------------
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
------------------------------*/
ty
3. /* Write a C program to find the simple interest , given principle, *
* rate of interest and times*/
or
#include <stdio.h>
#include <conio.h>
ab
void main()
kr
{
float p, r, si;
int t;
ha
C
clrscr();
si = (p * r * t)/ 100.0;
}
ee
/*-----------------------------
bh
Output
Enter the values of p,r and t
2000
A
8
3
Amount = Rs. 2000.00
Rate = Rs. 8.00%
Time = 3 years
Simple interest = 480.00
------------------------------*/
#include <stdio.h>
#include <conio.h>
void main()
{
int ival, remainder;
clrscr();
remainder = ival % 2;
ty
if (remainder == 0)
printf ("%d, is an even integer\n", ival);
or
else
printf ("%d, is an odd integer\n", ival);
ab
}
/*-----------------------------
kr
Output
RUN1
ha
Enter an integer :13
C
13, is an odd integer
RUN2
ar
---------------------------------*/
#include <stdio.h>
#include <conio.h>
ee
void main()
bh
{
int number;
clrscr();
A
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);
}
/*-----------------------------
Output
Enter a number
-5
-5, is a negative number
RUN2
Enter a number
89
89, is a positive number
------------------------------*/
#include <stdio.h>
ty
#include <conio.h>
#include <math.h>
or
void main()
{
ab
int a, b, c;
kr
clrscr();
if ( a > b)
ar
{
if ( a > c)
um
{
printf ("A is the greatest among three\n");
}
else
K
{
printf ("C is the greatest among three\n");
k
}
}
ee
else if (b > c)
{
bh
/*-----------------------------
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
B is the greatest among three
RUN3
Enter the values of a,b and c
30 20 10
a = 30 b = 20 c = 10
A is the greatest among three
ty
------------------------------*/
or
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*/
ab
#include <stdio.h>
kr
#include <conio.h>
#include <stdlib.h>
#include <math.h>
ha
void main()
C
{
float A, B, C, root1, root2;
float realp, imagp, disc;
ar
clrscr();
um
}
else
{
A
ty
}
or
} /* End of main() */
/*---------------------------
ab
Output
RUN 1
kr
Enter the values of A, B and C
321
Imaginary Roots
Root1 = -0.333333 +i 0.471405
ha
Root2 = -0.333333 -i 0.471405
C
RUN 2
Enter the values of A, B and C
ar
121
Roots are real and equal
um
Root1 = -1.000000
Root2 = -1.000000
RUN 3
K
Root2
= -1.000000
bh
A
---------------------------------*/
#include <stdio.h>
#include <conio.h>
void main()
{
char oper; /* oper is an operator to be selected */
float n1, n2, result;
clrscr();
fflush (stdin);
ty
scanf ("%c", &oper);
or
switch (oper)
{
ab
case '+': result = n1 + n2;
break;
kr
case '-': result = n1 - n2;
break;
case '*': result = n1 * n2;
break;
ha
case '/': result = n1 / n2;
C
break;
default : printf ("Error in operation\n");
break;
ar
}
um
}
/*-----------------------------
K
Output
Simulation of Simple Calculator
k
35
Enter the operator [+,-,*,/]
bh
RUN2
Simulation of Simple Calculator
RUN4
ty
Simulation of Simple Calculator
or
Enter two numbers
5
9
ab
Enter the operator [+,-,*,/]
/
kr
5.00 / 9.00= 0.56 ha
------------------------------*/
C
#include <stdio.h>
um
#include <conio.h>
void main()
{
K
int i, N, sum = 0;
k
clrscr();
ee
sum = sum + i;
}
/*----------------------------------------
Output
RUN1
RUN2
#include <stdio.h>
ty
void main()
or
{
int fib1=0, fib2=1, fib3, N, count=0;
ab
printf("Enter the value of N\n");
scanf("%d", &N);
kr
printf("First %d FIBONACCI numbers are ...\n", N);
printf("%d\n",fib1);
printf("%d\n",fib2);
ha
count = 2; /* fib1 and fib2 are already used */
C
while( count < N)
{
ar
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
}
K
} /* End of main() */
k
/*--------------------------
Enter the value of N
ee
10
First 5 FIBONACCI numbers are ...
bh
0
1
1
A
2
3
5
8
13
21
34
-------------------------------*/
11. /* Write a C program to find the GCD and LCM of two integers *
* output the results along with the given integers. Use Euclids' algorithm*/
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr();
ty
numerator = num1;
denominator = num2;
or
}
else
{
ab
numerator = num2;
denominator = num1;
kr
}
remainder = num1 % num2;
while(remainder !=0)
{
ha
numerator = denominator;
C
denominator = remainder;
remainder = numerator % denominator;
}
ar
gcd = denominator;
lcm = num1 * num2 / gcd;
um
Output
RUN 1
k
15
GCD of 5 and 15 = 5
bh
LCM of 5 and 15 = 15
------------------------------*/
A
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N, oddSum = 0, evenSum = 0;
clrscr();
printf("Enter the value of N\n");
scanf ("%d", &N);
ty
printf ("Sum of all even numbers = %d\n", evenSum);
}
or
/*-----------------------------
Output
RUN1
ab
Enter the value of N
kr
10
Sum of all odd numbers = 25
Sum of all even numbers = 30
ha
RUN2
C
Enter the value of N
50
Sum of all odd numbers = 625
ar
------------------------------*/
* message */
k
#include <stdio.h>
#include <conio.h>
ee
void main()
bh
{
int num, temp, digit, rev = 0;
A
clrscr();
printf("Enter an integer\n");
scanf("%d", &num);
while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
if(temp == rev )
printf("Number is a palindrome\n");
else
printf("Number is not a palindrome\n");
}
/*------------------------
Output
RUN 1
ty
Enter an integer
12321
or
Given number is = 12321
Its reverse is = 12321
Number is a palindrome
ab
RUN 2
kr
Enter an integer
3456
Given number is = 3456
Its reverse is = 6543
ha
Number is not a palindrome
C
-----------------------------------*/
14. /* Write a C program to find the value of sin(x) using the series *
ar
#include <stdio.h>
#include <conio.h>
#include <math.h>
K
#include <stdlib.h>
k
void main()
{
ee
int n, x1;
float acc, term, den, x, sinx=0, sinval;
bh
clrscr();
A
x1 = x;
x = x*(3.142/180.0);
sinval = sin(x);
do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;
ty
printf("Sum of the sine series = %f\n", sinx);
or
printf("Using Library function sin(%d) = %f\n", x1,sin(x));
} /*End of main() */
ab
/*------------------------------
kr
Output
Enter the value of x (in degrees)
30
Enter the accuary for the result
ha
0.000001
C
Sum of the sine series = 0.500059
Using Library function sin(30) = 0.500059
ar
RUN 2
Enter the value of x (in degrees)
um
45
Enter the accuary for the result
0.0001
Sum of the sine series = 0.707215
K
15. /* Write a C program to find the value of cos(x) using the series *
ee
#include <stdio.h>
#include <conio.h>
A
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float acc, term, den, x, cosx=0, cosval;
clrscr();
x = x*(3.142/180.0);
cosval = cos(x);
ty
do
or
{
den = 2*n*(2*n-1);
term = -term * x * x / den;
ab
cosx = cosx + term;
n = n + 1;
kr
} while(acc <= fabs(cosval - cosx));
30
Enter the accuary for the result
um
0.000001
Sum of the cosine series = 0.865991
Using Library function cos(30) = 0.865991
K
RUN 2
Enter the value of x (in degrees)
k
45
Enter the accuary for the result
ee
0.0001
Sum of the cosine series = 0.707031
bh
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
int num, j, flag;
clrscr();
printf("Enter a number\n");
scanf("%d", &num);
if ( num <= 1)
{
printf("%d is not a prime numbers\n", num);
exit(1);
}
flag = 0;
ty
for ( j=2; j<= num/2; j++)
{
or
if( ( num % j ) == 0)
{
flag = 1;
ab
break;
}
kr
}
if(flag == 0)
ha
printf("%d is a prime number\n",num);
else
C
printf("%d is not a prime number\n", num);
}
/*------------------------
ar
Output
RUN 1
um
Enter a number
34
34 is not a prime number
K
RUN 2
Enter a number
k
29
29 is a prime number
ee
-----------------------------*/
bh
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
int M, N, i, j, flag, temp, count = 0;
clrscr();
if(N < 2)
{
printf("There are no primes upto %d\n", N);
exit(0);
}
printf("Prime numbers are\n");
temp = M;
if ( M % 2 == 0)
{
M++;
ty
}
for (i=M; i<=N; i=i+2)
or
{
flag = 0;
ab
for (j=2; j<=i/2; j++)
{
kr
if( (i%j) == 0)
{
flag = 1;
break;
ha
}
C
}
if(flag == 0)
{
ar
printf("%d\n",i);
count++;
um
}
}
printf("Number of primes between %d and %d = %d\n",temp,N,count);
}
K
/*---------------------------------
Output
k
19
23
29
A
31
37
41
43
Number of primes between 15 and 45 = 8
-------------------------------------------*/
void main()
{
int i, N1, N2, count = 0, sum = 0;
clrscr();
ty
printf ("Integers divisible by 5 are\n");
or
for (i = N1; i < N2; i++)
{
if (i%5 == 0)
ab
{
printf("%3d,", i);
kr
count++;
sum = sum + i;
}
}
ha
C
printf ("\nNumber of integers divisible by 5 between %d and %d = %d\n",
N1,N2,count);
printf ("Sum of all integers that are divisible by 5 = %d\n", sum);
ar
} /* End of main()*/
um
/*-----------------------------
Output
Enter the value of N1 and N2
2
K
27
Integers divisible by 5 are
k
#include <stdio.h>
void main()
{
int i, N,num negsum=0, posum=0;
float total=0.0, averg;
clrscr();
ty
scanf("%d",&num);
fflush(stdin);
or
if(num < 0)
{
ab
negsum = negsum + num;
}
kr
else if(num > 0)
{
posum = posum + num;
}
ha
else if( num == 0)
C
{
;
}
ar
averg = total / N;
printf("\nSum of all negative numbers = %d\n",negsum);
printf("Sum of all positive numbers = %d\n", posum);
K
} /*End of main()*/
/*-------------------------------------
ee
Output
Enter the value of N
bh
5
Enter 5 numbers (-ve, +ve and zero)
5
A
-3
0
-7
6
Input array elements
+5
-3
+0
-7
+6
#include <stdio.h>
#include <conio.h>
ty
void main()
{
or
int array[10];
int i, N, keynum, found=0;
ab
clrscr();
kr
printf("Enter the value of N\n");
scanf("%d",&N); ha
printf("Enter the elements one by one\n");
for(i=0; i<N ; i++)
C
{
scanf("%d",&array[i]);
}
ar
{
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\n");
K
scanf("%d", &keynum);
k
{
if( keynum == array[i] )
bh
{
found = 1;
break;
A
}
}
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
89
Input array is
23
12
56
43
89
ty
Enter the element to be searched
56
or
SUCCESSFUL SEARCH
RUN 2
ab
Enter the value of N
3
kr
Enter the elements one by one
456
213
879
ha
Input array is
C
456
213
879
ar
Search is FAILED
--------------------------------------*/
* using Bubble sort and print both the given and the sorted *
* array with suitable headings */
k
#include <stdio.h>
ee
#include <conio.h>
#define MAXSIZE 10
bh
void main()
{
A
int array[MAXSIZE];
int i, j, N, temp;
clrscr();
ty
temp = array[j];
array[j] = array[j+1];
or
array[j+1] = temp;
}
}
ab
}
printf("Sorted array is...\n");
kr
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
ha
} /* End of main*/
C
/*----------------------------------
Output
ar
876
345
k
Input array is
390
ee
234
111
bh
876
345
Sorted array is...
A
111
234
345
390
876
---------------------------*/
#include <stdio.h>
#include <conio.h>
void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;
clrscr();
ty
printf("Enter the elements one by one\n");
for(i=0; i<N ; i++)
or
{
scanf("%d",&array[i]);
}
ab
printf("Input array elements\n");
for(i=0; i<N ; i++)
kr
{
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
ha
for(i=0; i< N ; i++)
C
{
for(j=0; j< (N-i-1) ; j++)
{
ar
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
K
}
}
k
{
printf("%d\n",array[i]);
bh
scanf("%d", &keynum);
low=1;
high=N;
do
{
mid= (low + high) / 2;
if ( keynum < array[mid] )
high = mid - 1;
else if ( keynum > array[mid])
low = mid + 1;
} while( keynum!=array[mid] && low <= high); /* End of do- while */
ty
} /* End of main*/
/*----------------------------------
or
Output
Enter the value of N
4
ab
Enter the elements one by one
3
kr
1
4
2
Input array elements
ha
3
C
1
4
2
ar
2
3
4
Enter the element to be searched
K
4
SUCCESSFUL SEARCH
k
---------------------------*/
ee
#include <stdio.h>
#include <conio.h>
A
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;
clrscr();
ty
}
avrg = sum /(float) n;
or
/* Compute varaience and standard deviation */
ab
for(i=0; i<n; i++)
{
kr
sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;
SD = sqrt(var);
ha
C
printf("Average of all elements = %.2f\n", avrg);
printf("Varience of all elements = %.2f\n", var);
printf("Standard deviation = %.2f\n", SD);
ar
} /*End of main()*/
/*--------------------------
um
Output
Enter the value of N
6
Enter 6 real numbers
K
12
34
k
10
50
ee
42
33
bh
-------------------------------------*/
#include <stdio.h>
#include <conio.h>
#define MAX 4
void main()
{
int a[MAX], i, l1,l2,temp;
clrscr();
ty
{
printf("%5d", a[i]);
or
}
printf("\n");
ab
l1 = a[0]; /*assume first element of array is the first largest*/
kr
l2 = a[1]; /*assume first element of array is the second largest*/
for (i=2;i<4;i++)
um
{
if (a[i] >= l1)
{
l2 = l1;
K
l1 = a[i];
}
k
l2= a[i];
}
bh
}
/*-----------------------------------
Output
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
90
54
ty
67
Input interger are
or
12 90 54 67
ab
67 is the second largest
kr
Average of 90 and 67 = 78
RUN 3
Enter 4 integer numbers
ha
100
C
200
300
400
ar
------------------------------------*/
ee
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 10
voidmain()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;
clrscr();
printf("Enter %d coefficients\n",N+1);
for (i=0;i <= N;i++)
ty
{
scanf("%d",&a[i]);
or
}
polySum = a[0];
ab
for (i=1;i<= N;i++)
kr
{
polySum = polySum * x + a[i];
}
ha
power = N;
C
/*power--;*/
ar
{
if (power < 0)
{
break;
K
}
k
printf(" + ");
else if (a[i] < 0)
bh
printf(" - ");
else
printf (" ");
A
printf("%dx^%d ",abs(a[i]),power--);
/*-----------------------------------------------------
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
4
ty
Enter the value of x
1
or
Enter 5 coefficients
3
-5
ab
6
8
kr
-9
Given polynomial is:
+ 3x^4 - 5x^3 + 6x^2 + 8x^1 - 9x^0
Sum of the polynomial = 3.00
ha
-----------------------------------------------------*/
C
26. /* Write a C program to read two matrices A (MxN) and B(MxN)*
* and perform addition OR subtraction of A and B. Find the *
ar
#include <stdio.h>
#include <conio.h>
K
void main()
{
k
clrscr();
A
ty
for(j=0; j<N; j++)
{
or
scanf("%d",&B[i][j]);
}
}
ab
printf("MATRIX B is\n");
kr
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
ha
printf("%3d",B[i][j]);
C
}
printf("\n");
}
ar
scanf("%d",&option);
switch (option)
{
K
ty
printf("%3d",diffmat[i][j]) ;
}
or
printf("\n");
}
ab
trace (diffmat, M, N);
break;
kr
}
} /* End of main() */
ha
/*Function to find the trace of a given matrix and print it*/
C
void trace (int arr[][10], int M, int N)
{
ar
int i, j, trace = 0;
for(i=0; i<M; i++)
um
{
for(j=0; j<N; j++)
{
if (i==j)
K
{
trace = trace + arr[i][j];
k
}
}
ee
}
printf ("Trace of the resultant matrix is = %d\n", trace);
bh
}
/*-----------------------------------
A
ty
#include <stdio.h>
or
#include <conio.h>
void main()
ab
{
int i,j,M,N;
kr
int A[10][10], B[10][10];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
K
scanf("%d",&A[i][j]);
}
k
}
ee
printf("Matrix A is\n");
for(i=0;i<M;i++)
bh
{
for(j=0;j<N;j++)
{
A
printf("%3d",A[i][j]);
}
printf("\n");
}
} /*End of main()*/
ty
/*---------------------------------------
Output
or
Enter the order of matrix A
33
Enter the elements of matrix
ab
1
2
kr
3
4
5
6
ha
7
C
8
9
MatrixxA is
ar
1 2 3
4 5 6
um
7 8 9
Its Transpose is
1 4 7
2 5 8
K
3 6 9
-----------------------------*/
k
#include <stdio.h>
#include <conio.h>
A
#include <string.h>
void main()
{
char string[25], revString[25]={'\0'};
int i,length = 0, flag = 0;
clrscr();
fflush(stdin);
printf("Enter a string\n");
gets(string);
/*Compare the input string and its reverse. If both are equal
ty
then the input string is palindrome. Otherwise it is
not a palindrome */
or
for (i=0; i < length ; i++)
{
ab
if (revString[i] == string[i])
flag = 1;
kr
else
flag = 0;
}
ha
if (flag == 1)
C
printf ("%s is a palindrome\n", string);
else
printf("%s is not a palindrome\n", string);
ar
} /*End of main()*/
um
/*----------------------------------------------------
Output
RUN 1
K
Enter a string
madam
k
madam is a palindrome
ee
RUN 2
Enter a string
bh
Madam
Madam is not a palindrome
A
RUN 3
Enter a string
good
good is not a palindrome
----------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string1[20], string2[20];
int i,j,pos;
ty
printf("Enter the second string:");
or
gets(string2);
ab
printf("Second string = %s\n", string2);
kr
/*To concate the second stribg to the end of the string
travserse the first to its end and attach the second string*/
ha
for (i=0; string1[i] != '\0'; i++)
{
C
; /*null statement: simply trvsering the string1*/
}
ar
pos = i;
um
}
/*---------------------------------------
bh
Output
Enter the first string :CD-
Enter the second string:ROM
A
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
ty
}
or
sentence[i]='\0';
ab
printf("The given sentence is : %s",sentence);
kr
printf("\nCase changed sentence is: ");
for(i=0; i < count; i++)
{
ha
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
C
putchar(ch);
}
ar
} /*End of main()*/
/*------------------------------
um
Output
Enter a sentence
Mera Bharat Mahan
The given sentence is : Mera Bhaaat Mahan
K
#include <stdio.h>
#include <conio.h>
A
void main()
{
char sentence[80];
int i, vowels=0, consonants=0, special = 0;
clrscr();
printf("Enter a sentence\n");
gets(sentence);
ty
{
special = special + 1;
or
}
}
ab
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
kr
printf("No. of consonants in %s = %d\n", sentence, consonants);
}
/*----------------------------------------
ha
Output
C
Enter a sentence
Good Morning
No. of vowels in Good Morning = 4
ar
#include <stdio.h>
#include <conio.h>
ee
#include <string.h>
bh
void main()
{
char name[10][8], Tname[10][8], temp[8];
A
int i, j, N;
clrscr();
ty
}
or
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
ab
for(i=0; i< N ; i++)
{
kr
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");
ha
} /* End of main() */
C
/*--------------------------------
Output
ar
Enter 3 names
Monica
Laxmi
Anand
K
----------------------------------------
k
Monica Anand
Laxmi Laxmi
bh
Anand Monica
----------------------------------------
---------------------------------------- */
A
void main()
{
int A[MAXROWS][MAXCOLS], B[MAXROWS][MAXCOLS], C[MAXROWS][MAXCOLS];
int M, N;
/*Function declarations*/
ty
void printMatrix(int arr[][MAXCOLS], int M, int N);
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
or
int M, int N);
clrscr();
ab
printf("Enter the value of M and N\n");
kr
scanf("%d %d",&M, &N);
printf ("Enter matrix A\n");
readMatrix(A,M,N);
printf("Matrix A\n");
ha
printMatrix(A,M,N);
C
printf ("Enter matrix B\n");
readMatrix(B,M,N);
ar
printf("Matrix B\n");
printMatrix(B,M,N);
um
productMatrix(A,B,C, M,N);
printMatrix(C,M,N);
}
k
int i, j;
for(i=0; i< M ; i++)
{
A
/* 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++)
ty
{
for ( j=0; j < N; j++)
or
{
C[i][j] = 0 ;
for (k=0; k < N; k++)
ab
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
kr
}
}
}
}
ha
/*---------------------------------------------
C
Output
Enter the value of M and N
33
ar
Enter matrix A
111
um
222
333
Matrix A
1 1 1
K
2 2 2
3 3 3
k
Enter matrix B
123
ee
456
789
bh
Matrix B
1 2 3
4 5 6
A
7 8 9
The product matrix is
12 15 18
24 30 36
36 45 54
--------------------------------*/
void main()
{
float M,N;
ty
printf ("Before Swapping:M = %5.2f\tN = %5.2f\n", M,N);
swap(&M, &N);
or
printf ("After Swapping:M = %5.2f\tN = %5.2f\n", M,N);
ab
} /* End of main() */
kr
/* Function swap - to interchanges teh contents of two items*/
void swap(float *ptr1, float *ptr2 )
{
float temp;
ha
temp=*ptr1;
C
*ptr1=*ptr2;
*ptr2=temp;
ar
} /* End of Function */
um
/* ----------------------------------------
Output
Enter the values of M and N
K
32 29
Before Swapping:M = 32.00 N = 29.00
k
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
void main()
{
int i,n,sum=0;
int *a;
clrscr();
printf("Enter the size of array A\n");
scanf("%d", &n);
ty
/*Compute the sum of all elements in the given array*/
for(i=0;i<n;i++)
or
{
sum = sum + *(a+i);
ab
}
kr
} /* End of main() */
ha
/*----------------------------
C
Output
Enter the size of array A
4
ar
20
30
40
Sum of all elements in array = 100
K
-------------------------------------*/
k
36. /* Write a C program to convert the given binary number into decimal */
ee
#include <stdio.h>
bh
void main()
{
int num, bnum, dec = 0, base = 1, rem ;
A
bnum = num;
} /* End of main() */
/*---------------------------------------------
Output
Enter a binary number(1s and 0s)
10101
The Binary number is = 10101
ty
Its decimal equivalent is =21
----------------------------------------------*/
or
37. /* Program to accept N integer number and store them in an array AR.
ab
* The odd elements in the AR are copied into OAR and other elements
* are copied into EAR. Display the contents of OAR and EAR
kr
*/
#include <stdio.h>
void main()
ha
{
C
long int ARR[10], OAR[10], EAR[10];
int i,j=0,k=0,n;
ar
scanf("%ld",&ARR[i]);
fflush(stdin);
k
}
/*Copy odd and even elemets into their respective arrays*/
ee
for(i=0;i<n;i++)
{
bh
if (ARR[i]%2 == 0)
{
EAR[j] = ARR[i];
A
j++;
}
else
{
OAR[k] = ARR[i];
k++;
}
}
ty
Enter the elements of the array
12
or
345
678
899
ab
900
111
kr
The elements of OAR are
345
899
111
ha
The elements of EAR are
C
12
678
900
ar
---------------------------------------*/
um
#include <stdio.h>
K
void main()
k
{
int fib1=0, fib2=1, fib3, limit, count=0;
ee
scanf("%d", &limit);
printf("%d\n",fib1);
printf("%d\n",fib2);
count = 2; /* fib1 and fib2 are already used */
ty
-----------------------------------------------------------*/
or
39. /* Write a C program to insert a particular element in a specified position *
* in a given array */
ab
#include <stdio.h>
#include <conio.h>
kr
void main()
{
int x[10];
ha
int i, j, n, m, temp, key, pos;
C
clrscr();
ar
scanf("%d", &x[i]);
}
k
printf("%d\n", x[i]);
}
A
ty
{
pos = i;
or
break;
}
}
ab
m = n - pos + 1 ;
kr
for(i=0; i<= m ; i++)
{
x[n-i+2] = x[n-i+1] ;
ha
}
C
x[pos] = key;
ar
{
printf("%d\n", x[i]);
}
} /* End of main() */
K
/*-------------------------------------
k
Output
Enter how many elements
ee
5
Enter the elements
bh
2
14
67
A
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
Final list is
2
14
29
34
67
83
-------------------------------------------------*/
ty
40. /* Write a C program to accept an integer and reverse it */
or
#include <stdio.h>
ab
void main()
{
kr
long num, rev = 0, temp, digit;
while(num > 0)
ar
{
digit = num % 10;
um
}
ee
/* ---------------------------
Output
bh
#include <stdio.h>
#include <conio.h>
void main()
{
char pasword[10],usrname[10], ch;
int i;
clrscr();
for(i=0;i<8;i++)
{
ty
ch = getch();
pasword[i] = ch;
or
ch = '*' ;
printf("%c",ch);
}
ab
pasword[i] = '\0';
kr
/*If you want to know what you have entered as password, you can print it*/
printf("\nYour password is :");
ha
for(i=0;i<8;i++)
C
{
printf("%c",pasword[i]);
}
ar
}
um
/*-----------------------------------------------
Output
Enter User name: Latha
Enter the password <any 8 characters>: ********
K
#include<stdio.h>
#include<conio.h>
A
void main()
{
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')
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
ty
{
flag=1;
or
if (str[j]!=search[j-i])
{
ab
flag=0;
break;
}
kr
}
if (flag==1) ha
break;
}
if (flag==1)
C
puts("SEARCH SUCCESSFUL!");
else
ar
puts("SEARCH UNSUCCESSFUL!");
getch();
}
um
/*------------------------------
Output
Enter a string:
K
SEARCH SUCCESSFUL!
ee
------------------------------*/
* volume of a cube */
A
#include <stdio.h>
#include <math.h>
void main()
{
float side, surfArea, volume;
/*------------------------------------------
Output
Enter the llngth of a side
4
Surface area = 96.00 and Volume = 64.00
RUN2
Enter the length of a side
ty
12.5
Surface area = 937.50 and Volume = 1953.12
or
------------------------------------------*/
ab
* and count the number of 1's in the binary number */
kr
#include <stdio.h>
void main()
{
ha
long num, dnum, rem, base = 1, bin = 0, no_of_1s = 0;
C
printf("Enter a decimal integer\n");
scanf("%ld", &num);
ar
dnum = num;
um
no_of_1s++;
}
ee
} /* End of main() */
/*--------------------------------------------------
Output
Enter a decimal integer
75
Input number is = 75
Its Binary equivalent is = 1001011
No.of 1's in the binary number is = 4
RUN2
Enter a decimal integer
128
Input number is = 128
Its Binary equivalent is = 10000000
No.of 1's in the binary number is = 1
-----------------------------------------------------*/
ty
#include <stdio.h>
or
void main()
{
ab
int year;
kr
printf("Enter a year\n");
scanf("%d",&year); ha
if ( (year % 4) == 0)
printf("%d is a leap year",year);
C
else
printf("%d is not a leap year\n",year);
}
ar
/*------------------------------------------
um
Output
Enter a year
2000
2000 is a leap year
K
RUN2
k
Enter a year
2007
ee
#include <stdio.h>
void main()
{
long i,k;
i = i^k;
k = i^k;
i = i^k;
/*------------------------------------------
Output
Enter two integers
ty
23 34
or
Before swapping i= 23 and k = 34
After swapping i= 34 and k = 23
------------------------------------------*/
ab
47. /* Write a c program to convert given number of days to a measure of time
kr
* given in years, weeks and days. For example 375 days is equal to 1 year
* 1 week and 3 days (ignore leap year) */ ha
#include <stdio.h>
#define DAYSINWEEK 7
C
void main()
{
ar
year = ndays/365;
K
/*-----------------------------------------------
Output
A
#include<stdio.h>
#include<conio.h>
void main()
{
int count1=0,count2=0,flag=0,i;
char str1[10],str2[10];
ty
clrscr();
or
puts("Enter a string:");
gets(str1);
ab
puts("Enter another string:");
gets(str2);
kr
/*Count the number of characters in str1*/
while (str1[count1]!='\0')
count1++;
/*Count the number of characters in str2*/
ha
while (str2[count2]!='\0')
C
count2++;
i=0;
ar
/*The string comparison starts with thh first character in each string and
continues with subsequent characters until the corresponding characters
um
if (str1[i] == str2[i])
{
k
i++;
continue;
ee
}
if (str1[i]<str2[i])
bh
{
flag = -1;
break;
A
}
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)
printf("String1 is less than string2\n", str1, str2);
getch();
}
/*----------------------------------------
Output
Enter a string:
happy
Enter another string:
HAPPY
String1 is greater than string2
RUN2
ty
Enter a string:
Hello
or
Enter another string:
Hello
Both strings are equal
ab
RUN3
kr
Enter a string:
gold
Enter another string:
silver
ha
String1 is less than string2
C
----------------------------------------*/
ar
#include <stdio.h>
#include <string.h>
K
void main()
k
{
char string1[20], string2[20];
ee
int i,j,pos;
bh
pos = i;
ty
string1[i]='\0'; /*set the last character of string1 to NULL*/
or
printf("Concatenated string = %s\n", string1);
}
ab
/*---------------------------------------
Output
kr
Enter the first string :CD-
Enter the second string:ROM
First string = CD-
Second string = ROM
ha
Concatenated string = CD-ROM
C
----------------------------------------*/
#include <stdio.h>
void main()
{
K
char string[50];
int i, length = 0;
k
printf("Enter a string\n");
ee
gets(string);
bh
}
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
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>
ty
#include <string.h>
or
void main()
{
char string[25], revString[25]={'\0'};
ab
int i,length = 0, flag = 0;
kr
clrscr();
fflush(stdin); ha
printf("Enter a string\n");
gets(string);
C
for (i=0; string[i] != '\0'; i++) /*keep going through each */
{ /*character of the string */
ar
revString[length-i-1] = string[i];
}
k
/*Compare the input string and its reverse. If both are equal
then the input string is palindrome. Otherwise it is
ee
not a palindrome */
bh
flag = 1;
else
flag = 0;
if (flag == 1)
printf ("%s is a palindrome\n", string);
else
printf("%s is not a palindrome\n", string);
}
/*----------------------------------------------------
Output
Enter a string
madam
The length of the string 'madam' = 5
madam is a palindrome
RUN2
Enter a string
good
The length of the string 'good' = 4
good is not a palindrome
ty
----------------------------------------------------------*/
or
52. /* Write a C program to accept a string and find the number of times the word
'the'
* appears in it*/
ab
#include<stdio.h>
kr
#include<conio.h>
void main()
{
ha
int count=0,i,times=0,t,h,e,space;
C
char str[100];
clrscr();
ar
puts("Enter a string:");
gets(str);
um
count++;
}
k
{
t=(str[i]=='t'||str[i]=='T');
bh
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');
A
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
---------------------------------------------------------*/
53. /* Write a C program to compute the value of X ^ N given X and N as inputs */
#include <stdio.h>
#include <math.h>
void main()
{
long int x,n,xpown;
long int power(int x, int n);
ty
scanf("%ld %ld",&x,&n);
or
xpown = power (x,n);
ab
}
kr
/*Recursive function to computer the X to power N*/
else
return (x*power(x, n-1)); /* if n is odd*/
um
}
/*-------------------------------------
Output
Enter the values of X and N
K
25
X to the power N = 32
k
RUN2
ee
RUN3
A
RUN4
Enter the values of X and N
10 5
X to the power N = 100000
-----------------------------------------*/
void main()
{
/* Function declarations */
void readmatA();
void printmatA();
ty
void readmatB();
void printmatB();
or
void sum();
void diff();
ab
printf("Enter the order of the matrix A\n");
scanf("%d %d", &R1, &C1);
kr
printf("Enter the order of the matrix B\n");
scanf("%d %d", &R2,&C2);
ha
if( R1 != R2 && C1 != C2)
C
{
printf("Addition and subtraction are possible\n");
exit(1);
ar
}
else
um
{
printf("Enter the elements of matrix A\n");
readmatA();
printf("MATRIX A is\n");
K
printmatA();
k
printf("MATRIX B is\n");
printmatB();
bh
sum();
diff();
A
}
} /* main() */
ty
}
}
or
/* Function to print a matrix A */
void printmatA()
ab
{
for(i=0; i<R1; i++)
kr
{
for(j=0; j<C1; j++)
{
printf("%3d",A[i][j]);
ha
}
C
printf("\n");
}
}
ar
void printmatB()
{
for(i=0; i<R2; i++)
{
K
printf("%3d",B[i][j]);
}
ee
printf("\n");
}
bh
}
/*Function to find the sum of elements of matrix A and Matrix B*/
void sum()
A
{
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");
for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
printf("%3d",sumat[i][j]) ;
}
printf("\n");
}
return;
}
ty
for(j=0; j<C2; j++)
{
or
diffmat[i][j] = A[i][j] - B[i][j];
}
}
ab
printf("Difference matrix is\n");
kr
for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
ha
printf("%3d",diffmat[i][j]);
C
}
printf("\n");
}
ar
return;
}
um
/*---------------------------------------------------
Output
Enter the order of the matrix A
K
22
Enter the order of the matrix B
k
22
Enter the elements of matrix A
ee
1
2
bh
3
4
MATRIX A is
A
1 2
3 4
Enter the elements of matrix B
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 */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
ty
{
int A[10][10], B[10][10];
or
int i, j, R1, C1, R2, C2, flag =1;
ab
scanf("%d %d", &R1, &C1);
kr
printf("Enter the order of the matrix B\n");
scanf("%d %d", &R2,&C2); ha
printf("Enter the elements of matrix A\n");
for(i=0; i<R1; i++)
C
{
for(j=0; j<C1; j++)
{
ar
scanf("%d",&A[i][j]);
}
um
{
for(j=0; j<C2; j++)
k
{
scanf("%d",&B[i][j]);
ee
}
}
bh
printf("MATRIX A is\n");
for(i=0; i<R1; i++)
A
{
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]);
}
printf("\n");
}
ty
for(j=0; j<C2; j++)
{
or
if(A[i][j] != B[i][j])
{
flag = 0;
ab
break;
}
kr
}
}
}
else
ha
{ printf(" Cannot be compared\n");
C
exit(1);
}
ar
if(flag == 1 )
printf("Two matrices are equal\n");
um
else
printf("But,two matrices are not equal\n");
}
K
/*------------------------------------------------------
k
Output
Enter the order of the matrix A
ee
22
Enter the order of the matrix B
bh
22
Enter the elements of matrix A
12
A
34
Enter the elements of matrix B
12
34
MATRIX A is
1 2
3 4
MATRIX B is
1 2
3 4
Matrices can be compared
Two matrices are equal
-------------------------------------------------------*/
#include <stdio.h>
void main()
{
int A[10][10];
int i, j, R, C, flag =1;
ty
printf("Enter the elements of matrix A\n");
or
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
ab
{
scanf("%d",&A[i][j]);
kr
}
}
printf("MATRIX A is\n");
for(i=0; i<R; i++)
ha
{
C
for(j=0; j<C; j++)
{
printf("%3d",A[i][j]);
ar
}
printf("\n");
um
flag = 0;
break;
}
A
}
}
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
1 2
It is not a identity matrix
Run 2
Enter the order of the matrix A
22
ty
Enter the elements of matrix A
10
or
01
MATRIX A is
1 0
ab
0 1
It is identity matrix
kr
------------------------------------------*/
void main()
ar
{
static int m1[10][10],m2[10][10];
um
int i,j,m,n,a,b,c, p, q, r;
for (j=0;j<n;++j)
{
bh
scanf ("%d,",&m1[i][j]);
m2[i][j] = m1[i][j];
}
A
for (i=0;i<m;++i)
{
c = m1[a-1][i]; /* first row has index is 0 */
m1[a-1][i] = m1[b-1][i];
m1[b-1][i] = c;
}
printf ("Enter the numbers of two columns to be exchnged\n");
scanf ("%d %d",&p,&q);
for (i=0;i<n;++i)
{
ty
r = m2[i][p-1]; /* first column index is 0 */
m2[i][p-1] = m2[i][q-1];
or
m2[i][q-1] = r;
}
ab
printf ("The matix after interchnging the two rows(in the original matrix)\n");
for (i=0; i<m; ++i)
kr
{
for (j=0; j<n; ++j)
{
printf (" %d",m1[i][j]);
ha
}
C
printf ("\n");
}
ar
printf("The matix after interchnging the two columns(in the original matrix)\n");
for (i=0;i<m;++i)
um
{
for (j=0;j<n;++j)
printf (" %d", m2[i][j]);
printf ("\n");
K
}
}
k
/*-----------------------------------------------------------------------
ee
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
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
-------------------------------------------------------------------------*/
#include <stdio.h>
ty
#include <conio.h>
or
void main()
{
struct date
ab
{
int day;
kr
int month;
int year;
};
ha
struct details
C
{
char name[20];
int price;
ar
int code;
int qty;
um
int n,i;
k
clrscr();
ee
fflush(stdin);
for(i=0;i<n;i++)
A
{
fflush(stdin);
printf("Item name:");
scanf("%[^\n]",item[i].name);
fflush(stdin);
printf("Item code:");
scanf("%d",&item[i].code);
fflush(stdin);
printf("Quantity:");
scanf("%d",&item[i].qty);
fflush(stdin);
printf("price:");
scanf("%d",&item[i].price);
fflush(stdin);
printf("Manufacturing date(dd-mm-yyyy):");
scanf("%d-%d-%d",&item[i].mfg.day,&item[i].mfg.month,&item[i].mfg.year);
}
printf(" ***** INVENTORY *****\n");
printf("------------------------------------------------------------------\n");
printf("S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE\n");
printf("------------------------------------------------------------------\n");
ty
for(i=0;i<n;i++)
printf("%d %-15s %-d %-5d %-5d
or
%d/%d/%d\n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price,
item[i].mfg.day,item[i].mfg.month,item[i].mfg.year);
printf("------------------------------------------------------------------\n");
ab
getch();
}
kr
/*------------------------------------------------------
Enter number of items:5
Item name:Tea Powder
ha
Item code:123
C
Quantity:23
price:40
Manufacturing date(dd-mm-yyyy):12-03-2007
ar
Quantity:20
price:80
Manufacturing date(dd-mm-yyyy):30-03-2007
Item name:Soap Powder
K
Item code:510
Quantity:10
k
price:30
Manufacturing date(dd-mm-yyyy):01-04-2007
ee
Quantity:25
price:12
Manufacturing date(dd-mm-yyyy):10-03-2007
A
Item name:Shampo
Item code:777
Quantity:8
price:50
Manufacturing date(dd-mm-yyyy):17-05-2007
***** INVENTORY *****
------------------------------------------------------------------------
S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE
------------------------------------------------------------------------
1 Tea Powder 123 23 40 12/3/2007
2 Milk Powder 345 20 80 30/3/2007
3 Soap Powder 510 10 30 1/4/2007
4 Washing Soap 890 25 12 10/3/2007
5 Shampo 777 8 50 17/5/2007
------------------------------------------------------------------------
--------------------------------------------------------------------*/
59. /* Write a C program to convert the given binary number into its equivalent
decimal */
#include <stdio.h>
void main()
{
ty
int num, bnum, dec = 0, base = 1, rem ;
or
printf("Enter the binary number(1s and 0s)\n");
scanf("%d", &num); /*Enter maximum five digits or use long int*/
ab
bnum = num;
kr
while( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
ha
num = num / 10 ;
C
base = base * 2;
}
ar
} /* End of main() */
/*---------------------------------------------------
K
Outpput
Enter the binary number(1s and 0s)
k
1010
The Binary number is = 1010
ee
#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
Enter an integer
15
15 x 4 = 60
RUN2
Enter an integer
262
262 x 4 = 1048
ty
---------------------------------*/
or
58. /* Writ a C programme to cyclically permute the elements of an array A. *
ab
i.e. the content of A1 become that of A2.And A2 contains that of A3 *
& so on as An contains A1 */
kr
#include <stdio.h> ha
void main ()
{
C
int i,n,number[30];
printf("Enter the value of the n = ");
scanf ("%d", &n);
ar
{
scanf ("%d", &number[i]);
}
number[n] = number[0];
K
{
number[i] = number[i+1];
ee
}
printf ("Cyclically permted numbers are given below \n");
bh
/*-------------------------------------
Output
Enter the value of the n = 5
Enter the numbers
10
30
20
45
18
Cyclically permted numbers are given below
30
20
45
18
10
---------------------------------------------------*/
#include <stdio.h>
void main ()
{
ty
int i,j,a,n,number[30];
or
printf ("Enter the value of N\n");
scanf ("%d", &n);
ab
printf ("Enter the numbers \n");
for (i=0; i<n; ++i)
kr
scanf ("%d",&number[i]);
a= number[i];
number[i] = number[j];
um
number[j] = a;
}
}
}
K
} /* End of main() */
bh
/*------------------------------------------------------------------
Output
Enter the value of N
A
5
Enter the numbers
80
20
67
10
45
The numbers arrenged in ascending order are given below
10
20
45
67
80
------------------------------------------------------------------*/
#include <stdio.h>
void main ()
{
int number[30];
int i,j,a,n;
ty
printf ("Enter the value of N\n");
scanf ("%d", &n);
or
printf ("Enter the numbers \n");
for (i=0; i<n; ++i)
ab
scanf ("%d",&number[i]);
kr
/* sorting begins ...*/
for (i=0; i<n; ++i)
{
for (j=i+1; j<n; ++j)
ha
{
C
if (number[i] < number[j])
{
a = number[i];
ar
number[i] = number[j];
number[j] = a;
um
}
}
}
K
{
printf ("%d\n",number[i]);
ee
}
bh
} /* End of main() */
/*------------------------------------------------
A
Output
Enter the value of N
6
Enter the numbers
10
35
67
100
42
688
The numbers arranged in descending order are given below
688
100
67
42
35
10
/*------------------------------------------------*/
60. /* Write a C program to accept a list of data items and find the second *
largest and second smallest elements in it. And also computer the average *
of both. And search for the average value whether it is present in the *
array or not. Display appropriate message on successful search. */
ty
#include <stdio.h>
or
void main ()
{
int number[30];
ab
int i,j,a,n,counter,ave;
kr
printf ("Enter the value of N\n");
scanf ("%d", &n); ha
printf ("Enter the numbers \n");
for (i=0; i<n; ++i)
C
scanf ("%d",&number[i]);
{
for (j=i+1; j<n; ++j)
um
{
if (number[i] < number[j])
{
a = number[i];
K
number[i] = number[j];
number[j] = a;
k
}
}
ee
}
bh
printf ("%d\n",number[i]);
}
/*-------------------------------------------------------
ty
Output
Enter the value of N
or
6
Enter the numbers
30
ab
80
10
kr
40
70
90
The numbers arranged in descending order are given below
ha
90
C
80
70
40
ar
30
10
um
-------------------------------------------------------*/
k
#include <stdio.h>
void main()
A
{
int vectx[10];
int i, n, pos, element, found = 0;
ty
{
found = 1;
or
pos = i;
break;
}
ab
}
kr
if (found == 1)
{
for(i=pos; i< n-1; i++)
{
ha
vectx[i] = vectx[i+1];
C
}
printf("%d\n",vectx[i]);
}
}
else
K
} /* End of main() */
ee
/*---------------------------------------------------
Output
bh
Run 1
Enter how many elements
A
5
Enter the elements
30
10
50
20
40
Input array elements are
30
10
50
20
40
Enter the element to be deleted
35
Element 35 is not found in the vector
Run 2
Enter how many elements
4
Enter the elements
23
10
55
81
ty
Input array elements are
23
or
10
55
81
ab
Enter the element to be deleted
55
kr
The resultant vector is
23
10
81
ha
C
--------------------------------------------------------*/
62. /* Write a C programme (1-D Array) store some elements in it.Accept key
ar
& split from that point. Add the first half to the end of second half*/
um
#include <stdio.h>
void main ()
{
K
int number[30];
int i,n,a,j;
k
scanf ("%d",&n);
bh
printf ("Enter the position of the element to split the array \n");
scanf ("%d",&a);
/*-------------------------------------------------
Output
Enter the value of n
5
ty
enter the numbers
30
or
10
40
50
ab
60
Enter the position of the element to split the array
kr
2
The resultant array is
40
50
ha
60
C
30
10
----------------------------------------------------------*/
ar
#include <stdio.h>
K
void main ()
{
k
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)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
ty
} /* End of main() */
or
/*-----------------------------------------------------
Output
ab
Enter the order ofthe matrix
33
kr
Enter the coefficients if matrix
123
456
789
ha
The given matrix is
C
123
456
789
ar
-------------------------------------------------------*/
K
64. /* Write a C program to accept a matrix of order M x N and store its elements *
* and interchange the main diagonal elements of the matrix *
k
#include <stdio.h>
bh
void main ()
{
A
if (m ==n )
{
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]);
}
}
ty
}
or
for (i=0;i<m;++i)
{
a = ma[i][i];
ab
ma[i][i] = ma[i][m-i-1];
ma[i][m-i-1] = a;
kr
}
printf ("\n");
}
}
else
K
} /* end of main() */
ee
/*----------------------------------------------------
Output
bh
123
456
789
The given matrix is
123
456
789
THe matrix after changing the
main diagonal & secondary diagonal
321
456
987
----------------------------------------------------------*/
#include <stdio.h>
void main()
{
float ht;
ty
scanf("%f",&ht);
or
if(ht < 150.0)
printf("Dwarf\n");
else if((ht>=150.0) && (ht<=165.0))
ab
printf(" Average Height\n");
else if((ht>=165.0) && (ht <= 195.0))
kr
printf("Taller\n");
else
printf("Abnormal height\n");
ha
} /* End of main() */
C
/*-----------------------------------
Output
ar
Dwarf
----------------------------------------*/
#include <stdio.h>
ee
void main()
{
bh
int x,y;
scanf("%d %d",&x,&y);
/*---------------------------------------------
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
-4
ty
-7
point (-7, -4) lies in the Third quandrant
or
---------------------------------------------*/
ab
67. /* Write a C program to accept a figure code and find the ares of different *
kr
* geometrical figures such as circle, square, rectangle etc using switch */
#include <stdio.h>
ha
void main()
C
{
int fig_code;
float side,base,length,bredth,height,area,radius;
ar
printf("-------------------------\n");
um
printf("-------------------------\n");
k
switch(fig_code)
bh
{
case 1: printf("Enter the radius\n");
scanf("%f",&radius);
A
area=3.142*radius*radius;
printf("Area of a circle=%f\n", area);
break;
ty
} /* End of switch */
or
} /* End of main() */
/*----------------------------------------------------
Output
ab
Run 1
-------------------------
kr
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
ha
-------------------------
C
Enter the Figure code
2
Enter the bredth and length
ar
2
6
um
Area of a Reactangle=12.000000
Run 2
-------------------------
K
1 --> Circle
2 --> Rectangle
k
3 --> Triangle
4 --> Square
ee
-------------------------
Enter the Figure code
bh
3
Enter the base and height
5
A
7
Area of a Triangle=17.500000
------------------------------------------------------*/
68. /* Write a C Program to accept a grade and declare the equivalent descrption *
* if code is S, then print SUPER
*
* if code is A, then print VERY GOOD *
* if code is B, then print FAIR *
* if code is Y, then print ABSENT *
* if code is F, then print FAIL */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void main()
{
char remark[15];
char grade;
ty
switch(grade)
or
{
case 'S': strcpy(remark," SUPER");
break;
ab
case 'A': strcpy(remark," VERY GOOD");
kr
break;
} /* End of switch */
k
printf("RESULT : %s\n",remark);
ee
} /* End of main() */
bh
/*-------------------------------------------
Output
RUN 1
A
RUN 2
Enter the grade
y
RESULT : ABSENT
-----------------------------------------------*/
ty
{
fact = fact * i;
or
}
} /* End of else */
ab
printf("Factorial of %d =%5d\n", num,fact);
kr
} /* End of main() */
/*-------------------------------------------
Output
ha
Enter the number
C
5
Factorial of 5 = 120
ar
---------------------------------------------*/
um
#include <stdio.h>
K
void main()
{
k
char string[80];
int count,nc=0,sum=0;
ee
scanf("%s",string);
for(count=0;string[count]!='\0'; count++)
A
{
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
goodmorning25
NO. of Digits in the string=2
Sum of all digits=7
--------------------------------------------------------*/
70. /* 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 */
ty
#include <stdio.h>
or
void main ()
{
ab
static int m1[10][10];
int i,j,m,n,sum=0;
kr
printf ("Enter the order of the matrix\n");
scanf ("%d %d", &m,&n);
printf ("Enter the co-efficients of the matrix\n");
ha
C
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
ar
{
scanf ("%d",&m1[i][j]);
um
}
}
for (i=0;i<m;++i)
{
K
for (j=0;j<n;++j)
{
k
sum=0;
A
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;
}
} /*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
ty
----------------------------------------------------*/
or
71. /* Write a C program to find accept a matric of order M x N and find *
* the sum of the main diagonal and off diagonal elements */
ab
#include <stdio.h>
kr
void main ()
{
static int ma[10][10];
ha
int i,j,m,n,a=0,sum=0;
C
printf ("Enetr the order of the matix \n");
scanf ("%d %d",&m,&n);
ar
if ( m == n )
um
{
printf ("Enter the co-efficients of the matrix\n");
for (i=0;i<m;++i)
K
{
for (j=0;j<n;++j)
k
{
scanf ("%d",&ma[i][j]);
ee
}
}
bh
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}
for (i=0;i<m;++i)
{
sum = sum + ma[i][i];
a = a + ma[i][m-i-1];
}
/*--------------------------------------------------------
Output
Enetr the order of the matix
33
ty
Enter the co-efficients of the matrix
123
or
456
789
The given matrix is
ab
123
456
kr
789
--------------------------------------------------------*/
72. /* 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 *
K
#include <stdio.h>
ee
#include <math.h>
bh
void main ()
{
static int ma[10][10];
A
int i,j,m,n,sum=0,sum1=0,a=0,normal;
} /*End of main() */
/*---------------------------------------------------
ty
Output
Enter the order of the matrix
or
33
Enter the ncoefficients of the matrix
123
ab
456
789
kr
The normal of the given matrix is = 16
Trace of the matrix is = 15 ha
Run 2
Enter the order of the matrix
C
22
Enter the ncoefficients of the matrix
24
ar
68
The normal of the given matrix is = 10
um
-----------------------------------------------------*/
K
73. /* Write a C program to accept a matric of order MxN and find its transpose */
k
#include <stdio.h>
void main ()
ee
{
static int ma[10][10];
bh
int i,j,m,n;
ty
printf (" %d",ma[i][j]);
}
or
printf ("\n");
}
} /* End of main() */
ab
/*------------------------------------------
kr
Output
Enter the order of the matrix
22
Enter the coefiicients of the matrix
ha
3 -1
C
60
The given matrix is
3 -1
ar
60
Transpose of matrix is
um
36
-1 0
-------------------------------------------*/
K
#include <stdio.h>
bh
void main ()
{
A
ty
printf ("There are %d number of zeros",counter);
or
} /* EN dof main() */
/*----------------------------------------------
ab
Output
Enter the order of the matix
kr
22
Enter the co-efficients of the matix
12
34
ha
The given matrix is not a sparse matrix
C
There are 0 number of zeros
Run 2
ar
-----------------------------------------------*/
ee
75. /* Write a C program to accept a matrix of order MxN and sort all rows *
bh
#include <stdio.h>
A
void main ()
{
static int ma[10][10],mb[10][10];
int i,j,k,a,m,n;
ty
printf ("\n");
}
or
printf ("After arranging rows in ascending order\n");
for (i=0;i<m;++i)
ab
{
for (j=0;j<n;++j)
kr
{
for (k=(j+1);k<n;++k)
{
if (ma[i][j] > ma[i][k])
ha
{
C
a = ma[i][j];
ma[i][j] = ma[i][k];
ma[i][k] = a;
ar
}
}
um
}
} /* End of outer for loop*/
for (i=0;i<m;++i)
K
{
for (j=0;j<n;++j)
k
{
printf (" %d",ma[i][j]);
ee
}
printf ("\n");
bh
for (j=0;j<n;++j)
{
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;
}
}
}
} /* End of outer for loop*/
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("\n");
}
ty
} /*End of main() */
or
/*-------------------------------------------------
Enter the order of the matrix
22
ab
Enter co-efficients of the matrix
31
kr
52
The given matrix is
31
52
ha
After arranging rows in ascending order
C
13
25
After arranging the columns in descending order
ar
52
31
um
-----------------------------------------------------*/
#include<stdio.h>
k
#include<conio.h>
#include<string.h>
ee
struct person
bh
{
char name[10];
int rno;
A
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
clrscr();
fflush(stdin);
printf("Enter the number of students in the list\n");
scanf("%d",&no);
ty
}
or
printf("\n*****************************\n");
printf (" Names before sorting \n");
/* Print the list of names before sorting */
ab
for(i=0;i<no;i++)
{
kr
printf("%-10s\t%3d\n",temp[i].name,temp[i].rno);
} ha
sort(no); /* Function call */
C
printf("\n*****************************\n");
printf (" Names after sorting \n");
printf("\n*****************************\n");
ar
{
printf("%-10s\t%3d\n",stud[i].name,stud[i].rno);
}
K
printf("\n*****************************\n");
} /* End of main() */
k
void sort(int N)
{
bh
int i,j;
NAME temp;
A
/*--------------------------------------------------------
Enter the number of students in the list
5
ty
Enter the roll number of 3 : 331
or
Enter the name of person 4 : Anand
Enter the roll number of 4 : 411
ab
Enter the name of person 5 : Nirmala
Enter the roll number of 5 : 311
kr
*****************************
Names before sorting
Rajashree 123
ha
John 222
C
Priya 331
Anand 411
Nirmala 311
ar
*****************************
um
*****************************
Anand 411
K
John 222
Nirmala 311
k
Priya 331
Rajashree 123
ee
*****************************
bh
----------------------------------------------------*/
77. /* Write a C Program to convert the lower case letters to upper case *
A
* and vice-versa */
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
}
count = i;
ty
putchar(ch);
}
or
} /*End of main()
ab
/*-----------------------------------------
Output
kr
Enter a sentence
gOOD mORNING ha
Input sentence is : gOOD mORNING
C
Resultant sentence is :Good Morning
------------------------------------------*/
ar
78. /* Write a C program to find the sum of two one-dimensional arrays using *
* Dynamic Memory Allocation */
um
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
K
void main()
k
{
int i,n;
ee
int *a,*b,*c;
bh
a = (int *) malloc(n*sizeof(int));
b = (int *) malloc(n*sizeof(int));
c =( int *) malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
*(c+i) = *(a+i) + *(b+i);
}
ty
}
or
} /* End of main() */
/*---------------------------------------
ab
Output
How many Elements in each array...
kr
4
Enter Elements of First List
1
2
ha
3
C
4
Enter Elements of Second List
6
ar
7
8
um
9
Resultant List is
7
9
K
11
13
k
----------------------------------------*/
ee
#include <stdio.h>
A
void main()
{
static int array[5]={ 200,400,600,800,1000 };
int sum;
sum = addnum(array);
ty
/*-----------------------------------
Output
or
Sum of all array elements = 3000
------------------------------------*/
ab
80. /* Write a C program to accept an array of 10 elements and swap 3rd *
* element with 4th element using pointers. And display the results */
kr
#include <stdio.h> ha
void main()
{
C
float x[10];
int i,n;
ar
scanf("%d", &n);
K
for(i=0;i<n;i++)
ee
{
scanf("%f",x+i);
bh
printf("\nResultant Array...\n");
for(i=0;i<n;i++)
{
printf("X[%d] = %f\n",i,x[i]);
}
} /* End of main() */
/* Function to swap the 3rd element with the 4th element in the array */
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
} /* End of Function */
/*-------------------------------------------
Output
How many Elements...
10
Enter Elements one by one
10
ty
20
30
or
40
50
60
ab
70
80
kr
90
100 ha
Resultant Array...
X[0] = 10.000000
C
X[1] = 20.000000
X[2] = 40.000000
X[3] = 30.000000
ar
X[4] = 50.000000
X[5] = 60.000000
um
X[6] = 70.000000
X[7] = 80.000000
X[8] = 90.000000
X[9] = 100.000000
K
----------------------------------------------------*/
k
#include <stdio.h>
bh
#include <conio.h>
void main()
A
{
union number
{
int n1;
float n2;
};
union number x;
clrscr() ;
} /* End of main() */
/*------------------------------------
Output
Enter the value of n1: 2
Value of n1 =2
ty
Enter the value of n2: 3
Value of n2 = 0
or
------------------------------------*/
ab
82. /* Write a C program to find the size of a union*/
kr
#include <stdio.h> ha
void main()
{
C
union sample
{
int m;
ar
float n;
char ch;
um
};
union sample u;
K
/*initialization */
ee
u.m = 25;
printf("%d %f %c\n", u.m, u.n,u.ch);
bh
u.n = 0.2;
printf("%d %f %c\n", u.m, u.n,u.ch);
A
u.ch = 'p';
printf("%d %f %c\n", u.m, u.n,u.ch);
} /*End of main() */
/*-----------------------------------------
Output
The size of union =4
25 12163373596672.000000
-13107 0.200000
-13200 0.199999 p
------------------------------------------*/
83. /* Write a C program to create a file called emp.rec and store information *
* about a person, in terms of his name, age and salary. */
#include <stdio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
ty
fptr = fopen ("emp.rec", "w"); /*open for writing*/
or
if (fptr == NULL)
{
printf("File does not exists\n");
ab
return;
}
kr
printf("Enter the name\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
ha
C
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
ar
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
K
}
k
/*---------------------------------------------------------------------------
Output
ee
25000
-------------------------------------
Please note that you have to open the file called emp.rec in the directory
Name = Prabhu
Age = 25
Salary = 25000.00
-----------------------------------------------------------------------------*/
84. /*Write a C program to illustrate as to how the data stored on the disk is read */
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr;
char filename[15];
char ch;
ty
if (fptr == NULL)
{
or
printf("Cannot open file\n");
exit(0);
}
ab
ch = fgetc(fptr);
kr
while (ch != EOF)
{
printf ("%c", ch);
ha
ch = fgetc(fptr);
C
}
fclose(fptr);
ar
} /* End of main () */
/*----------------------------------------------
um
Output
Enter the filename to be opened
emp.rec
Name = Prabhu
K
Age = 25
Salary = 25000.00
k
------------------------------------------------*/
ee
#include <stdio.h>
A
main()
{
float xval=8.5;
char usrans='Q';
printf("\naddress of xval is %p", &xval);
printf("\nvalue of xval is %f", xval);
printf("\naddress of usrans is %p", &usrans);
printf("\nvalue of usrans is %c\n", usrans);
system("PAUSE");
return 1;
}
2. Checks a given number if prime
#include <stdio.h>
int main()
{
int r, i, flag=0, n;
printf("Enter any number to check if prime:\n");
scanf("%d",&n);
for(i=2; i<n; i++)
{
ty
r=n%i;
if(r==0)
or
{
flag=1;
ab
}/*end if*/
} /*end of for*/
if(flag==0)
kr
{ printf("tne number is prime\n"); }
else
ha
{ printf("the number is not prime\n"); }
return 0;
C
}
#include <stdio.h>
um
int main()
{
float f, c;
K
f=32 + (9*c)/5;
printf("The temperature in f=%f/n", f);
ee
return 0;
}
bh
#include <stdio.h>
int main()
{
int num,x,y;
printf("Enter a number :\n");
scanf("%d",&num);
x = num % 2;
y = num % 3;
if((x==0) && (y==0))
printf("%d is divisible by both 2 and 3\n",num);
else
printf("%d is not divisible by both 2 and 3\n",num);
return(0);
}
#include <stdio.h>
ty
int main()
{
or
int sum=0, r, x,y;
printf("Enter a number: \n");
ab
scanf("%d", &x);
y=x;
while (x>0)
kr
{
r=x%10;
sum=sum+r;
ha
x=x/10;
C
}
printf("The sum of the degits of %d is: %d\n", y,sum);
return 0;
ar
}
um
#include <stdio.h>
k
int main()
{
ee
int sum=0,x=2,num,i;
printf("Enter a number to sum upto :\n");
bh
scanf("%d",&num);
A
ty
}
or
printf("The sum is : %f\n",sum);
return(0);
ab
}
kr
given in years, weeks and days. For example 375 days is equal to 1 year
1 week and 3 days (ignore leap year)
ha
#include <stdio.h>
C
#define DAYSINWEEK 7
void main()
ar
{
int ndays, year, week, days;
um
year = ndays/365;
k
#include <stdio.h>
int main()
{
int num1, num2, r, t, gcd;
printf("Enter the first number to get the GCD :\n");
scanf("%d",&num1);
printf("Enter the second number to get the GCD :\n");
scanf("%d",&num2);
printf("The GCD of %d and %d is : \n",num1,num2);
if (num2>num1)
{
t=num1;
num1=num2;
num2=t;
}
while(num1%num2!=0)
{
r=num1%num2;
num1=num2;
ty
num2=r;
}
or
gcd=num2;
printf("%d\n",gcd);
ab
return(1);
}
kr
93. Write a c program to swap the contents of two numbers
ha
using bitwise XOR operation. Don't use either the
temporary variable or arithmetic operators
C
#include <stdio.h>
void main()
ar
{
long i,k;
um
i = i^k;
k = i^k;
bh
i = i^k;
A
#include <stdio.h>
int main()
{
int i,num,x=1;
long fact=1;
printf("Enter a number :\n");
scanf("%d",&num);
ty
95. Prints n numbers of fibonacci numbers
or
#include <stdio.h>
ab
int main()
{
int num,i;
kr
long first=0, second=1, new;
ha
printf("How many fibonacci number to print :\n");
scanf("%d",&num);
C
printf("Fibonacci of %d numbers :\n",num);
printf("Fibonacci [ 0] is : 0\n");
printf("Fibonacci [ 1] is : 1\n");
ar
{
new = first + second;
printf("Fibonacci [%2d] is : %ld\n",i+1,new);
K
first = second;
second = new;
k
}
return(1);
ee
}
bh
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2, m, n;
printf("Enter two numbers to get their simple ratio :\n");
scanf("%d %d",&num1,&num2);
printf("The two numbers are - %d : %d\n",num1,num2);
m=num1;
n=num2;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
num1 /=m;
num2 /=m;
printf("The simple ratio is - %d : %d\n",num1,num2);
return 0;
ty
}
or
97. Converts a decimal number to it's binary equivalent.
ab
#include <stdio.h>
int main()
kr
{
int num,sum=0,bs=1,r,n;
printf("Enter a number :\n");
ha
scanf("%d",&num);
C
n=num;
while(num>1)
{
ar
r = num%2;
num /=2;
um
sum += r * bs;
bs *=10;
}
K
return(0);
}
bh
98. Write a C program to convert the given binary number into decimal
A
#include <stdio.h>
void main()
{
int num, bnum, dec = 0, base = 1, rem ;
bnum = num;
while( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}
ty
}
or
99. Area of the circle without using macro definition
ab
#include <stdio.h>
int main()
{
kr
float a,r,pi=3.142;
printf("enter the value of radius r\n");
scanf("%f",&r);
ha
a=pi*r*r;
C
printf("area of the circle a=%f\n",a);
return 1;
}
ar
#include<stdio.h>
# define pi 3.142
K
int main()
k
{
float a,r;
ee
a=pi*r*r;
printf("area of the circle a=%f\n",a);
A
return 1;
}
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
ty
}
or
sentence[i]='\0';
ab
count = i; /*shows the number of chars accepted in a sentence*/
kr
printf("\nCase changed sentence is: ");
for(i=0; i < count; i++)
ha
{
C
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
ar
}
um
#include <stdio.h>
ee
#include <conio.h>
bh
void main()
{
A
char sentence[80];
int i, vowels=0, consonants=0, special = 0;
clrscr();
printf("Enter a sentence\n");
gets(sentence);
ty
special = special + 1;
}
or
}
ab
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
kr
}
ha
C
103. Takes input as : INDIA
outputs as :
ar
I
IN
um
IND
INDI
INDIA
K
# include <stdio.h>
k
# include <string.h>
ee
int main()
{
bh
char ip[20];
int i,j,l;
A
}
printf("\n");
}
return(1);
}
#include <stdio.h>
int main()
{
int a[10], i, sum=0;
ty
printf("Enter 10 elements:\n");
for(i=0; i<10; i++)
or
{
scanf("%d", &a[i]);
ab
sum=sum+a[i];
}
printf("sum of the array=%d\n", sum);
kr
return 0;
}
ha
105. Prints the reverse of a given string
C
#include <stdio.h>
#include <string.h>
ar
int main()
um
{
char name[15], rev[15];
int n,i=0, l;
K
printf("Enter a word:\n");
k
gets(name);
n=strlen(name);
ee
{
printf("%c",name[i]);
A
}
printf("\n");
return(1);
}
#include <stdio.h>
#include <string.h>
int main()
{
char fname[15], lname[15], name[30];
int n, x;
printf("Enter ur first name :\n");
gets(fname);
printf("Enter ur last name :\n");
gets(lname);
strcpy(name,fname);
printf("Your name : %s\n",name);
strcat(name,lname);
printf("Your full name : %s\n",name);
ty
n=strlen(name);
printf("The length of ur name : %d\n",n);
or
x = strncmp(fname,lname,1);
if(x==0)
ab
printf("The first character of ur first and last name is same.\n");
else
printf("The first character of ur first and last name is not same.\n");
kr
return(1);
}
ha
C
107. Takes the values of an array and prints the max and
next max values
ar
#include <stdio.h>
um
int main()
{
int nums[10];
K
scanf("%d",&n);
for(i=0; i<n; i++)
ee
{
printf("number %d :\n",x);
bh
x++;
scanf("%d",&nums[i]);
A
x=0;
max1=nums[0];
for(i=1; i<n; i++)
{
if(max1<nums[i])
{
max2=max1;
max1=nums[i];
}
}
printf("The maximum values are: %d %d\n",max1, max2);
return(1);
}
ty
108. To find Max and Min in an array
or
#include<stdio.h>
main()
ab
{
float a[20],max,min;
int i;
kr
for(i=0;i<20;i++)
{
printf("Enter value:\n");
ha
scanf("%f",&a[i]);
C
}
max=min=a[0];
for(i=1;i<20;i++)
ar
{
if(max<a[i])
um
max=a[i];
if(min>a[i])
min=a[i];
K
}
printf("In the given array max=%f & min=%f\n",max,min);
k
}
ee
#include <stdio.h>
#include <string.h>
A
#include <ctype.h>
int main()
{
char name[25];
int n, i=0, x;
printf("Enter your name : \n");
gets(name);
n=strlen(name);
if(name[0]!='\0')
x=toupper(name[0]);
printf("%c",x);
for(i=1; i<n; i++)
{
if(name[i]==' ')
{
x=toupper(name[i+1]);
printf(". %c",x);
}
}
printf("\n");
ty
return(0);
}
or
ab
110. Checks a string if polidrome and and print the string in reverse.
#include <stdio.h>
kr
#include <string.h>
int main()
{
ha
char name[15];
C
int n,i=0, flag;
printf("Enter a word:\n");
gets(name);
ar
n=strlen(name);
while(i<=n/2)
um
{
if(name[i]==name[n-1])
{
K
flag=1;
i++;
k
n--;
}
ee
else
{
bh
flag=0;
break;
A
}
}
if (flag==1)
printf("This word is a polidrome\n");
else printf("This word is not a polidrome\n");
return(1);
}
111. Write a C Program to accept a grade and declare the equivalent
descrption *
if code is S, then print SUPER
if code is A, then print VERY GOOD
if code is B, then print FAIR
if code is Y, then print ABSENT
if code is F, then print FAILS
ty
*/
or
#include <stdio.h>
#include <ctype.h>
ab
#include <string.h>
void main()
kr
{
char remark[15];
char grade;
ha
C
printf("Enter the grade\n");
scanf("%c",&grade);
ar
switch(grade)
{
case 'S': strcpy(remark," SUPER");
K
break;
k
break;
A
} /* End of switch */
printf("RESULT : %s\n",remark);
#include<stdio.h>
void main()
{
ty
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
or
puts("Enter a string:");
gets(str);
ab
puts("Enter search substring:");
gets(search);
kr
while (str[count1]!='\0')
count1++;
ha
C
while (search[count2]!='\0')
count2++;
ar
for(i=0;i<=count1-count2;i++)
{
um
for(j=i;j<i+count2;j++)
{
flag=1;
K
if (str[j]!=search[j-i])
{
k
flag=0;
break;
ee
}
}
bh
if (flag==1) break;
}
A
if (flag==1)
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}
#include <stdio.h>
int sumdigits(int x);
int main()
{
int sum, x;
printf("Enter a number: \n");
scanf("%d", &x);
sum=sumdigits(x);
printf("The sum of the degits of %d is: %d\n",x,sum);
return 0;
}
ty
int sumdigits(int x)
or
{
int r, sum=0;
ab
while (x>0)
{
r=x%10;
kr
sum=sum+r;
x=x/10;
} /* end of while*/
ha
return sum;
C
} /* end of fun */
#include <stdio.h>
um
int main()
K
{
int x, y;
k
#include <stdio.h>
#include <math.h>
ty
printf("Enter the values of X and N\n");
scanf("%ld %ld",&x,&n);
or
xpown = power (x,n);
ab
printf("X to the power N = %ld\n");
}
kr
/*Recursive function to computer the X to power N*/
ha
long int power(int x, int n)
C
{
if (n==1)
return(x);
ar
else if ( n%2 == 0)
return (pow(power(x,n/2),2)); /*if n is even*/
um
else
return (x*power(x, n-1)); /* if n is odd*/
}
K
#include <stdio.h>
ee
int main()
{
A
int x, y, g;
printf("Enter two numbers:\n");
scanf("%d %d", &x, &y);
g=gcd(x, y);
printf("Ultimate value of GCD= %d\n", g);
return 0;
}
ty
{
max=min;
or
min=r;
r=max%min;
ab
} /* end of while */
return min;
} /* end of function */
kr
117. Function to concatenate two string
ha
C
#include <stdio.h>
int main()
um
{
char firststr[15], secondstr[15];
int i=0;
K
str_concat(firststr, secondstr);
printf("the new string is:\n%s\n", firststr);
bh
return 0;
}
A
} /* end of function */
#include <stdio.h>
void str_copy(char *source, char *destination);
ty
int leg_str(char *source);
or
int main()
{
ab
char firststr[15], secondstr[15];
int i=0;
printf("enter one string:\n");
kr
scanf("%s", firststr);
str_copy(firststr, secondstr);
ha
printf("the copied string is:\n%s\n", secondstr);
i=leg_str(firststr);
C
printf("the length of the string is :%d\n", i);
return 0;
}
ar
{
int i=0;
while(source[i]!='\0')
K
{
destination[i]= source[i];
k
i++;
} /* end of while loop */
ee
destination[i]='\0';
bh
} /* end of function */
A
#include <stdio.h>
int strlength(char a[]);
int main()
{
char a[10];
int l;
printf("enter a string:\n");
scanf("%s", a);
l=strlength(a);
printf("length of the string=%d", l);
ty
system("PAUSE");
return 0;
or
}
ab
int strlength(char a[])
{
int l=0;
kr
while (a[l]!='\0')
{
l++;
ha
} /* end of while loop */
C
return l;
} /* end of function */
ar
#include <stdio.h>
long fact(long x);
int main()
K
{
long factorial;
k
long num;
printf("Enter a number :\n");
ee
scanf("%ld",&num);
factorial = fact(num);
bh
long fact(long x)
{
if(x==0)
return (1);
else
return(x * fact(x-1));
}
#include <stdio.h>
void main()
{
float x[10];
int i,n;
void swap34(float *ptr1, float *ptr2 ); /* Function Declaration */
ty
scanf("%d", &n);
or
printf("Enter Elements one by one\n");
for(i=0;i<n;i++)
ab
{
scanf("%f",x+i);
}
kr
swap34(x+2, x+3); /* Function call:Interchanging 3rd element by 4th */
ha
printf("\nResultant Array...\n");
C
for(i=0;i<n;i++)
{
printf("X[%d] = %f\n",i,x[i]);
ar
}
}
um
#include <stdio.h>
k
void main()
ee
{
static int array[5]={ 200,400,600,800,1000 };
bh
int sum;
int addnum(int *ptr); /* function prototype */
A
sum = addnum(array);
} /* End of main() */
122. Dynamic allocation of array and getting the average of that array.
#include<stdio.h>
#include<stdlib.h>
ty
int main()
{
or
float *a,sum=0.0,avg;
int i,l;
ab
printf("How many real numbers u want to put in to array:\n");
scanf("%d",&l);
a=(float*)malloc(l*sizeof(float));
kr
for(i=0;i<l;i++)
{
ha
printf("Enter no %d value:\n",i+1);
scanf("%f",&a[i]);
C
sum+=a[i];
avg=sum/(float)l;
}
ar
#include <stdio.h>
#include <conio.h>
bh
#include <string.h>
A
void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;
clrscr();
ty
strcpy(name[i],name[j]);
strcpy(name[j],temp);
or
}
}
ab
}
printf("\n----------------------------------------\n");
kr
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
for(i=0; i< N ; i++)
ha
{
C
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");
ar
}
um
#include<stdio.h>
main()
k
{
int i,j,rsum=0,csum=0,d1sum=0,d2sum=0,vals[4][4];
ee
for(i=0;i<4;i++)
{
bh
for(j=0;j<4;j++)
{
A
printf("\n vals[%d][%d]:",i,j);
scanf("%d",&vals[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("\t vals[%d][%d]%d",i,j,vals[i][j]);
printf("\n");
}
for(i=0;i<4;i++)
{
rsum=0;
csum=0;
d1sum=0;
d2sum=0;
for(j=0;j<4;j++)
{
rsum+=vals[i][j];
if(i==j)
d1sum+=vals[i][j];
if((i+j)==3)
d2sum+=vals[i][j];
ty
csum+=vals[j][i];
}
or
printf("row sum for row %d is %d",i+1,rsum);
printf("\n");
ab
printf("col sum for row %d is %d",i+1,csum);
printf("\n");
}
kr
}
ha
125. Takes the elements from the user and puts into a matrix
C
#include <stdio.h>
#define m 3
#define n 3
ar
int main()
um
{
int a[m][n];
int i,j;
K
{
for(j=0; j<n; j++)
ee
{
scanf("%d", &a[i][j]);
bh
}
}
A
system("PAUSE");
return 0;
}
126. 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;
ty
printf("Enter the elements of matrix A\n");
or
for(i=0; i<R; i++)
{
ab
for(j=0; j<C; j++)
{
scanf("%d",&A[i][j]);
kr
}
}
printf("MATRIX A is\n");
ha
for(i=0; i<R; i++)
C
{
for(j=0; j<C; j++)
{
ar
printf("%3d",A[i][j]);
}
um
printf("\n");
}
K
flag = 0;
break;
}
}
}
if(flag == 1 )
printf("It is identity matrix\n");
else
printf("It is not a identity matrix\n");
}
#include <stdio.h>
ty
static char *a1[]={"zero", "One", "Two", "Three", "Four", "Five",
or
"Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Furteen", "Fifteen", "Sixteen",
ab
"Seventeen", "Eighteen", "Ninteen"};
kr
"Sixty", "Seventy", "Eighty", "Ninty"};
int i;
ha
int main()
C
{
double num, num1;
long rs;
ar
rs=num;
printf("Rupees ");
if(rs<20) f1(rs);
K
num1=num-rs;
if(num1!=0)
A
{
printf("and ");
num1 *=100;
rs=num1;
if (rs>19) f2(rs);
else f1(rs);
printf("Paise ");
}
printf("Only....\n");
return 0;
}
void f1(int x)
{
printf("%s ", a1[x]);
}
void f2(int x)
{
i=x/10;
printf("%s ",a2[i]);
i=x%10;
ty
if(i!=0) printf("%s ", a1[i]);
}
or
void f3(int x)
ab
{
i=x/100;
printf("%s Hundred ", a1[i]);
kr
i=x%100;
if((i!=0)&&(i<20)) f1(i);
else if((i!=0)&&(i>19)) f2(i);
ha
}
C
void f4(int x)
{
ar
i=x/1000;
if(i<20) f1(i);
um
else f2(i);
printf("Thousand ");
i=x%1000;
K
if((i!=0)&&(i<20)) f1(i);
else if ((i!=0)&&(i<100)) f2(i);
k
void f5(int x)
bh
{
i=x/100000;
A
if(i>19) f2(i);
else f1(i);
printf("Lakh ");
i=x%100000;
if((i!=0)&&(i<20)) f1(i);
else if((i!=0)&&(i<100)) f2(i);
else if((i!=0)&&(i<1000)) f3(i);
else if((i!=0)&&(i<100000)) f4(i);
}
void f6(int x)
{
i=x/10000000;
if(i>19) f2(i);
else f1(i);
printf("Crore ");
i=x%10000000;
if((i!=0)&&(i<20)) f1(i);
else if((i!=0)&&(i<100)) f2(i);
else if((i!=0)&&(i<1000)) f3(i);
else if((i!=0)&&(i<100000)) f4(i);
else if((i!=0)&&(i<10000000)) f5(i);
}
ty
128. Write a C program to read N integers and store them
or
in an array A, and so find the sum of all these
elements using pointer. Output the given array and
ab
and the computed sum with suitable heading
#include <stdio.h>
kr
#include <malloc.h>
void main()
ha
{
C
int i,n,sum=0;
int *a;
ar
{
scanf("%d",a+i);
ee
}
bh
{
sum = sum + *(a+i);
}
} /* End of main() */
129. Stores the employee dbase using structure, from the user and
prints it
#include <stdio.h>
#include <string.h>
typedef struct
{
char fname[20];
char lname[15];
long emp_code;
} EMP;
int main()
{
EMP nist[20];
ty
int i,n;
printf("Enter the number of employees :\n");
or
scanf("%d",&n);
for(i=0; i<n; i++)
ab
{
printf("Enter the first name :\n");
scanf("%s",nist[i].fname);
kr
printf("Enter the last name :\n");
scanf("%s",nist[i].lname);
ha
printf("Enter the employee code :\n");
scanf("%ld",&nist[i].emp_code);
C
}
{
printf("The name of the employee : %s %s and the employee code
um
:%ld\n", nist[i].fname,nist[i].lname,nist[i].emp_code);
}
K
return(0);
}
k
130. Write a C program to illustrate as to how the data stored in the file is read
ee
#include <stdio.h>
bh
#include <stdlib.h>
A
void main()
{
FILE *fptr;
char filename[15];
char ch;
ch = fgetc(fptr);
ty
}
or
fclose(fptr);
}
ab
131. Write a C program to create a file called emp.rec and store information
about a person, in terms of his name, age and salary.
kr
#include <stdio.h>
ha
void main()
C
{
FILE *fptr;
char name[20];
ar
int age;
float salary;
um
if (fptr == NULL)
{
k
}
printf("Enter the name\n");
bh
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
A
fclose(fptr);
}
A
bh
ee
k
K
um
ar
C
ha
kr
ab
or
ty