0% found this document useful (0 votes)
339 views

C Programs

This is wonderful collection of C Programmes and very useful for candidates preparing for Campus Interviews. Moreover this note can be very beneficial for the students who are in the process of learning basic C Programme.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
339 views

C Programs

This is wonderful collection of C Programmes and very useful for candidates preparing for Campus Interviews. Moreover this note can be very beneficial for the students who are in the process of learning basic C Programme.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 131

1.

/* Write a C program to find the area of a triangle, given three sides*/

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

void main()
{
int s, a, b, c, area;
clrscr();

printf("Enter the values of a,b and c\n");


scanf ("%d %d %d", &a, &b, &c);

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
------------------------------*/

2. /* Write a C program to find the area of a circl, given the adius*/


K

#include <stdio.h>
k

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

#define PI 3.142
bh

void main()
{
float radius, area;
A

clrscr();

printf("Enter the radius of a circle\n");


scanf ("%f", &radius);

area = PI * pow (radius,2);

printf ("Area of a circle = %5.2f\n", 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

------------------------------*/

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();

printf("Enter the values of p,r and t\n");


ar

scanf ("%f %f %d", &p, &r, &t);


um

si = (p * r * t)/ 100.0;

printf ("Amount = Rs. %5.2f\n", p);


printf ("Rate = Rs. %5.2f%\n", r);
K

printf ("Time = %d years\n", t);


printf ("Simple interest = %5.2f\n", si);
k

}
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

------------------------------*/

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;

clrscr();

printf("Enter an integer :");


scanf ("%d", &ival);

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

Enter an integer :24


24, is an even integer
um

---------------------------------*/

5. /* Write a C program to check whether a given integer *


K

* number is positive or negative*/


k

#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
------------------------------*/

6. /* Write a C program to find the biggest of three numbers*/

#include <stdio.h>

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

or
void main()
{

ab
int a, b, c;

kr
clrscr();

printf("Enter the values of a,b and c\n");


scanf ("%d %d %d", &a, &b, &c);
ha
C
printf ("a = %d\tb = %d\tc = %d\n", a,b,c);

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

printf ("B is the greatest among three\n");


}
else
A

printf ("C is the greatest among three\n");

/*-----------------------------
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

printf("Enter the values of A, B and C\n");


scanf("%f %f %f", &A,&B,&C);
K

/* If A = 0, it is not a quadratic equation */


k

if( A==0 || B==0 || C==0)


{
ee

printf("Error: Roots cannot be determined\n");


exit(1);
bh

}
else
{
A

disc = B*B - 4.0*A*C;


if(disc < 0)
{
printf("Imaginary Roots\n");
realp = -B/(2.0*A) ;
imagp = sqrt(abs(disc))/(2.0*A);
printf("Root1 = %f +i %f\n",realp, imagp);
printf("Root2 = %f -i %f\n",realp, imagp);
}
else if(disc == 0)
{
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 )
{
printf("Roots are real and distinct\n");
root1 =(-B+sqrt(disc))/(2.0*A);
root2 =(-B-sqrt(disc))/(2.0*A);
printf("Root1 = %f \n",root1);
printf("Root2 = %f \n",root2);
}

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

Enter the values of A, B and C


352
k

Roots are real and distinct


Root1 = -0.666667
ee

Root2

= -1.000000
bh
A

---------------------------------*/

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 */

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

void main()
{
char oper; /* oper is an operator to be selected */
float n1, n2, result;

clrscr();

printf ("Simulation of a Simple Calculator\n\n");

printf("Enter two numbers\n");


scanf ("%f %f", &n1, &n2);

fflush (stdin);

printf("Enter the operator [+,-,*,/]\n");

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

printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);

}
/*-----------------------------
K

Output
Simulation of Simple Calculator
k

Enter two numbers


ee

35
Enter the operator [+,-,*,/]
bh

3.00 + 5.00= 8.00


A

RUN2
Simulation of Simple Calculator

Enter two numbers


12.75
8.45
Enter the operator [+,-,*,/]
-

12.75 - 8.45= 4.30


RUN3
Simulation of Simple Calculator

Enter two numbers


12 12
Enter the operator [+,-,*,/]
*

12.00 * 12.00= 144.00

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

9. /* Write a C program to find the sum of 'N' natural numbers*/


ar

#include <stdio.h>
um

#include <conio.h>

void main()
{
K

int i, N, sum = 0;
k

clrscr();
ee

printf("Enter an integer number\n");


scanf ("%d", &N);
bh

for (i=1; i <= N; i++)


{
A

sum = sum + i;
}

printf ("Sum of first %d natural numbers = %d\n", N, sum);


}

/*----------------------------------------
Output
RUN1

Enter an integer number


10
Sum of first 10 natural numbers = 55

RUN2

Enter an integer number


50
Sum of first 50 natural numbers = 1275
------------------------------------------*/

10. /*Write a C program to generate and print first N FIBONACCI numbers*/

#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

fib3 = fib1 + fib2;


count ++;
um

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();

printf("Enter two numbers\n");


scanf("%d %d", &num1,&num2);

if (num1 > num2)


{

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

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() */
/*------------------------
K

Output
RUN 1
k

Enter two numbers


5
ee

15
GCD of 5 and 15 = 5
bh

LCM of 5 and 15 = 15
------------------------------*/
A

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();
printf("Enter the value of N\n");
scanf ("%d", &N);

for (i=1; i <=N; i++)


{
if (i % 2 == 0)
evenSum = evenSum + i;
else
oddSum = oddSum + i;
}

printf ("Sum of all odd numbers = %d\n", oddSum);

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

Sum of all even numbers = 650


um

------------------------------*/

13. /* Write a C program to reverse a given integer number and check *


* whether it is a palindrome. Output the given numbers with suitable*
K

* 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);

temp = num; /* original number is stored at temp */

while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

printf("Given number is = %d\n", temp);


printf("Its reverse is = %d\n", rev);

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

* up to the given accuracy (without using user defined function) *


* Also print sin(x) using library function. */
um

#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

printf("Enter the value of x (in degrees)\n");


scanf("%f",&x);

x1 = x;

/* Converting degrees to radians*/

x = x*(3.142/180.0);
sinval = sin(x);

printf("Enter the accuary for the result\n");


scanf("%f", &acc);
term = x;
sinx = term;
n = 1;

do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;

} while(acc <= fabs(sinval - sinx));

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

Using Library function sin(45) = 0.707179


---------------------------------------------*/
k

15. /* Write a C program to find the value of cos(x) using the series *
ee

* up to the given accuracy (without using user defined function) *


* Also print cos(x) using library function. */
bh

#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();

printf("Enter the value of x (in degrees)\n");


scanf("%f",&x);
x1 = x;

/* Converting degrees to radians*/

x = x*(3.142/180.0);
cosval = cos(x);

printf("Enter the accuary for the result\n");


scanf("%f", &acc);
term = 1;
cosx = term;
n = 1;

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));

printf("Sum of the cosine series = %f\n", cosx);


ha
printf("Using Library function cos(%d) = %f\n", x1,cos(x));
} /*End of main() */
C
/*------------------------------
Output
Enter the value of x (in degrees)
ar

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

Using Library function cos(45) = 0.707035


---------------------------------------------*/
A

16. /* Write a C program to check whether a given number is prime or not *


* and output the given number with suitable message */

#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

17. /* Write a C program to generate and print prime numbers in a given *


* range. Also print the number of prime numbers */
A

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

void main()
{
int M, N, i, j, flag, temp, count = 0;

clrscr();

printf("Enter the value of M and N\n");


scanf("%d %d", &M,&N);

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

Enter the value of M and N


15 45
ee

Prime numbers are


17
bh

19
23
29
A

31
37
41
43
Number of primes between 15 and 45 = 8
-------------------------------------------*/

18. /* Write a C program to find the number of integers divisible by 5 *


* between the given range N1 and N2, where N1 < N2 and are integers.*
* Also find the sum of all these integer numbers that divisible by 5*
* and output the computed results */
#include <stdio.h>
#include <conio.h>

void main()
{
int i, N1, N2, count = 0, sum = 0;

clrscr();

printf ("Enter the value of N1 and N2\n");


scanf ("%d %d", &N1, &N2);

/*Count the number and compute their sum*/

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

5, 10, 15, 20, 25,


Number of integers divisible by 5 between 2 and 27 = 5
ee

Sum of all integers that are divisible by 5 = 75


------------------------------------------------------*/
bh

19. /* Write a C program to read N integers (zero, +ve and -ve) *


A

* into an array A and to *


* a) Find the sum of negative numbers *
* b) Find the sum of positive numbers and *
* c) Find the average of all input numbers *
* Output the various results computed with proper headings */

#include <stdio.h>

void main()
{
int i, N,num negsum=0, posum=0;
float total=0.0, averg;

clrscr();

printf ("Enter the value of N\n");


scanf("%d", &N);

printf("Enter %d numbers (-ve, +ve and zero)\n", N);

for(i=0; i< N ; i++)


{

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

total = total + num ;


}
um

averg = total / N;
printf("\nSum of all negative numbers = %d\n",negsum);
printf("Sum of all positive numbers = %d\n", posum);
K

printf("\nAverage of all input numbers = %.2f\n", averg);


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

Sum of all negative numbers = -10


Sum of all positive numbers = 11

Average of all input numbers = 0.20


--------------------------------------*/

20. /* Write a C program to input N numbers (integers or reals) *


* and store them in an array. Conduct a linear search for a *
* given key number and report success or failure in the form *
* of a suitable message */

#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("Input array is\n");


for(i=0; i<N ; i++)
um

{
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\n");
K

scanf("%d", &keynum);
k

/* Linear search begins */


for ( i=0; i < N ; i++)
ee

{
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

Entee the element to be searched


1000
um

Search is FAILED
--------------------------------------*/

21. /* Write a C program to sort N numbers in ascending order *


K

* 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();

printf("Enter the value of N\n");


scanf("%d",&N);

printf("Enter the elements one by one\n");


for(i=0; i<N ; i++)
{
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])
{

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

Enter the value of N


5
um

Enter the elements one by one


390
234
111
K

876
345
k

Input array is
390
ee

234
111
bh

876
345
Sorted array is...
A

111
234
345
390
876
---------------------------*/

22. /* Write a C program to accept N numbers sorted in ascending order *


* and to search for a given number using binary search. Report *
* sucess or fialure in the form of suitable messages */

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

void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&N);

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

if(array[j] > array[j+1])


{
um

temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
K

}
}
k

printf("Sorted array is...\n");


for(i=0; i<N ; i++)
ee

{
printf("%d\n",array[i]);
bh

printf("Enter the element to be searched\n");


A

scanf("%d", &keynum);

/* Binary searching begins */

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 */

if( keynum == array[mid] )


{
printf("SUCCESSFUL SEARCH\n");
}
else
{
printf("Search is FAILED\n");
}

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

Sorted array is...


1
um

2
3
4
Enter the element to be searched
K

4
SUCCESSFUL SEARCH
k

---------------------------*/
ee

23. /* Write a C program to input real numbers and find the *


* mean, variance and standard deviation */
bh

#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();

printf("Enter the value of N\n");


scanf("%d", &n);

printf("Enter %d real numbers\n",n);


for(i=0; i<n; i++)
{
scanf("%f", &x[i]);
}

/* Compute the sum of all elements */

for(i=0; i<n; i++)


{
sum = sum + x[i];

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

Average of all elements = 29.66


Varience of all elements = 213.89
Standard deviation = 14.62
A

-------------------------------------*/

24. /* Write a C program to read in four integer numbers into an array *


* and find the average of largest two of the given numbers without *
* sorting the array. The program should output the given four numbers*
* and the average with suitable headings. */

#include <stdio.h>
#include <conio.h>
#define MAX 4

void main()
{
int a[MAX], i, l1,l2,temp;

clrscr();

printf("Enter %d integer numbers\n", MAX);


for (i=0; i < MAX; i++)
{
scanf("%d", &a[i]);
}

printf("Input interger are\n");


for (i=0; i < MAX; i++)

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*/

if (l1 < l2)


{
ha
temp = l1;
C
l1 = l2;
l2 = temp;
}
ar

for (i=2;i<4;i++)
um

{
if (a[i] >= l1)
{
l2 = l1;
K

l1 = a[i];
}
k

else if(a[i] > l2)


{
ee

l2= a[i];
}
bh

printf("\n%d is the first largest\n", l1);


A

printf("%d is the second largest\n", l2);


printf("\nAverage of %d and %d = %d\n", l1,l2, (l1+l2)/2);

}
/*-----------------------------------
Output
RUN 1
Enter 4 integer numbers
45
33
21
10
Input interger are
45 33 21 10

45 is the first largest


33 is the second largest

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

90 is the first largest

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

Input interger are


100 200 300 400
um

400 is the first largest


300 is the second largest
K

Average of 400 and 300 = 350


k

------------------------------------*/
ee

25. /* Write a C program to evaluate the given polynomial *


* P(x)=AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0, by *
bh

* reading its coefficents into an array. [Hint:Rewrite *


* the polynomial as *
* P(x) = a0 + x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan)))) *
A

* 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();

printf("Enter the order of the polynomial\n");


scanf("%d", &N);

printf("Enter the value of x\n");


scanf("%f", &x);

/*Read the coefficients into an array*/

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

printf("Given polynomial is:\n");


for (i=0;i<= N;i++)
um

{
if (power < 0)
{
break;
K

}
k

/* printing proper polynomial function*/


if (a[i] > 0)
ee

printf(" + ");
else if (a[i] < 0)
bh

printf(" - ");
else
printf (" ");
A

printf("%dx^%d ",abs(a[i]),power--);

printf("\nSum of the polynomial = %6.2f\n",polySum);


}

/*-----------------------------------------------------
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

* trace of the resultant matrix. Output the given matrix, *


* their sum or Differences and the trace. */
um

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

void main()
{
k

int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];


int i, j, M,N, option;
ee

void trace (int arr[][10], int M, int N);


bh

clrscr();
A

printf("Enter the order of the matrice A and B\n");


scanf("%d %d", &M, &N);

printf("Enter the elements of matrix A\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&A[i][j]);
}
}
printf("MATRIX A is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

printf("Enter the elements of matrix B\n");


for(i=0; i<M; i++)
{

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

printf("Enter your option: 1 for Addition and 2 for Subtraction\n");


um

scanf("%d",&option);

switch (option)
{
K

case 1: for(i=0; i<M; i++)


{
k

for(j=0; j<N; j++)


{
ee

sumat[i][j] = A[i][j] + B[i][j];


}
bh

printf("Sum matrix is\n");


A

for(i=0; i<M; i++)


{
for(j=0; j<N; j++)
{
printf("%3d",sumat[i][j]) ;
}
printf("\n");
}

trace (sumat, M, N);


break;
case 2:for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
diffmat[i][j] = A[i][j] - B[i][j];
}
}

printf("Difference matrix is\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{

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

Enter the order of the matrice A and B


22
Enter the elements of matrix A
11
22
MATRIX A is
1 1
2 2
Enter the elements of matrix B
33
44
MATRIX B is
3 3
4 4
Enter your option: 1 for Addition and 2 for Subtraction
1
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. */

ty
#include <stdio.h>

or
#include <conio.h>

void main()

ab
{
int i,j,M,N;

kr
int A[10][10], B[10][10];

int transpose(int A[][10], int r, int c); /*Function prototype*/


ha
clrscr();
C
printf("Enter the order of matrix A\n");
scanf("%d %d", &M, &N);
ar

printf("Enter the elements of matrix\n");


um

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");
}

/* Finding Transpose of matrix*/


for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
B[i][j] = A[j][i];
}
}

printf("Its Transpose is\n");


for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%3d",B[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

28. /* Write a C program to read a string and check whether it is *


ee

* a palindrome or not (without using library functions). Output *


* the given string along with suitable message */
bh

#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);

for (i=0; string[i] != '\0'; i++) /*keep going through each */


{ /*character of the string */
length++; /*till its end */
}

for (i=length-1; i >= 0 ; i--)


{
revString[length-i-1] = string[i];
}

/*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
----------------------------------------------------------*/

29. /* Write a C program to read two strings and concatenate them *


* (without using library functions). Output the concatenated *
* string along with the given string */

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

void main()
{
char string1[20], string2[20];
int i,j,pos;

strset(string1, '\0'); /*set all occurrences in two strings to NULL*/


strset(string2,'\0');

printf("Enter the first string :");


gets(string1);
fflush(stdin);

ty
printf("Enter the second string:");

or
gets(string2);

printf("First string = %s\n", string1);

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

for (i=pos,j=0; string2[j]!='\0'; i++)


{
string1[i] = string2[j++];
}
K

string1[i]='\0'; /*set the last character of string1 to NULL*/


k

printf("Concatenated string = %s\n", string1);


ee

}
/*---------------------------------------
bh

Output
Enter the first string :CD-
Enter the second string:ROM
A

First string = CD-


Second string = ROM
Concatenated string = CD-ROM
----------------------------------------*/

30. /* Write a C program to read an English sentence and replace*


* lowercase characters by uppercase and vice-versa. Output *
* the given sentence as well as the case covrted sentence on*
* two different lines. */

#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';

count = i; /*shows the number of chars accepted in a sentence*/

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

Case changed sentence is: mERA bHARAT mAHAN


------------------------------------------------*/
k

31. /* Write a C program read a sentence and count the number of *


ee

* number of vowels and consonants in the given sentence. *


* Output the results on two lines with suitable headings */
bh

#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);

for(i=0; sentence[i] != '\0'; i++)


{
if((sentence[i] == 'a'||sentence[i] == 'e'||sentence[i] == 'i'||
sentence[i] == 'o'||sentence[i] == 'u') ||(sentence[i] == 'A'||
sentence[i] == 'E'||sentence[i] == 'I'|| sentence[i] == 'O'||
sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')

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

No. of consonants in Good Morning = 7


-----------------------------------------*/
um

32. /* Write a C program to read N names, store them in the form *


* of an array and sort them in alphabetical order. Output the*
* give names and the sorted names in two columns side by side*
K

* with suitable heading */


k

#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();

printf("Enter the value of N\n");


scanf("%d", &N);

printf("Enter %d names\n", N);


for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}

for(i=0; i < N-1 ; i++)


{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}

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 the value of N


3
um

Enter 3 names
Monica
Laxmi
Anand
K

----------------------------------------
k

Input Names Sorted names


----------------------------------------
ee

Monica Anand
Laxmi Laxmi
bh

Anand Monica
----------------------------------------
---------------------------------------- */
A

33. /* Develop functions *


* a) To read a given matrix *
* b) To output a matrix *
* c) To compute the product of twomatrices *
* *
* Use the above functions to read in two matrices A (MxN)*
* B (NxM), to compute the product of the two matrices, to*
* output the given matrices and the computed matrix in a *
* main function */
#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;

/*Function declarations*/

void readMatrix(int arr[][MAXCOLS], int M, int N);

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);

printf ("The product matrix is\n");


K

printMatrix(C,M,N);
}
k

/*Input matrix A*/


ee

void readMatrix(int arr[][MAXCOLS], int M, int N)


{
bh

int i, j;
for(i=0; i< M ; i++)
{
A

for ( j=0; j < N; j++)


{
scanf("%d",&arr[i][j]);
}
}
}
void printMatrix(int arr[][MAXCOLS], int M, int N)
{
int i, j;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
printf("%3d",arr[i][j]);
}
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++)

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
--------------------------------*/

34. /* Write a C program to read two integers M and N *


* and to swap their values. Use a user-defined *
* function for swapping. Output the values of *
* M and N before and after swapping with suitable*
* mesages */
#include <stdio.h>
#include <conio.h>

void main()
{
float M,N;

void swap(float *ptr1, float *ptr2 ); /* Function Declaration */

printf("Enter the values of M and N\n");


scanf("%f %f", &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

After Swapping:M = 29.00 N = 32.00


------------------------------------------*/
ee
bh

35. /* Write a C program to read N integers and store them *


* in an array A, and so find the sum of all these *
* elements using pointer. Output the given array and *
A

* and the computed sum with suitable heading */

#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);

a=(int *) malloc(n*sizeof(int)); /*Dynamix Memory Allocation */

printf("Enter Elements of First List\n");


for(i=0;i<n;i++)
{
scanf("%d",a+i);
}

ty
/*Compute the sum of all elements in the given array*/
for(i=0;i<n;i++)

or
{
sum = sum + *(a+i);

ab
}

printf("Sum of all elements in array = %d\n", sum);

kr
} /* End of main() */
ha
/*----------------------------
C
Output
Enter the size of array A
4
ar

Enter Elements of First List


10
um

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

printf("Enter a binary number(1s and 0s)\n");


scanf("%d", &num); /*maximum five digits */

bnum = num;

while( num > 0)


{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}

printf("The Binary number is = %d\n", bnum);


printf("Its decimal equivalent is =%d\n", dec);

} /* 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

printf("Enter the size of array AR\n");


scanf("%d",&n);
um

printf("Enter the elements of the array\n");


for(i=0;i<n;i++)
{
K

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++;
}
}

printf("The elements of OAR are\n");


for(i=0;i<j;i++)
{
printf("%ld\n",OAR[i]);
}

printf("The elements of EAR are\n");


for(i=0;i<k;i++)
{
printf("%ld\n", EAR[i]);
}
} /*End of main()*/
/*-------------------------------------
Output
Enter the size of array AR
6

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

38. /* Write a C program to generate Fibonacci sequence *


* Fibonacci sequence is 0 1 1 2 3 5 8 13 21 ... */

#include <stdio.h>
K

void main()
k

{
int fib1=0, fib2=1, fib3, limit, count=0;
ee

printf("Enter the limit to generate the fibonacci sequence\n");


bh

scanf("%d", &limit);

printf("Fibonacci sequence is ...\n");


A

printf("%d\n",fib1);
printf("%d\n",fib2);
count = 2; /* fib1 and fib2 are already used */

while( count < limit)


{
fib3 = fib1 + fib2;
count ++;
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
}
} /* End of main() */
/*-----------------------------------------------------------
Enter the limit to generate the fibonacci sequence
7
Fibonacci sequence is ...
0
1
1
2
3
5
8

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

printf("Enter how many elements\n");


scanf("%d", &n);
um

printf("Enter the elements\n");


for(i=0; i<n; i++)
{
K

scanf("%d", &x[i]);
}
k

printf("Input array elements are\n");


ee

for(i=0; i<n; i++)


{
bh

printf("%d\n", x[i]);
}
A

for(i=0; i< n; i++)


{
for(j=i+1; j<n; j++)
{
if (x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
printf("Sorted list is\n");
for(i=0; i<n; i++)
{
printf("%d\n", x[i]);
}

printf("Enter the element to be inserted\n");


scanf("%d",&key);

for(i=0; i<n; i++)


{
if ( key < x[i] )

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("Final list is\n");


for(i=0; i<n+1; i++)
um

{
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;

printf("Enter the number\n"); /*For better programming,choose 'long int' */


scanf("%ld", &num);
ha
C
temp = num;

while(num > 0)
ar

{
digit = num % 10;
um

rev = rev * 10 + digit;


num /= 10;
}
K

printf("Given number = %ld\n", temp);


printf("Its reverse is = %ld\n", rev);
k

}
ee

/* ---------------------------
Output
bh

Enter the number


123456
Given number = 123456
A

Its reverse is = 654321


------------------------------*/

41. /* 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 '*' */

#include <stdio.h>
#include <conio.h>
void main()
{
char pasword[10],usrname[10], ch;
int i;

clrscr();

printf("Enter User name: ");


gets(usrname);
printf("Enter the password <any 8 characters>: ");

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

Your password is :Wipro123


-----------------------------------------------*/
k

42. /* Write a C program to accept a string and a substring and


ee

* check if the substring is present in the given strig */


bh

#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);

puts("Enter search substring:");


gets(search);

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

Hello how are you?


Enter search substring:
how
k

SEARCH SUCCESSFUL!
ee

------------------------------*/

43. /* Write a c program to compute the surface area and *


bh

* volume of a cube */
A

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

void main()
{
float side, surfArea, volume;

printf("Enter the length of a side\n");


scanf("%f", &side);

surfArea = 6.0 * side * side;


volume = pow (side, 3);

printf("Surface area = %6.2f and Volume = %6.2f\n", 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
------------------------------------------*/

44. /* Write a C program to accept a decimal number and convert it to binary *

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

while( num > 0 )


{
rem = num % 2;
K

if ( rem == 1 ) /*To count no.of 1s*/


{
k

no_of_1s++;
}
ee

bin = bin + rem * base;


num = num / 2 ;
bh

base = base * 10;


}
printf("Input number is = %d\n", dnum);
A

printf("Its Binary equivalent is = %ld\n", bin);


printf("No.of 1's in the binary number is = %d\n", no_of_1s);

} /* 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

-----------------------------------------------------*/

45. /* Write a c program to find whether a given year *


* is leap year or not */

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

2007 is not a leap year


------------------------------------------*/
bh

46. /* Write a c program to swap the contents of two numbers *


* using bitwise XOR operation. Don't use either the *
A

* temporary variable or arithmetic operators */

#include <stdio.h>

void main()
{
long i,k;

printf("Enter two integers\n");


scanf("%ld %ld",&i,&k);
printf("\nBefore swapping i= %ld and k = %ld",i,k);

i = i^k;
k = i^k;
i = i^k;

printf("\nAfter swapping i= %ld and k = %ld",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

int ndays, year, week, days;


um

printf("Enter the number of days\n");


scanf("%d",&ndays);

year = ndays/365;
K

week = (ndays % 365)/DAYSINWEEK;


days = (ndays%365) % DAYSINWEEK;
k

printf ("%d is equivalent to %d years, %d weeks and %d days\n",


ee

ndays, year, week, days);


}
bh

/*-----------------------------------------------
Output
A

Enter the number of days


375
375 is equivalent to 1 years, 1 weeks and 3 days

Enter the number of dayy


423
423 is equivalent tt 1 years, 8 weeks and 2 days

Enter the number of days


1497
1497 is equivalent to 4 years, 5 weeks and 2 days
---------------------------------------------------*/
48. /* Program to accepts two strings and compare them. Finally it prints *
* whether both are equal, or first string is greater than the second *
* or the first string is less than the second string */

#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

differ or until the end of the strings is reached.*/

while ( (i < count1) && (i < count2))


{
K

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

49. /* Program to accept two strings and concatenate them *


* i.e.The second string is appended to the end of the first string */
um

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

void main()
k

{
char string1[20], string2[20];
ee

int i,j,pos;
bh

strset(string1, '\0'); /*set all occurrences in two strings to NULL*/


strset(string2,'\0');
A

printf("Enter the first string :");


gets(string1);
fflush(stdin);

printf("Enter the second string:");


gets(string2);

printf("First string = %s\n", string1);


printf("Second string = %s\n", string2);

/*To concate the second stribg to the end of the string


travserse the first to its end and attach the second string*/
for (i=0; string1[i] != '\0'; i++)
{
; /*null statement: simply trvsering the string1*/
}

pos = i;

for (i=pos,j=0; string2[j]!='\0'; i++)


{
string1[i] = string2[j++];
}

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
----------------------------------------*/

50. /* Write a c program to find the length of a string *


ar

* without using the built-in function */


um

#include <stdio.h>

void main()
{
K

char string[50];
int i, length = 0;
k

printf("Enter a string\n");
ee

gets(string);
bh

for (i=0; string[i] != '\0'; i++) /*keep going through each */


{ /*character of the string */
length++; /*till its end */
A

}
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
----------------------------------------------------------*/

51. /* Write a c program to find the length of a string


* without using the built-in function also check
* whether it is a palindrome or not
*/

#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

length++; /*till its end */


}
um

printf("The length of the string \'%s\' = %d\n", string, length);

for (i=length-1; i >= 0 ; i--)


{
K

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

for (i=0; i < length ; i++)


{
if (revString[i] == string[i])
A

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

/*Traverse the string to count the number of characters*/


while (str[count]!='\0')
{
K

count++;
}
k

/*Finding the frequency of the word 'the'*/


for(i=0;i<=count-3;i++)
ee

{
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);

printf("Enter the values of X and N\n");

ty
scanf("%ld %ld",&x,&n);

or
xpown = power (x,n);

printf("X to the power N = %ld\n");

ab
}

kr
/*Recursive function to computer the X to power N*/

long int power(int x, int n)


{
ha
if (n==1)
C
return(x);
else if ( n%2 == 0)
return (pow(power(x,n/2),2)); /*if n is even*/
ar

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

Enter the values offX and N


44
bh

X to the power N ==256

RUN3
A

Enter the values of X and N


52
X to the power N = 25

RUN4
Enter the values of X and N
10 5
X to the power N = 100000
-----------------------------------------*/

54. /* Write a C program to accept two matrices and *


* find the sum and difference of the matrices */
#include <stdio.h>
#include <stdlib.h>

int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];


int i, j, R1, C1, R2, C2;

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("Enter the elements of matrix B\n");


readmatB();
ee

printf("MATRIX B is\n");
printmatB();
bh

sum();
diff();
A

}
} /* main() */

/* Function to read a matrix A */


void readmatA()
{
for(i=0; i<R1; i++)
{
for(j=0; j<C1; j++)
{
scanf("%d",&A[i][j]);
}
}
return;
}

/* Function to read a matrix B */


void readmatB()
{
for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
scanf("%d",&B[i][j]);
}

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

/* Function to print a matrix B */


um

void printmatB()
{
for(i=0; i<R2; i++)
{
K

for(j=0; j<C2; j++)


{
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;
}

/*Function to find the difference of elements of matrix A and Matrix B*/


void diff()
{
for(i=0; i<R1; i++)
{

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;

printf("Enter the order of the matrix A\n");

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

printf("Enter the elements of matrix B\n");


for(i=0; i<R2; i++)
K

{
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");
}

/* Comparing two matrices for equality */

if(R1 == R2 && C1 == C2)


{
printf("Matrices can be compared\n");
for(i=0; i<R1; i++)
{

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
-------------------------------------------------------*/

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;

printf("Enter the order of the matrix A\n");


scanf("%d %d", &R, &C);

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

/* Check for unit (or identity) matrix */


K

for(i=0; i<R; i++)


{
k

for(j=0; j<C; j++)


{
ee

if(A[i][j] != 1 && A[j][i] !=0)


{
bh

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
------------------------------------------*/

57. /* Write a C program to accept a matrix of given order and


ha
* interchnge any two rows and columns in the original matrix*/
C
#include <stdio.h>

void main()
ar

{
static int m1[10][10],m2[10][10];
um

int i,j,m,n,a,b,c, p, q, r;

printf ("Enter the order of the matrix\n");


scanf ("%d %d",&m,&n);
K

printf ("Enter the co-efficents of the matrix\n");


k

for (i=0; i<m;++i)


{
ee

for (j=0;j<n;++j)
{
bh

scanf ("%d,",&m1[i][j]);
m2[i][j] = m1[i][j];
}
A

printf ("Enter the numbers of two rows to be exchnged \n");


scanf ("%d %d", &a,&b);

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);

printf ("The given matrix is \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
printf (" %d",m2[i][j]);
printf ("\n");
}

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

Enter the order of the matrix


33
bh

Enter the co-efficents of the matrix


124
579
A

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
-------------------------------------------------------------------------*/

58. /* Write a C program to display the inventory of items in a store/shop *


* The inventory maintains details such as name, price, quantity and *
* manufacturing date of each item. */

#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

struct date mfg;


};

struct details item[50];


K

int n,i;
k

clrscr();
ee

printf("Enter number of items:");


scanf("%d",&n);
bh

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

Item name:Milk Powder


Item code:345
um

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

Item name:Washing Soap


Item code:890
bh

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

printf("The Binary number is = %d\n", bnum);


printf("Its decimal equivalent is =%d\n", dec);
um

} /* End of main() */

/*---------------------------------------------------
K

Outpput
Enter the binary number(1s and 0s)
k

1010
The Binary number is = 1010
ee

Its decimal equivalent is =10


----------------------------------------------------*/
bh

/* Write a c program to multifly given number by 4 *


* using bitwise operators */
A

#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

printf ("Enter the numbers\n");


for (i=0; i<n; ++i)
um

{
scanf ("%d", &number[i]);
}
number[n] = number[0];
K

for (i=0; i<n; ++i)


k

{
number[i] = number[i+1];
ee

}
printf ("Cyclically permted numbers are given below \n");
bh

for (i=0; i<n; ++i)


printf ("%d\n", number[i]);
}
A

/*-------------------------------------
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
---------------------------------------------------*/

59. /* Write a C program to accept N numbers and arrange them in an asceding


order */

#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]);

for (i=0; i<n; ++i)


{
ha
for (j=i+1; j<n; ++j)
C
{
if (number[i] > number[j])
{
ar

a= number[i];
number[i] = number[j];
um

number[j] = a;
}
}
}
K

printf ("The numbers arrenged in ascending order are given below\n");


k

for (i=0; i<n; ++i)


printf ("%d\n",number[i]);
ee

} /* 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
------------------------------------------------------------------*/

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;

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 ("The numbers arranged in descending order are given below\n");


for (i=0; i<n; ++i)
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 (i=0; i<n; ++i)


ar

{
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 ("The numbers arranged in descending order are given below\n");


for (i=0; i<n; ++i)
{
A

printf ("%d\n",number[i]);
}

printf ("The 2nd largest number is = %d\n", number[1]);


printf ("The 2nd smallest number is = %d\n", number[n-2]);

ave = (number[1] +number[n-2])/2;


counter = 0;

for (i=0; i<n; ++i)


{
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() */

/*-------------------------------------------------------

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

The 2nd largest number is = 80


The 2nd smallest number is = 30
The average of 80 and 30 is = 55 is not in the array
K

-------------------------------------------------------*/
k

61. /* Write a C program to accept an array of integers and delete the *


ee

* specified integer from the list */


bh

#include <stdio.h>

void main()
A

{
int vectx[10];
int i, n, pos, element, found = 0;

printf("Enter how many elements\n");


scanf("%d", &n);

printf("Enter the elements\n");


for(i=0; i<n; i++)
{
scanf("%d", &vectx[i]);
}
printf("Input array elements are\n");
for(i=0; i<n; i++)
{
printf("%d\n", vectx[i]);
}

printf("Enter the element to be deleted\n");


scanf("%d",&element);

for(i=0; i<n; i++)


{
if ( vectx[i] == element)

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("The resultant vector is \n");


ar

for(i=0; i<n-1; i++)


{
um

printf("%d\n",vectx[i]);
}
}
else
K

printf("Element %d is not found in the vector\n", element);


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

printf ("Enter the value of n\n");


ee

scanf ("%d",&n);
bh

printf ("enter the numbers\n");


for (i=0; i<n; ++i)
scanf ("%d", &number[i]);
A

printf ("Enter the position of the element to split the array \n");
scanf ("%d",&a);

for (i=0; i<a; ++i)


{
number[n] = number[0];

for (j=0; j<n; ++j)


{
number[j] = number[j+1];
}
}

printf("The resultant array is\n");


for (i=0; i<n; ++i)
{
printf ("%d\n",number[i]);
}
} /* End of main() */

/*-------------------------------------------------
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

63. /* Write a C program to find the frequency of odd numbers *


um

* and even numbers in the input of a matrix */

#include <stdio.h>
K

void main ()
{
k

static int ma[10][10];


int i,j,m,n,even=0,odd=0;
ee

printf ("Enter the order ofthe matrix \n");


bh

scanf ("%d %d",&m,&n);

printf ("Enter the coefficients if matrix \n");


A

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");
}

printf ("\nThe frequency of occurance of odd number = %d\n",odd);


printf ("The frequency of occurance of even number = %d\n",even);

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

The frequency of occurance of odd number = 5


um

The frequency of occurance of even number = 4

-------------------------------------------------------*/
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

with that of the secondary diagonal elements */


ee

#include <stdio.h>
bh

void main ()
{
A

static int ma[10][10];


int i,j,m,n,a;

printf ("Enetr the order of the matix \n");


scanf ("%d %d",&m,&n);

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]);
}
}

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
}

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 ("THe matrix after changing the \n");


ha
printf ("main diagonal & secondary diagonal\n");
for (i=0;i<m;++i)
C
{
for (j=0;j<n;++j)
{
ar

printf (" %d",ma[i][j]);


}
um

printf ("\n");
}
}
else
K

printf ("The givan order is not square matrix\n");


k

} /* end of main() */
ee

/*----------------------------------------------------
Output
bh

Enetr the order of the matix


33
Enter the co-efficients of the matrix
A

123
456
789
The given matrix is
123
456
789
THe matrix after changing the
main diagonal & secondary diagonal
321
456
987
----------------------------------------------------------*/

65. /* Write a C program to accept the height of a person in centermeter and *


* categorize the person based on height as taller, dwarf and *
* average height person */

#include <stdio.h>
void main()
{
float ht;

printf("Enter the Height (in centimetres)\n");

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

Enter the Height (in centimetres)


45
um

Dwarf
----------------------------------------*/

66. /* Write a C program to accept a coordinate point in a XY coordinate system *


K

* and determine its quadrant */


k

#include <stdio.h>
ee

void main()
{
bh

int x,y;

printf("Enter the values for X and Y\n");


A

scanf("%d %d",&x,&y);

if( x > 0 && y > 0)


printf("point (%d,%d) lies in the First quandrant\n");
else if( x < 0 && y > 0)
printf("point (%d,%d) lies in the Second quandrant\n");
else if( x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quandrant\n");
else if( x > 0 && y < 0)
printf("point (%d,%d) lies in the Fourth quandrant\n");
else if( x == 0 && y == 0)
printf("point (%d,%d) lies at the origin\n");
} /* 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
-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(" 1 --> Circle\n");


printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
K

printf("-------------------------\n");
k

printf("Enter the Figure code\n");


scanf("%d",&fig_code);
ee

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;

case 2: printf("Enter the bredth and length\n");


scanf("%f %f",&bredth, &length);
area=bredth *length;
printf("Area of a Reactangle=%f\n", area);
break;

case 3: printf("Enter the base and height\n");


scanf("%f %f",&base,&height);
area=0.5 *base*height;
printf("Area of a Triangle=%f\n", area);
break;

case 4: printf("Enter the side\n");


scanf("%f",&side);
area=side * side;
printf("Area of a Square=%f\n", area);
break;

default: printf("Error in figure code\n");


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;

printf("Enter the grade\n");


scanf("%c",&grade);

grade=toupper(grade); /* lower case letter to upper case */

ty
switch(grade)

or
{
case 'S': strcpy(remark," SUPER");
break;

ab
case 'A': strcpy(remark," VERY GOOD");

kr
break;

case 'B': strcpy(remark," FAIR");


break;
ha
C
case 'Y': strcpy(remark," ABSENT");
break;
ar

case 'F': strcpy(remark," FAILS");


break;
um

default : strcpy(remark, "ERROR IN GRADE\n");


break;
K

} /* End of switch */
k

printf("RESULT : %s\n",remark);
ee

} /* End of main() */
bh

/*-------------------------------------------
Output
RUN 1
A

Enter the grade


s
RESULT : SUPER

RUN 2
Enter the grade
y
RESULT : ABSENT
-----------------------------------------------*/

69. /* Write a C program to find the factorial of a given number */


#include <stdio.h>
void main()
{
int i,fact=1,num;

printf("Enter the number\n");


scanf("%d",&num);

if( num <= 0)


fact = 1;
else
{
for(i = 1; i <= num; i++)

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

69. /* Write a C program to accept a string and find the sum of *


* all digits present in the string */

#include <stdio.h>
K

void main()
{
k

char string[80];
int count,nc=0,sum=0;
ee

printf("Enter the string containing both digits and alphabet\n");


bh

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 */

printf("NO. of Digits in the string=%d\n",nc);


printf("Sum of all digits=%d\n",sum);

} /* 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 = sum + m1[i][j] ;


}
ee

printf ("Sum of the %d row is = %d\n",i,sum);


sum = 0;
bh

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

printf ("The given matrix is \n");


for (i=0;i<m;++i)
A

{
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];
}

printf ("\nThe sum of the main diagonal elements is = %d\n",sum);


printf ("The sum of the off diagonal elemets is = %d\n",a);
}
else
printf ("The givan order is not square matrix\n");
} /* End of main() */

/*--------------------------------------------------------
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

The sum of the main diagonal elements is = 15


The sum of the off diagonal elemets is = 15
ha
C
Run 2
Enetr the order of the matix
23
ar

The givan order is not square matrix


um

--------------------------------------------------------*/

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

* elements and Normal is defined as squre root of the sum of all *


* the elements */
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;

printf ("Enter the order of the matrix\n");


scanf ("%d %d", &m,&n);

printf ("Enter the ncoefficients of the matrix \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
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() */
/*---------------------------------------------------

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

Trace of the matrix is = 10

-----------------------------------------------------*/
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;

printf ("Enter the order of the matrix \n");


A

scanf ("%d %d",&m,&n);

printf ("Enter the coefiicients of the matrix\n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
}
}
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");
}

printf ("Transpose of matrix is \n");


for (j=0;j<n;++j)
{
for (i=0;i<m;++i)
{

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

74. /* Write a C program to accept a matrix and determine whether *


k

* it is a sparse matrix. A sparse martix is matrix which *


* has more zero elements than nonzero elements */
ee

#include <stdio.h>
bh

void main ()
{
A

static int m1[10][10];


int i,j,m,n;
int counter=0;

printf ("Enter the order of the matix\n");


scanf ("%d %d",&m,&n);

printf ("Enter the co-efficients of the matix\n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&m1[i][j]);
if (m1[i][j]==0)
{
++counter;
}
}
}
if (counter>((m*n)/2))
{
printf ("The given matrix is sparse matrix \n");
}
else
printf ("The given matrix is not a sparse matrix \n");

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

Enter the order of the matix


33
um

Enter the co-efficients of the matix


100
001
010
K

The given matrix is sparse matrix


There are 6 number of zeros
k

-----------------------------------------------*/
ee

75. /* Write a C program to accept a matrix of order MxN and sort all rows *
bh

* of the matrix in ascending order and all columns in descendng order */

#include <stdio.h>
A

void main ()
{
static int ma[10][10],mb[10][10];
int i,j,k,a,m,n;

printf ("Enter the order of the matrix \n");


scanf ("%d %d", &m,&n);

printf ("Enter co-efficients of the matrix \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
mb[i][j] = ma[i][j];
}
}
printf ("The given matrix is \n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}

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

printf ("After arranging the columns in descending order \n");


A

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

-----------------------------------------------------*/

76. /* Write a C program to accept a set of names and sort them in *


* an alphabetical order, Use structures to store the names */
K

#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;

void sort(int N); /* Function declaration */

clrscr();
fflush(stdin);
printf("Enter the number of students in the list\n");
scanf("%d",&no);

for(i = 0; i < no; i++)


{
printf("\nEnter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);

printf("Enter the roll number of %d : ", i+1);


scanf("%d",&stud[i].rno);
temp[i] = stud[i];

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

/* Display the sorted names */


for(i=0;i<no;i++)
um

{
printf("%-10s\t%3d\n",stud[i].name,stud[i].rno);

}
K

printf("\n*****************************\n");
} /* End of main() */
k

/* Function to sort the given names */


ee

void sort(int N)
{
bh

int i,j;
NAME temp;
A

for(i = 0; i < N-1;i++)


{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */

/*--------------------------------------------------------
Enter the number of students in the list
5

Enter the name of person 1 : Rajashree


Enter the roll number of 1 : 123

Enter the name of person 2 : John


Enter the roll number of 2 : 222

Enter the name of person 3 : Priya

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

Names after sorting

*****************************
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;

printf("\nInput sentence is : %s ",sentence);

printf("\nResultant sentence is : ");


for(i=0; i < count; i++)
{
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[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

printf("How many Elements in each array...\n");


scanf("%d", &n);
A

a = (int *) malloc(n*sizeof(int));
b = (int *) malloc(n*sizeof(int));
c =( int *) malloc(n*sizeof(int));

printf("Enter Elements of First List\n");


for(i=0;i<n;i++)
{
scanf("%d",a+i);
}

printf("Enter Elements of Second List\n");


for(i=0;i<n;i++)
{
scanf("%d",b+i);
}

for(i=0;i<n;i++)
{
*(c+i) = *(a+i) + *(b+i);
}

printf("Resultant List is\n");


for(i=0;i<n;i++)
{
printf("%d\n",*(c+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

79. /* Write a C program to find the sum of all elements of *


bh

* an array using pointersas arguments */

#include <stdio.h>
A

void main()
{
static int array[5]={ 200,400,600,800,1000 };
int sum;

int addnum(int *ptr); /* function prototype */

sum = addnum(array);

printf("Sum of all array elements = %5d\n", sum);


} /* End of main() */

int addnum(int *ptr)


{
int index, total=0;

for(index = 0; index < 5; index++)


{
total += *(ptr+index);
}
return(total);
}

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

void swap34(float *ptr1, float *ptr2 ); /* Function Declaration */


um

printf("How many Elements...\n");

scanf("%d", &n);
K

printf("Enter Elements one by one\n");


k

for(i=0;i<n;i++)
ee

{
scanf("%f",x+i);
bh

swap34(x+2, x+3); /* Function call:Interchanging 3rd element by 4th */


A

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 */

void swap34(float *ptr1, float *ptr2 ) /* Function Definition */


{
float temp;

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

81. /* Write a C program to illustrate the concept of unions*/


ee

#include <stdio.h>
bh

#include <conio.h>

void main()
A

{
union number
{
int n1;
float n2;
};

union number x;

clrscr() ;

printf("Enter the value of n1: ");


scanf("%d", &x.n1);
printf("Value of n1 =%d", x.n1);

printf("\nEnter the value of n2: ");


scanf("%d", &x.n2);
printf("Value of n2 = %d\n",x.n2);

} /* 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

printf("The size of union =%d\n", sizeof(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

printf("Enter the salary\n");


um

scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);
K

}
k

/*---------------------------------------------------------------------------
Output
ee

Enter the name


Prabhu
bh

Enter the age


25
Enter the salary
A

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;

printf("Enter the filename to be opened\n");


gets(filename);

fptr = fopen (filename, "r"); /*open for reading*/

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

85. Prints the value of a variable and it's memory address


bh

#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
}

86. Converts a temperature given in cel into far.


ar

#include <stdio.h>
um

int main()
{
float f, c;
K

printf("Enter the temp in c: \n");


scanf("%f", &c);
k

f=32 + (9*c)/5;
printf("The temperature in f=%f/n", f);
ee

return 0;
}
bh

87. Programe checks, wheather a number is divisible by 2 and 3


A

#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);
}

88. Adds the digits of a given number.

#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

89. Find the sum of the given series : 2+5+8+......


K

#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

for(i=0; i<num; i++)


{
sum += x;
x +=3;
}

printf("The sum is : %d\n",sum);


return(0);
}

90. ADD 1+1/2+1/3+1/4.......+1/n (n will be given by the user)


#include <stdio.h>
int main()
{
int num, i, x=2;
float sum=1.0;
printf("Enter a number to get the sum upto:\n");
scanf("%d",&num);
for(i=1; i<num; i++)
{
sum +=1.0/x;
x++;

ty
}

or
printf("The sum is : %f\n",sum);
return(0);

ab
}

91. 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>
C
#define DAYSINWEEK 7

void main()
ar

{
int ndays, year, week, days;
um

printf("Enter the number of days\n");


scanf("%d",&ndays);
K

year = ndays/365;
k

week = (ndays % 365)/DAYSINWEEK;


days = (ndays%365) % DAYSINWEEK;
ee

printf ("%d is equivalent to %d years, %d weeks and %d days\n",


bh

ndays, year, week, days);


}
A

92. Finds gcd of two given numbers

#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

printf("Enter two integers\n");


scanf("%ld %ld",&i,&k);
K
k

printf("\nBefore swapping i= %ld and k = %ld",i,k);


ee

i = i^k;
k = i^k;
bh

i = i^k;
A

printf("\nAfter swapping i= %ld and k = %ld",i,k);

94. Calculates the factorial of a given number

#include <stdio.h>
int main()
{
int i,num,x=1;
long fact=1;
printf("Enter a number :\n");
scanf("%d",&num);

for(i=0; i<num; i++)


{
fact *= x;
x++;
}

printf("The factorial of %d is : %ld\n",num,fact);


return(0);
}

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

for(i=1; i<num; i++)


um

{
new = first + second;
printf("Fibonacci [%2d] is : %ld\n",i+1,new);
K

first = second;
second = new;
k

}
return(1);
ee

}
bh

96. Finds out the simple ratio of two given numbers.


A

#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

sum += num * bs;


k

printf("The binary conversion of %d is : %d\n",n,sum);


ee

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 ;

printf("Enter a binary number(1s and 0s)\n");


scanf("%d", &num); /*maximum five digits */

bnum = num;
while( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}

printf("The Binary number is = %d\n", bnum);


printf("Its decimal equivalent is =%d\n", dec);

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

100. Area of the circle using macro definitiom


um

#include<stdio.h>
# define pi 3.142
K

int main()
k

{
float a,r;
ee

printf("enter the value of radius of r:\n");


scanf("%f",&r);
bh

a=pi*r*r;
printf("area of the circle a=%f\n",a);
A

return 1;
}

101. Write a C program to read an English sentence and replace


lowercase characters by uppercase and vice-versa. Output
the given sentence as well as the case covrted sentence on
two different lines.

#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*/

printf("The given sentence is : %s",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

102. Write a C program read a sentence and count the number of


K

number of vowels and consonants in the given sentence.


Output the results on two lines with suitable headings
k

#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);

for(i=0; sentence[i] != '\0'; i++)


{
if((sentence[i] == 'a'||sentence[i] == 'e'||sentence[i] == 'i'||
sentence[i] == 'o'||sentence[i] == 'u') ||(sentence[i] == 'A'||
sentence[i] == 'E'||sentence[i] == 'I'|| sentence[i] == 'O'||
sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
{

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("Enter a word : \n");


gets(ip);
l=strlen(ip);

for (i=0; i<l; i++)


{
for(j=0; j<=i; j++)
{
printf("%c ", ip[j]);

}
printf("\n");
}
return(1);
}

104. Sums the elements of an array.

#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("The reverse of the word :");


for(i=n-1; i>=0; i--)
bh

{
printf("%c",name[i]);
A

}
printf("\n");

return(1);
}

106. Program for concatanation, copy and length of string

#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

int max1, max2, n, i, x=1;


printf("How many numbers (less than 10) u want to compare :\n");
k

scanf("%d",&n);
for(i=0; i<n; i++)
ee

{
printf("number %d :\n",x);
bh

x++;
scanf("%d",&nums[i]);
A

printf("The numbers are:\n");


for(i=0; i<n; i++)
{
printf("%d ",nums[i]);
}
printf("\n");

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

109. For input: National Institute Science Tetcnology , output: N. I. S. T.


bh

#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");

printf("The reverse of the word :");


n=strlen(name);
for(i=n-1; i>=0; i--)
{
printf("%c",name[i]);
}
printf("\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

grade=toupper(grade); /* lower case letter to upper case */


um

switch(grade)
{
case 'S': strcpy(remark," SUPER");
K

break;
k

case 'A': strcpy(remark," VERY GOOD");


break;
ee

case 'B': strcpy(remark," FAIR");


bh

break;
A

case 'Y': strcpy(remark," ABSENT");


break;

case 'F': strcpy(remark," FAILS");


break;

default : strcpy(remark, "ERROR IN GRADE\n");


break;

} /* End of switch */
printf("RESULT : %s\n",remark);

112. Write a C program to accept a string and a substring and


check if the substring is present in the given string

#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();
}

113. Adds the digits of a number using a user defined function.

#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 */

114. Swaps the value of two variables with function \


ar

#include <stdio.h>
um

void swap(int *x,int *y);

int main()
K

{
int x, y;
k

printf("Enter two values of x and y:\n");


scanf("%d%d",&x,&y);
ee

printf("Before swapping X=%d and Y=%d\n", x,y);


swap(&x,&y);
bh

printf("After swapping X=%d and Y=%d\n", x,y);


return 0;
A

void swap(int *x, int *y)


{
int t=*x;
*x=*y;
*y=t;
}
115. Write a C program to compute the value of X ^ N given X and N as inputs
using recursive function

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

long int power(int x, int n);


void main()
{
long int x,n,xpown;
long int power(int x, int n);

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

116. Calculates the GCD of two numbers with function


k

#include <stdio.h>
ee

int gcd(int x, int y);


bh

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;
}

int gcd(int x, int y)


{
int min, max, r;
if(x>y)
{
max=x;
min=y;
}
else
{
max=y;
min=x;
}
r=max%min;
while(r!=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>

void str_concat(char *source, char *destination);


ar

int main()
um

{
char firststr[15], secondstr[15];
int i=0;
K

printf("enter one string:\n");


scanf("%s", firststr);
k

printf("enter the second string:\n");


scanf("%s", secondstr);
ee

str_concat(firststr, secondstr);
printf("the new string is:\n%s\n", firststr);
bh

return 0;
}
A

void str_concat(char *source, char *destination)


{
int s=0, d=0;
while(source[s]!='\0')
{
s++;
} /* end of while loop */
while(destination[d]!='\0')
{
source[s]=destination[d];
s++;
d++;
} /* end of 2nd while */
source[s]='\0';

} /* end of function */

118. Function to copy string

#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

void str_copy(char *source, char *destination)


um

{
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

int leg_str(char *source)


{
int c=0;
while(source[c]!='\0')
{
c++;
} /* end of while */
return(c);
} /*end of function */
119. Userdefined function to find a string's length.

#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

120. Calculates the factorial of a number by using recursive function call.


um

#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

printf("The factorial of %ld is : %ld\n",num,factorial);


return(0);
A

long fact(long x)
{
if(x==0)
return (1);
else
return(x * fact(x-1));
}

121. Write a C program to accept an array of 10 elements and swap 3rd


element with 4th element using pointers. And display the results

#include <stdio.h>

void main()
{

float x[10];
int i,n;
void swap34(float *ptr1, float *ptr2 ); /* Function Declaration */

printf("How many Elements...\n");

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

121. Write a C program to find the sum of all elements of


an array using pointers as arguments
K

#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);

printf("Sum of all array elements = %5d\n", sum);

} /* End of main() */

int addnum(int *ptr)


{
int index, total=0;
for(index = 0; index < 5; index++)
{
total += *(ptr+index);
}
return(total);
}

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

printf("The avarage of the elements in array is : %f\n",avg);


return 1;
um

123. Write a C program to read N names, store them in the form


K

of an array and sort them in alphabetical order. Output the


give names and the sorted names in two columns side by side
k

with suitable heading


ee

#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();

printf("Enter the value of N\n");


scanf("%d", &N);

printf("Enter %d names\n", N);


for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}
for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);

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

124. Prints the sum of each row and column of a matrix


K

#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

printf("enter %d elements:\n", m*n);


for(i=0; i<m; i++)
k

{
for(j=0; j<n; j++)
ee

{
scanf("%d", &a[i][j]);
bh

}
}
A

for(i=0; i<m; i++)


{
for(j=0; j<n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}

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;

printf("Enter the order of the matrix A\n");


scanf("%d %d", &R, &C);

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

/* Check for unit (or identity) matrix */


k

for(i=0; i<R; i++)


{
ee

for(j=0; j<C; j++)


{
bh

if(A[i][j] != 1 && A[j][i] !=0)


{
A

flag = 0;
break;
}
}
}

if(flag == 1 )
printf("It is identity matrix\n");
else
printf("It is not a identity matrix\n");
}

127. Writes the Rs amount in words.

#include <stdio.h>

void fi(int x);


void f2(int x);
void f3(int x);
void f4(int x);
void f5(int x);
void f6(int x);

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"};

static char *a2[]={"Zero", "Ten", "Twenty", "Thirty", "Fourty", "Fifty",

kr
"Sixty", "Seventy", "Eighty", "Ninty"};
int i;
ha
int main()
C
{
double num, num1;
long rs;
ar

printf("Enter the amount in Rs :\n");


scanf("%lf", &num);
um

rs=num;
printf("Rupees ");
if(rs<20) f1(rs);
K

else if((rs>19) && (rs<100)) f2(rs);


else if((rs>99) && (rs<1000)) f3(rs);
k

else if((rs>999) && (rs<100000)) f4(rs);


else if((rs>99999)&&(rs<10000000)) f5(rs);
ee

else if((rs>9999999)&&(rs<1000000000)) f6(rs);


bh

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

else if (i!=0) f3(i);


}
ee

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

printf("Enter the size of array A\n");


scanf("%d", &n);
um

a=(int *) malloc(n*sizeof(int)); /*Dynamix Memory Allocation */


K

printf("Enter Elements of First List\n");


for(i=0;i<n;i++)
k

{
scanf("%d",a+i);
ee

}
bh

/*Compute the sum of all elements in the given array*/


for(i=0;i<n;i++)
A

{
sum = sum + *(a+i);
}

printf("Sum of all elements in array = %d\n", sum);

} /* 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
}

for(i=0; i<n; i++)


ar

{
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;

printf("Enter the filename to be opened\n");


gets(filename);

fptr = fopen (filename, "r"); /*open for reading*/


if (fptr == NULL)
{
printf("Cannot open file\n");
exit(0);
}

ch = fgetc(fptr);

while (ch != EOF)


{
printf ("%c", 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

fptr = fopen ("emp.rec", "w"); /*open for writing*/


K

if (fptr == NULL)
{
k

printf("File does not exists\n");


return;
ee

}
printf("Enter the name\n");
bh

scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
A

printf("Enter the age\n");


scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);
}
A
bh
ee
k
K
um
ar
C
ha
kr
ab
or
ty

You might also like