c programs

161
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); /* compute s*/ s = (a + b + c) / 2; area = sqrt ( s * (s-a) * (s-b) * (s-c)); printf ("Area of a triangale = %d\n", area); } /*----------------------------- Output Enter the values of a,b and c 3 4 5 Area of a triangale = 6 ------------------------------*/ 2. /* Write a C program to find the area of a circl, given the adius*/ #include <stdio.h> #include <conio.h> #include <math.h> #define PI 3.142 void main() { float radius, area; clrscr();

Upload: abheek-kumar-chakraborty

Post on 23-Oct-2014

110 views

Category:

Documents


2 download

DESCRIPTION

Here's a collection of common C programs, which are often asked in various campuses. Hope this will help all. Best of Luck

TRANSCRIPT

Page 1: C Programs

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

/* compute s*/

s = (a + b + c) / 2;

area = sqrt ( s * (s-a) * (s-b) * (s-c));

printf ("Area of a triangale = %d\n", area);}

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

Enter the values of a,b and c345Area of a triangale = 6------------------------------*/

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

#include <stdio.h>#include <conio.h>#include <math.h>#define PI 3.142

void main(){

float radius, area;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

Page 2: C Programs

RUN1Enter the radius of a circle3.2Area of a circle = 32.17

RUN 2Enter the radius of a circle6Area of a circle = 113.11

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

3. /* Write a C program to find the simple interest , given principle, * * rate of interest and times*/

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

void main(){

float p, r, si; int t;

clrscr();

printf("Enter the values of p,r and t\n"); scanf ("%f %f %d", &p, &r, &t);

si = (p * r * t)/ 100.0; printf ("Amount = Rs. %5.2f\n", p); printf ("Rate = Rs. %5.2f%\n", r); printf ("Time = %d years\n", t); printf ("Simple interest = %5.2f\n", si);

}

/*-----------------------------OutputEnter the values of p,r and t200083Amount = Rs. 2000.00Rate = Rs. 8.00%Time = 3 yearsSimple interest = 480.00

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

4. /* Write a C program to check whether a given integer is odd or even*/

#include <stdio.h>

Page 3: C Programs

#include <conio.h>

void main(){ int ival, remainder;

clrscr();

printf("Enter an integer :"); scanf ("%d", &ival);

remainder = ival % 2;

if (remainder == 0)printf ("%d, is an even integer\n", ival);

elseprintf ("%d, is an odd integer\n", ival);

}/*-----------------------------Output

RUN1

Enter an integer :1313, is an odd integer

RUN2Enter an integer :2424, is an even integer

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

5. /* Write a C program to check whether a given integer * * number is positive or negative*/

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

void main(){

int number;clrscr();

printf("Enter a number\n");scanf ("%d", &number);

if (number > 0)printf ("%d, is a positive number\n", number);

elseprintf ("%d, is a negative number\n", number);

}/*-----------------------------Output

Page 4: C Programs

Enter a number-5-5, is a negative number

RUN2Enter a number8989, is a positive number------------------------------*/

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

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

void main(){

int a, b, c;

clrscr();

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

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

if ( a > b) {

if ( a > c){

printf ("A is the greatest among three\n");}else{ printf ("C is the greatest among three\n");}

} else if (b > c) {

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

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

}

/*-----------------------------OutputEnter the values of a,b and c23 32 45a = 23 b = 32 c = 45C is the greatest among three

Page 5: C Programs

RUN2Enter the values of a,b and c234678195a = 234 b = 678 c = 195B is the greatest among three

RUN3Enter the values of a,b and c30 20 10a = 30 b = 20 c = 10A is the greatest among three------------------------------*/

7. /* write a C program to find and output all the roots of a * * quadratic equation, for non-zero coefficients. In case * * of errors your program should report suitable error message*/

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

void main(){ float A, B, C, root1, root2; float realp, imagp, disc;

clrscr();

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

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

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

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

} else {

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

Page 6: C Programs

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

} }

} /* End of main() */

/*--------------------------- Output RUN 1 Enter the values of A, B and C 3 2 1 Imaginary Roots Root1 = -0.333333 +i 0.471405 Root2 = -0.333333 -i 0.471405

RUN 2 Enter the values of A, B and C 1 2 1 Roots are real and equal Root1 = -1.000000 Root2 = -1.000000

RUN 3 Enter the values of A, B and C 3 5 2 Roots are real and distinct Root1 = -0.666667 Root2

= -1.000000 ---------------------------------*/

8. /* Write a C program to simulate a simple calculator to perform * * arithmetic operations like addition, subtraction,multiplication *

* and division only on integers. Error message should be repoetrd * * if any attempt is made to divide by zero */

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

void main(){

Page 7: C Programs

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");scanf ("%c", &oper);

switch (oper) {

case '+': result = n1 + n2; break;

case '-': result = n1 - n2; break;

case '*': result = n1 * n2; break;

case '/': result = n1 / n2; break;

default : printf ("Error in operation\n"); break;

}

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

}/*-----------------------------OutputSimulation of Simple Calculator

Enter two numbers3 5Enter the operator [+,-,*,/]+

3.00 + 5.00= 8.00

RUN2Simulation of Simple Calculator

Enter two numbers12.758.45Enter the operator [+,-,*,/]-

12.75 - 8.45= 4.30

Page 8: C Programs

RUN3Simulation of Simple Calculator

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

12.00 * 12.00= 144.00

RUN4Simulation of Simple Calculator

Enter two numbers59Enter the operator [+,-,*,/]/

5.00 / 9.00= 0.56

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

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

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

void main(){ int i, N, sum = 0;

clrscr();

printf("Enter an integer number\n");scanf ("%d", &N);

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

sum = sum + i;}

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

/*----------------------------------------OutputRUN1

Enter an integer number10

Page 9: C Programs

Sum of first 10 natural numbers = 55

RUN2

Enter an integer number50Sum of first 50 natural numbers = 1275------------------------------------------*/

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

#include <stdio.h>

void main(){ int fib1=0, fib2=1, fib3, N, count=0;

printf("Enter the value of N\n"); scanf("%d", &N);

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

while( count < N) { fib3 = fib1 + fib2; count ++; printf("%d\n",fib3); fib1 = fib2; fib2 = fib3; }} /* End of main() */

/*--------------------------Enter the value of N10First 5 FIBONACCI numbers are ...0112358132134-------------------------------*/

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

Page 10: C Programs

#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) { numerator = num1; denominator = num2; } else { numerator = num2; denominator = num1; } remainder = num1 % num2; while(remainder !=0) {

numerator = denominator; denominator = remainder; remainder = numerator % denominator;

} gcd = denominator; lcm = num1 * num2 / gcd; printf("GCD of %d and %d = %d \n", num1,num2,gcd); printf("LCM of %d and %d = %d \n", num1,num2,lcm);} /* End of main() *//*------------------------OutputRUN 1Enter two numbers515GCD of 5 and 15 = 5LCM of 5 and 15 = 15------------------------------*/

12. /* Write a C program to find the sum of odd numbers and * * sum of even numbers from 1 to N. Output the computed * * sums on two different lines with suitable headings */

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

void main(){ int i, N, oddSum = 0, evenSum = 0;

clrscr();

Page 11: C Programs

printf("Enter the value of N\n");scanf ("%d", &N);

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

if (i % 2 == 0)evenSum = evenSum + i;

elseoddSum = oddSum + i;

}

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

}/*-----------------------------OutputRUN1

Enter the value of N10Sum of all odd numbers = 25Sum of all even numbers = 30

RUN2Enter the value of N50Sum of all odd numbers = 625Sum of all even numbers = 650

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

13. /* Write a C program to reverse a given integer number and check * * whether it is a palindrome. Output the given numbers with suitable*

* message */

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

void main(){ int num, temp, digit, rev = 0;

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;

Page 12: C Programs

}

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

}/*------------------------OutputRUN 1Enter an integer12321Given number is = 12321Its reverse is = 12321Number is a palindrome

RUN 2Enter an integer3456Given number is = 3456Its reverse is = 6543Number is not a palindrome-----------------------------------*/

14. /* Write a C program to find the value of sin(x) using the series * * up to the given accuracy (without using user defined function) *

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

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

void main(){ int n, x1; float acc, term, den, x, sinx=0, sinval;

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); sinval = sin(x);

printf("Enter the accuary for the result\n"); scanf("%f", &acc);

Page 13: C Programs

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

printf("Sum of the sine series = %f\n", sinx); printf("Using Library function sin(%d) = %f\n", x1,sin(x));

} /*End of main() */

/*------------------------------OutputEnter the value of x (in degrees)30Enter the accuary for the result0.000001Sum of the sine series = 0.500059Using Library function sin(30) = 0.500059

RUN 2Enter the value of x (in degrees)45Enter the accuary for the result0.0001Sum of the sine series = 0.707215Using Library function sin(45) = 0.707179---------------------------------------------*/

15. /* Write a C program to find the value of cos(x) using the series * * up to the given accuracy (without using user defined function) * * Also print cos(x) using library function. */

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

void main(){ int n, x1; float acc, term, den, x, cosx=0, cosval;

clrscr();

printf("Enter the value of x (in degrees)\n"); scanf("%f",&x);

Page 14: C Programs

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;

do {

den = 2*n*(2*n-1); term = -term * x * x / den; cosx = cosx + term; n = n + 1;

} while(acc <= fabs(cosval - cosx));

printf("Sum of the cosine series = %f\n", cosx); printf("Using Library function cos(%d) = %f\n", x1,cos(x));} /*End of main() *//*------------------------------OutputEnter the value of x (in degrees)30Enter the accuary for the result0.000001Sum of the cosine series = 0.865991Using Library function cos(30) = 0.865991

RUN 2Enter the value of x (in degrees)45Enter the accuary for the result0.0001Sum of the cosine series = 0.707031Using Library function cos(45) = 0.707035---------------------------------------------*/

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;

Page 15: C Programs

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;

for ( j=2; j<= num/2; j++){

if( ( num % j ) == 0){

flag = 1;break;

}}

if(flag == 0) printf("%d is a prime number\n",num);

else printf("%d is not a prime number\n", num);

}/*------------------------OutputRUN 1Enter a number3434 is not a prime number

RUN 2Enter a number2929 is a prime number-----------------------------*/

17. /* Write a C program to generate and print prime numbers in a given * * range. Also print the number of prime numbers */

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

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

clrscr();

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

Page 16: C Programs

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++; } for (i=M; i<=N; i=i+2) { flag = 0;

for (j=2; j<=i/2; j++) {

if( (i%j) == 0){ flag = 1; break;}

} if(flag == 0) { printf("%d\n",i); count++; } } printf("Number of primes between %d and %d = %d\n",temp,N,count);}/*---------------------------------OutputEnter the value of M and N15 45Prime numbers are1719232931374143Number 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 */

Page 17: C Programs

#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*/ printf ("Integers divisible by 5 are\n");

for (i = N1; i < N2; i++) { if (i%5 == 0) {

printf("%3d,", i);count++;sum = sum + i;

} }

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

} /* End of main()*//*-----------------------------OutputEnter the value of N1 and N2227Integers divisible by 5 are 5, 10, 15, 20, 25,Number of integers divisible by 5 between 2 and 27 = 5Sum of all integers that are divisible by 5 = 75------------------------------------------------------*/

19. /* Write a C program to read N integers (zero, +ve and -ve) * * 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(){

Page 18: C Programs

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++) { scanf("%d",&num);

fflush(stdin);

if(num < 0){ negsum = negsum + num;}else if(num > 0){ posum = posum + num;}else if( num == 0){ ;}total = total + num ;

}

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

} /*End of main()*//*-------------------------------------OutputEnter the value of N5Enter 5 numbers (-ve, +ve and zero)5-30-76Input array elements +5 -3 +0 -7 +6

Sum of all negative numbers = -10

Page 19: C Programs

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>

void main(){ int array[10]; int i, N, keynum, found=0;

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]); } printf("Enter the element to be searched\n"); scanf("%d", &keynum);

/* Linear search begins */ for ( i=0; i < N ; i++) { if( keynum == array[i] ) {

found = 1; break;

} } if ( found == 1) printf("SUCCESSFUL SEARCH\n"); else printf("Search is FAILED\n");

} /* End of main *//*------------------------------------OutputRUN 1Enter the value of N

Page 20: C Programs

5Enter the elements one by one2312564389Input array is2312564389Enter the element to be searched56SUCCESSFUL SEARCH

RUN 2Enter the value of N3Enter the elements one by one456213879Input array is456213879Entee the element to be searched1000Search is FAILED--------------------------------------*/

21. /* Write a C program to sort N numbers in ascending order * * using Bubble sort and print both the given and the sorted *

* array with suitable headings */

#include <stdio.h>#include <conio.h>#define MAXSIZE 10

void main(){ 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]);

Page 21: C Programs

} printf("Input array is\n"); for(i=0; i<N ; i++) {

printf("%d\n",array[i]); } /* Bubble sorting begins */ for(i=0; i< N ; i++) { for(j=0; j< (N-i-1) ; j++) {

if(array[j] > array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; }

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

/*----------------------------------OutputEnter the value of N5Enter the elements one by one390234111876345Input array is390234111876345Sorted array is...111234345390876---------------------------*/

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>

Page 22: C Programs

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

printf("Enter the elements one by one\n"); for(i=0; i<N ; i++) {

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

printf("%d\n",array[i]); } /* Bubble sorting begins */ for(i=0; i< N ; i++) { for(j=0; j< (N-i-1) ; j++) {

if(array[j] > array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; }

} } printf("Sorted array is...\n"); for(i=0; i<N ; i++) { printf("%d\n",array[i]); }

printf("Enter the element to be searched\n"); scanf("%d", &keynum);

/* Binary searching begins */

low=1; high=N;

do { mid= (low + high) / 2; if ( keynum < array[mid] )

high = mid - 1;

Page 23: C Programs

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

} /* End of main*//*----------------------------------OutputEnter the value of N4Enter the elements one by one3142Input array elements3142Sorted array is...1234Enter the element to be searched4SUCCESSFUL SEARCH---------------------------*/

23. /* Write a C program to input real numbers and find the * * mean, variance and standard deviation */

#include <stdio.h>#include <conio.h>#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");

Page 24: C Programs

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]; } avrg = sum /(float) n;

/* Compute varaience and standard deviation */

for(i=0; i<n; i++) { sum1 = sum1 + pow((x[i] - avrg),2); } var = sum1 / (float) n; SD = sqrt(var);

printf("Average of all elements = %.2f\n", avrg); printf("Varience of all elements = %.2f\n", var); printf("Standard deviation = %.2f\n", SD);} /*End of main()*//*--------------------------OutputEnter the value of N6Enter 6 real numbers123410504233Average of all elements = 29.66Varience of all elements = 213.89Standard deviation = 14.62-------------------------------------*/

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

Page 25: C Programs

{ 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++) { printf("%5d", a[i]); }

printf("\n");

l1 = a[0]; /*assume first element of array is the first largest*/ l2 = a[1]; /*assume first element of array is the second largest*/

if (l1 < l2) { temp = l1; l1 = l2; l2 = temp; }

for (i=2;i<4;i++) { if (a[i] >= l1) {

l2 = l1; l1 = a[i];

} else if(a[i] > l2) {

l2= a[i]; } }

printf("\n%d is the first largest\n", l1); printf("%d is the second largest\n", l2); printf("\nAverage of %d and %d = %d\n", l1,l2, (l1+l2)/2);

}/*-----------------------------------OutputRUN 1Enter 4 integer numbers45332110

Page 26: C Programs

Input interger are 45 33 21 10

45 is the first largest33 is the second largest

Average of 45 and 33 = 39

RUN 2Enter 4 integer numbers12905467Input interger are 12 90 54 67

90 is the first largest67 is the second largest

Average of 90 and 67 = 78

RUN 3Enter 4 integer numbers100200300400Input interger are 100 200 300 400

400 is the first largest300 is the second largest

Average of 400 and 300 = 350

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

25. /* Write a C program to evaluate the given polynomial * * P(x)=AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0, by * * reading its coefficents into an array. [Hint:Rewrite * * the polynomial as * * P(x) = a0 + x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan)))) * * and evalate the function starting from the inner loop]*/

#include <stdio.h>#include <conio.h>#include <stdlib.h>#define MAXSIZE 10

voidmain(){ int a[MAXSIZE]; int i, N,power; float x, polySum;

Page 27: C Programs

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++) { scanf("%d",&a[i]); }

polySum = a[0];

for (i=1;i<= N;i++) { polySum = polySum * x + a[i]; }

power = N; /*power--;*/

printf("Given polynomial is:\n"); for (i=0;i<= N;i++) { if (power < 0) {

break; }

/* printing proper polynomial function*/ if (a[i] > 0)

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

printf(" - "); else

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

}

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

/*-----------------------------------------------------OutputRUN 1Enter the order of the polynomial2

Page 28: C Programs

Enter the value of x2Enter 3 coefficients326Given polynomial is: + 3x^2 + 2x^1 + 6x^0Sum of the polynomial = 22.00

RUN 2Enter the order of the polynomial4Enter the value of x1Enter 5 coefficients3-568-9Given polynomial is: + 3x^4 - 5x^3 + 6x^2 + 8x^1 - 9x^0Sum of the polynomial = 3.00-----------------------------------------------------*/

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 *

* trace of the resultant matrix. Output the given matrix, * * their sum or Differences and the trace. */

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

void main(){ int A[10][10], B[10][10], sumat[10][10], diffmat[10][10]; int i, j, M,N, option;

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

clrscr();

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

}

Page 29: C Programs

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

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

scanf("%d",&B[i][j]); }

}

printf("MATRIX B is\n"); for(i=0; i<M; i++) {

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

}

printf("Enter your option: 1 for Addition and 2 for Subtraction\n"); scanf("%d",&option);

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

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

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

}

printf("Sum matrix is\n"); for(i=0; i<M; i++) {

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

printf("%3d",sumat[i][j]) ; } printf("\n");

}

trace (sumat, M, N); break;

Page 30: C Programs

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

printf("%3d",diffmat[i][j]) ; } printf("\n");

}

trace (diffmat, M, N); break;}

} /* End of main() */

/*Function to find the trace of a given matrix and print it*/

void trace (int arr[][10], int M, int N){ int i, j, trace = 0; for(i=0; i<M; i++) { for(j=0; j<N; j++) {

if (i==j) { trace = trace + arr[i][j]; }

} } printf ("Trace of the resultant matrix is = %d\n", trace);

}/*-----------------------------------Enter the order of the matrice A and B2 2Enter the elements of matrix A1 12 2MATRIX A is 1 1 2 2Enter the elements of matrix B3 34 4MATRIX B is

Page 31: C Programs

3 3 4 4Enter your option: 1 for Addition and 2 for Subtraction1Sum matrix is 4 4 6 6Trace of the resultant matrix is = 10---------------------------------------------*/

27. /* Write a C program to read A (MxN), find the transpose * * of a given matrix and output both the input matrix and*

* the transposed matrix. */

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

void main(){ int i,j,M,N; int A[10][10], B[10][10];

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

clrscr();

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

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

/* Finding Transpose of matrix*/ for(i=0;i<M;i++) { for(j=0;j<N;j++) {

B[i][j] = A[j][i]; }

Page 32: C Programs

}

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()*//*---------------------------------------OutputEnter the order of matrix A3 3Enter the elements of matrix123456789MatrixxA is 1 2 3 4 5 6 7 8 9Its Transpose is 1 4 7 2 5 8 3 6 9-----------------------------*/

28. /* Write a C program to read a string and check whether it is * * a palindrome or not (without using library functions). Output * * the given string along with suitable message */

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

void main() {

char string[25], revString[25]={'\0'};int i,length = 0, flag = 0;

clrscr();

fflush(stdin);

printf("Enter a string\n");

Page 33: C Programs

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 then the input string is palindrome. Otherwise it is not a palindrome */

for (i=0; i < length ; i++){ if (revString[i] == string[i])

flag = 1; else

flag = 0;}

if (flag == 1) printf ("%s is a palindrome\n", string);else printf("%s is not a palindrome\n", string);

} /*End of main()*/

/*----------------------------------------------------OutputRUN 1Enter a stringmadammadam is a palindrome

RUN 2Enter a stringMadamMadam is not a palindrome

RUN 3Enter a stringgoodgood 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>

Page 34: C Programs

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

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

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

printf("Concatenated string = %s\n", string1);}/*---------------------------------------OutputEnter the first string :CD-Enter the second string:ROMFirst string = CD-Second string = ROMConcatenated 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>

Page 35: C Programs

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

sentence[i]='\0';

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

printf("The given sentence is : %s",sentence);

printf("\nCase changed sentence is: "); for(i=0; i < count; i++) { ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]); putchar(ch); }

} /*End of main()*//*------------------------------OutputEnter a sentenceMera Bharat MahanThe given sentence is : Mera Bhaaat MahanCase changed sentence is: mERA bHARAT mAHAN------------------------------------------------*/

31. /* Write a C program read a sentence and count the number of * * number of vowels and consonants in the given sentence. *

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

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

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

Page 36: C Programs

{ 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] ==' ') {

special = special + 1; } }

consonants = consonants - special; printf("No. of vowels in %s = %d\n", sentence, vowels); printf("No. of consonants in %s = %d\n", sentence, consonants);

}/*----------------------------------------OutputEnter a sentenceGood MorningNo. of vowels in Good Morning = 4No. of consonants in Good Morning = 7-----------------------------------------*/

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* * with suitable heading */

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

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

Page 37: C Programs

}

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

}

printf("\n----------------------------------------\n"); printf("Input Names\tSorted names\n"); printf("------------------------------------------\n"); for(i=0; i< N ; i++) {

printf("%s\t\t%s\n",Tname[i], name[i]); } printf("------------------------------------------\n");

} /* End of main() */

/*--------------------------------OutputEnter the value of N3Enter 3 namesMonicaLaxmiAnand

----------------------------------------Input Names Sorted names----------------------------------------Monica AnandLaxmi LaxmiAnand Monica-------------------------------------------------------------------------------- */

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

Page 38: C Programs

#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); void printMatrix(int arr[][MAXCOLS], int M, int N); void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],

int M, int N);

clrscr();

printf("Enter the value of M and N\n"); scanf("%d %d",&M, &N); printf ("Enter matrix A\n"); readMatrix(A,M,N); printf("Matrix A\n"); printMatrix(A,M,N);

printf ("Enter matrix B\n"); readMatrix(B,M,N); printf("Matrix B\n"); printMatrix(B,M,N);

productMatrix(A,B,C, M,N);

printf ("The product matrix is\n"); printMatrix(C,M,N);}

/*Input matrix A*/void readMatrix(int arr[][MAXCOLS], int M, int N){ int i, j; for(i=0; i< M ; i++) {

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

Page 39: C Programs

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

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

}}/*---------------------------------------------OutputEnter the value of M and N3 3Enter matrix A1 1 12 2 23 3 3Matrix A 1 1 1 2 2 2 3 3 3Enter matrix B1 2 34 5 67 8 9Matrix B 1 2 3 4 5 6 7 8 9The 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 */

Page 40: C Programs

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

printf ("Before Swapping:M = %5.2f\tN = %5.2f\n", M,N); swap(&M, &N);

printf ("After Swapping:M = %5.2f\tN = %5.2f\n", M,N);

} /* End of main() */

/* Function swap - to interchanges teh contents of two items*/void swap(float *ptr1, float *ptr2 ){ float temp; temp=*ptr1; *ptr1=*ptr2; *ptr2=temp;

} /* End of Function */

/* ----------------------------------------OutputEnter the values of M and N32 29Before Swapping:M = 32.00 N = 29.00After Swapping:M = 29.00 N = 32.00------------------------------------------*/

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

Page 41: C Programs

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

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

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

} /* End of main() */

/*----------------------------OutputEnter the size of array A4Enter Elements of First List10203040Sum of all elements in array = 100-------------------------------------*/

36. /* Write a C program to convert the given binary number into decimal */

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

Page 42: C Programs

base = base * 2; }

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

} /* End of main() */

/*---------------------------------------------OutputEnter a binary number(1s and 0s)10101The Binary number is = 10101Its decimal equivalent is =21----------------------------------------------*/

37. /* Program to accept N integer number and store them in an array AR. * The odd elements in the AR are copied into OAR and other elements * are copied into EAR. Display the contents of OAR and EAR */

#include <stdio.h> void main() { long int ARR[10], OAR[10], EAR[10]; int i,j=0,k=0,n;

printf("Enter the size of array AR\n"); scanf("%d",&n);

printf("Enter the elements of the array\n"); for(i=0;i<n;i++) {

scanf("%ld",&ARR[i]);fflush(stdin);

} /*Copy odd and even elemets into their respective arrays*/ for(i=0;i<n;i++) {

if (ARR[i]%2 == 0){ EAR[j] = ARR[i]; j++;}else{ OAR[k] = ARR[i]; k++;}

}

printf("The elements of OAR are\n"); for(i=0;i<j;i++) {

Page 43: C Programs

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 Enter the elements of the array 12 345 678 899 900 111 The elements of OAR are 345 899 111 The elements of EAR are 12 678 900 ---------------------------------------*/

38. /* Write a C program to generate Fibonacci sequence * * Fibonacci sequence is 0 1 1 2 3 5 8 13 21 ... */

#include <stdio.h>

void main(){ int fib1=0, fib2=1, fib3, limit, count=0;

printf("Enter the limit to generate the fibonacci sequence\n"); scanf("%d", &limit);

printf("Fibonacci sequence is ...\n"); 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;

}

Page 44: C Programs

} /* End of main() *//*-----------------------------------------------------------Enter the limit to generate the fibonacci sequence7Fibonacci sequence is ...0112358-----------------------------------------------------------*/

39. /* Write a C program to insert a particular element in a specified position * * in a given array */

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

void main(){

int x[10]; int i, j, n, m, temp, key, pos;

clrscr();

printf("Enter how many elements\n");scanf("%d", &n);

printf("Enter the elements\n");for(i=0; i<n; i++){

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

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

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

}}

Page 45: C Programs

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

pos = i;break;

} }

m = n - pos + 1 ;

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

x[n-i+2] = x[n-i+1] ; }

x[pos] = key;

printf("Final list is\n"); for(i=0; i<n+1; i++) {

printf("%d\n", x[i]); }} /* End of main() */

/*-------------------------------------OutputEnter how many elements5Enter the elements214678329Input array elements are214678329Sorted list is21429

Page 46: C Programs

6783Enter the element to be inserted34Final list is21429346783-------------------------------------------------*/

40. /* Write a C program to accept an integer and reverse it */

#include <stdio.h>

void main(){ long num, rev = 0, temp, digit;

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

temp = num;

while(num > 0) {

digit = num % 10; rev = rev * 10 + digit; num /= 10; }

printf("Given number = %ld\n", temp); printf("Its reverse is = %ld\n", rev);}

/* ---------------------------OutputEnter the number123456Given number = 123456Its 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>

Page 47: C Programs

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

ch = getch();pasword[i] = ch;ch = '*' ;printf("%c",ch);

}

pasword[i] = '\0';

/*If you want to know what you have entered as password, you can print it*/ printf("\nYour password is :");

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

printf("%c",pasword[i]); }

}

/*-----------------------------------------------OutputEnter User name: LathaEnter the password <any 8 characters>: ********Your password is :Wipro123-----------------------------------------------*/

42. /* Write a C program to accept a string and a substring and * check if the substring is present in the given strig */

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

void main(){

char str[80],search[10];int count1=0,count2=0,i,j,flag;

clrscr();

puts("Enter a string:");gets(str);

puts("Enter search substring:");

Page 48: C Programs

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

flag=1;if (str[j]!=search[j-i]){

flag=0; break;}

}if (flag==1)

break;}if (flag==1)

puts("SEARCH SUCCESSFUL!");else

puts("SEARCH UNSUCCESSFUL!");getch();

}/*------------------------------OutputEnter a string:Hello how are you?Enter search substring:howSEARCH SUCCESSFUL!------------------------------*/

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

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

Page 49: C Programs

volume = pow (side, 3);

printf("Surface area = %6.2f and Volume = %6.2f\n", surfArea, volume); }

/*------------------------------------------OutputEnter the llngth of a side4Surface area = 96.00 and Volume = 64.00

RUN2Enter the length of a side12.5Surface area = 937.50 and Volume = 1953.12------------------------------------------*/

44. /* Write a C program to accept a decimal number and convert it to binary * * and count the number of 1's in the binary number */

#include <stdio.h>

void main(){ long num, dnum, rem, base = 1, bin = 0, no_of_1s = 0;

printf("Enter a decimal integer\n"); scanf("%ld", &num);

dnum = num;

while( num > 0 ) {

rem = num % 2; if ( rem == 1 ) /*To count no.of 1s*/ {

no_of_1s++; } bin = bin + rem * base; num = num / 2 ; base = base * 10;

} printf("Input number is = %d\n", dnum); 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() */

/*--------------------------------------------------OutputEnter a decimal integer75Input number is = 75Its Binary equivalent is = 1001011No.of 1's in the binary number is = 4

Page 50: C Programs

RUN2Enter a decimal integer128Input number is = 128Its Binary equivalent is = 10000000No.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 */

#include <stdio.h>

void main() {

int year;

printf("Enter a year\n");scanf("%d",&year);

if ( (year % 4) == 0) printf("%d is a leap year",year);else printf("%d is not a leap year\n",year);

}

/*------------------------------------------OutputEnter a year20002000 is a leap year

RUN2Enter a year20072007 is not a leap year------------------------------------------*/

46. /* Write a c program to swap the contents of two numbers * * using bitwise XOR operation. Don't use either the * * temporary variable or arithmetic operators */

#include <stdio.h>

void main() {

long i,k;

printf("Enter two integers\n");scanf("%ld %ld",&i,&k);

Page 51: C Programs

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

}

/*------------------------------------------OutputEnter two integers23 34

Before swapping i= 23 and k = 34After swapping i= 34 and k = 23------------------------------------------*/

47. /* Write a c program to convert given number of days to a measure of time * given in years, weeks and days. For example 375 days is equal to 1 year * 1 week and 3 days (ignore leap year) */

#include <stdio.h> #define DAYSINWEEK 7

void main() { int ndays, year, week, days;

printf("Enter the number of days\n"); scanf("%d",&ndays);

year = ndays/365; week = (ndays % 365)/DAYSINWEEK; days = (ndays%365) % DAYSINWEEK;

printf ("%d is equivalent to %d years, %d weeks and %d days\n", ndays, year, week, days);

}

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

Page 52: C Programs

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

clrscr();puts("Enter a string:");gets(str1);

puts("Enter another string:");gets(str2);/*Count the number of characters in str1*/while (str1[count1]!='\0')

count1++;/*Count the number of characters in str2*/while (str2[count2]!='\0')

count2++;i=0;

/*The string comparison starts with thh first character in each string andcontinues with subsequent characters until the corresponding charactersdiffer or until the end of the strings is reached.*/

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

if (str1[i] == str2[i]){

i++;continue;

}if (str1[i]<str2[i]){

flag = -1;break;

}if (str1[i] > str2[i]){

flag = 1;break;

}}

if (flag==0) printf("Both strings are equal\n");if (flag==1) printf("String1 is greater than string2\n", str1, str2);

Page 53: C Programs

if (flag == -1) printf("String1 is less than string2\n", str1, str2);getch();

}/*----------------------------------------OutputEnter a string:happyEnter another string:HAPPYString1 is greater than string2

RUN2Enter a string:HelloEnter another string:HelloBoth strings are equal

RUN3Enter a string:goldEnter another string:silverString1 is less than string2----------------------------------------*/

49. /* Program to accept two strings and concatenate them * * i.e.The second string is appended to the end of the first string */

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

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

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

Page 54: C Programs

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

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

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

/*---------------------------------------OutputEnter the first string :CD-Enter the second string:ROMFirst string = CD-Second string = ROMConcatenated string = CD-ROM----------------------------------------*/

50. /* Write a c program to find the length of a string * * without using the built-in function */

#include <stdio.h>

void main(){

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

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 */}printf("The length of a string is the number of characters in it\n");printf("So, the length of %s =%d\n", string, length);

}

/*----------------------------------------------------OutputEnter a stringhelloThe length of a string is the number of characters in itSo, the length of hello = 5

Page 55: C Programs

RUN2Enter a stringE-Commerce is hot nowThe length of a string is the number of characters in itSo, 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> #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 */}printf("The length of the string \'%s\' = %d\n", string, length);

for (i=length-1; i >= 0 ; i--){ revString[length-i-1] = string[i];}/*Compare the input string and its reverse. If both are equal then the input string is palindrome. Otherwise it is not a palindrome */

for (i=0; i < length ; i++){ if (revString[i] == string[i])

flag = 1; else

flag = 0;

}

if (flag == 1) printf ("%s is a palindrome\n", string);else printf("%s is not a palindrome\n", string);

}

Page 56: C Programs

/*----------------------------------------------------OutputEnter a stringmadamThe length of the string 'madam' = 5madam is a palindrome

RUN2Enter a stringgoodThe length of the string 'good' = 4good is not a palindrome----------------------------------------------------------*/

52. /* Write a C program to accept a string and find the number of times the word 'the'

* appears in it*/

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

void main(){

int count=0,i,times=0,t,h,e,space;char str[100];

clrscr();puts("Enter a string:");gets(str);

/*Traverse the string to count the number of characters*/while (str[count]!='\0'){

count++;}/*Finding the frequency of the word 'the'*/for(i=0;i<=count-3;i++){

t=(str[i]=='t'||str[i]=='T');h=(str[i+1]=='h'||str[i+1]=='H');e=(str[i+2]=='e'||str[i+2]=='E');space=(str[i+3]==' '||str[i+3]=='\0');if ((t&&h&&e&&space)==1) times++;

}printf("Frequency of the word \'the\' is %d\n",times);getch();

}/*-----------------------------------------------------OutputEnter a string:The Teacher's day is the birth day of Dr.S.RadhakrishnanFrequency of the word 'the' is 2---------------------------------------------------------*/

Page 57: C Programs

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"); scanf("%ld %ld",&x,&n);

xpown = power (x,n);

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

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

long int power(int x, int n) { if (n==1) return(x); else if ( n%2 == 0) return (pow(power(x,n/2),2)); /*if n is even*/ else return (x*power(x, n-1)); /* if n is odd*/}/*-------------------------------------OutputEnter the values of X and N2 5X to the power N = 32

RUN2Enter the values offX and N4 4X to the power N ==256

RUN3Enter the values of X and N5 2X to the power N = 25

RUN4Enter the values of X and N10 5X to the power N = 100000-----------------------------------------*/

54. /* Write a C program to accept two matrices and * * find the sum and difference of the matrices */

Page 58: C Programs

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

void printmatB();void sum();

void diff();

printf("Enter the order of the matrix A\n"); scanf("%d %d", &R1, &C1);

printf("Enter the order of the matrix B\n");scanf("%d %d", &R2,&C2);

if( R1 != R2 && C1 != C2){ printf("Addition and subtraction are possible\n"); exit(1);}else{

printf("Enter the elements of matrix A\n");readmatA();printf("MATRIX A is\n");printmatA();

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

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

sum(); diff();

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

Page 59: C Programs

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

}}

/* Function to print a matrix A */void printmatA(){ for(i=0; i<R1; i++) {

for(j=0; j<C1; j++){

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

}}

/* Function to print a matrix B */void printmatB(){ for(i=0; i<R2; i++) {

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

printf("%3d",B[i][j]); } printf("\n");

}}/*Function to find the sum of elements of matrix A and Matrix B*/void sum(){ for(i=0; i<R1; i++) {

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

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

} printf("Sum matrix is\n"); for(i=0; i<R1; i++) {

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

Page 60: C Programs

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

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

diffmat[i][j] = A[i][j] - B[i][j]; }

} printf("Difference matrix is\n"); for(i=0; i<R1; i++) { for(j=0; j<C2; j++) {

printf("%3d",diffmat[i][j]); } printf("\n"); }

return;}

/*---------------------------------------------------OutputEnter the order of the matrix A2 2Enter the order of the matrix B2 2Enter the elements of matrix A1234MATRIX A is 1 2 3 4Enter the elements of matrix B2468MATRIX B is 2 4 6 8Sum matrix is 3 6

Page 61: C Programs

9 12Difference 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(){ int A[10][10], B[10][10]; int i, j, R1, C1, R2, C2, flag =1;

printf("Enter the order of the matrix A\n"); scanf("%d %d", &R1, &C1);

printf("Enter the order of the matrix B\n"); scanf("%d %d", &R2,&C2);

printf("Enter the elements of matrix A\n"); for(i=0; i<R1; i++) { for(j=0; j<C1; j++) {

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

printf("Enter the elements of matrix B\n"); for(i=0; i<R2; i++) { for(j=0; j<C2; j++) {

scanf("%d",&B[i][j]); } }

printf("MATRIX A is\n"); for(i=0; i<R1; i++) { for(j=0; j<C1; j++) {

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

printf("MATRIX B is\n"); for(i=0; i<R2; i++) { for(j=0; j<C2; j++)

Page 62: C Programs

{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++) { for(j=0; j<C2; j++) {

if(A[i][j] != B[i][j]){

flag = 0; break;

} } } } else { printf(" Cannot be compared\n"); exit(1); }

if(flag == 1 )printf("Two matrices are equal\n");

elseprintf("But,two matrices are not equal\n");

}

/*------------------------------------------------------OutputEnter the order of the matrix A2 2Enter the order of the matrix B2 2Enter the elements of matrix A1 23 4Enter the elements of matrix B1 23 4MATRIX A is 1 2 3 4MATRIX B is 1 2 3 4Matrices can be comparedTwo matrices are equal

Page 63: C Programs

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

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

printf("Enter the elements of matrix A\n"); for(i=0; i<R; i++) { for(j=0; j<C; j++) {

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

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

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

for(i=0; i<R; i++) { for(j=0; j<C; j++) {

if(A[i][j] != 1 && A[j][i] !=0) { flag = 0; break; }}

}

if(flag == 1 )printf("It is identity matrix\n");

elseprintf("It is not a identity matrix\n");

}

/*------------------------------------------OutputRun 1

Page 64: C Programs

Enter the order of the matrix A2 2Enter the elements of matrix A2 21 2MATRIX A is 2 2 1 2It is not a identity matrix

Run 2Enter the order of the matrix A2 2Enter the elements of matrix A1 00 1MATRIX A is 1 0 0 1It is identity matrix------------------------------------------*/

57. /* Write a C program to accept a matrix of given order and * interchnge any two rows and columns in the original matrix*/

#include <stdio.h>

void main(){

static int m1[10][10],m2[10][10];int i,j,m,n,a,b,c, p, q, r;

printf ("Enter the order of the matrix\n");scanf ("%d %d",&m,&n);

printf ("Enter the co-efficents of the matrix\n");for (i=0; i<m;++i)

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

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

}}

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;

}

Page 65: C Programs

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

r = m2[i][p-1]; /* first column index is 0 */m2[i][p-1] = m2[i][q-1];m2[i][q-1] = r;

}

printf ("The matix after interchnging the two rows(in the original matrix)\n");for (i=0; i<m; ++i)

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

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

}printf ("\n");

}

printf("The matix after interchnging the two columns(in the original matrix)\n");for (i=0;i<m;++i)

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

printf (" %d", m2[i][j]);printf ("\n");

}}

/*-----------------------------------------------------------------------Enter the order of the matrix3 3Enter the co-efficents of the matrix1 2 45 7 93 0 6Enter the numbers of two rows to be exchnged1 2Enter the numbers of two columns to be exchnged2 3The given matrix is 1 2 4 5 7 9 3 0 6The matix after interchnging the two rows (in the original matrix) 5 7 9 1 2 4

Page 66: C Programs

3 0 6The matix after interchnging the two columns(in the original matrix) 1 4 2 5 9 7 3 6 0-------------------------------------------------------------------------*/

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>#include <conio.h>

void main(){

struct date{

int day;int month;int year;

};

struct details{

char name[20];int price;int code;int qty;struct date mfg;

};

struct details item[50];int n,i;

clrscr();

printf("Enter number of items:");scanf("%d",&n);fflush(stdin);

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

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

Page 67: C Programs

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

printf("%d %-15s %-d %-5d %-5d %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");getch();

}

/*------------------------------------------------------Enter number of items:5Item name:Tea PowderItem code:123Quantity:23price:40Manufacturing date(dd-mm-yyyy):12-03-2007Item name:Milk PowderItem code:345Quantity:20price:80Manufacturing date(dd-mm-yyyy):30-03-2007Item name:Soap PowderItem code:510Quantity:10price:30Manufacturing date(dd-mm-yyyy):01-04-2007Item name:Washing SoapItem code:890Quantity:25price:12Manufacturing date(dd-mm-yyyy):10-03-2007Item name:ShampoItem code:777Quantity:8price:50Manufacturing date(dd-mm-yyyy):17-05-2007

***** INVENTORY *****------------------------------------------------------------------------S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE------------------------------------------------------------------------1 Tea Powder 123 23 40 12/3/20072 Milk Powder 345 20 80 30/3/20073 Soap Powder 510 10 30 1/4/2007

Page 68: C Programs

4 Washing Soap 890 25 12 10/3/20075 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(){ int num, bnum, dec = 0, base = 1, rem ;

printf("Enter the binary number(1s and 0s)\n"); scanf("%d", &num); /*Enter maximum five digits or use long int*/

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

/*---------------------------------------------------OutpputEnter the binary number(1s and 0s)1010The Binary number is = 1010Its decimal equivalent is =10----------------------------------------------------*/

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

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

Page 69: C Programs

printf("%ld x 4 = %ld\n", tempnum,number);}

/*------------------------------OutputEnter an integer1515 x 4 = 60

RUN2Enter an integer262262 x 4 = 1048---------------------------------*/

58. /* Writ a C programme to cyclically permute the elements of an array A. *i.e. the content of A1 become that of A2.And A2 contains that of A3 *& so on as An contains A1 */

#include <stdio.h>

void main (){

int i,n,number[30];printf("Enter the value of the n = ");scanf ("%d", &n);printf ("Enter the numbers\n");for (i=0; i<n; ++i){

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

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

number[i] = number[i+1];}printf ("Cyclically permted numbers are given below \n");for (i=0; i<n; ++i)printf ("%d\n", number[i]);

}/*-------------------------------------OutputEnter the value of the n = 5Enter the numbers1030204518Cyclically permted numbers are given below3020

Page 70: C Programs

451810---------------------------------------------------*/

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

#include <stdio.h>

void main (){ int i,j,a,n,number[30];

printf ("Enter the value of N\n"); scanf ("%d", &n);

printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);

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

{if (number[i] > number[j]){

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

} }

}

printf ("The numbers arrenged in ascending order are given below\n"); for (i=0; i<n; ++i) printf ("%d\n",number[i]);} /* End of main() */

/*------------------------------------------------------------------OutputEnter the value of N5Enter the numbers8020671045The numbers arrenged in ascending order are given below10204567

Page 71: C Programs

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;

printf ("Enter the value of N\n"); scanf ("%d", &n);

printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);

/* sorting begins ...*/ for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j)

{if (number[i] < number[j]){

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

} }

}

printf ("The numbers arranged in descending order are given below\n"); for (i=0; i<n; ++i) {

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

} /* End of main() */

/*------------------------------------------------ Output Enter the value of N6Enter the numbers10356710042688The numbers arranged in descending order are given below688

Page 72: C Programs

10067423510

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

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

#include <stdio.h>

void main (){ int number[30]; int i,j,a,n,counter,ave;

printf ("Enter the value of N\n"); scanf ("%d", &n);

printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);

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

for (j=i+1; j<n; ++j){

if (number[i] < number[j]){

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

}}

}

printf ("The numbers arranged in descending order are given below\n"); for (i=0; i<n; ++i) {

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

Page 73: C Programs

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

/*-------------------------------------------------------OutputEnter the value of N6Enter the numbers308010407090The numbers arranged in descending order are given below908070403010The 2nd largest number is = 80The 2nd smallest number is = 30The average of 80 and 30 is = 55 is not in the array

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

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

#include <stdio.h>

void main(){ int vectx[10];

int i, n, pos, element, found = 0;

printf("Enter how many elements\n"); scanf("%d", &n);

printf("Enter the elements\n"); for(i=0; i<n; i++)

{ scanf("%d", &vectx[i]);

}

Page 74: C Programs

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

found = 1;pos = i;break;

}}

if (found == 1){

for(i=pos; i< n-1; i++) {

vectx[i] = vectx[i+1]; }

printf("The resultant vector is \n"); for(i=0; i<n-1; i++)

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

} } else

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

} /* End of main() */

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

Run 1Enter how many elements5Enter the elements3010502040Input array elements are30105020

Page 75: C Programs

40Enter the element to be deleted35Element 35 is not found in the vector

Run 2Enter how many elements4Enter the elements23105581Input array elements are23105581Enter the element to be deleted55The resultant vector is231081

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

62. /* Write a C programme (1-D Array) store some elements in it.Accept key& split from that point. Add the first half to the end of second half*/

#include <stdio.h>

void main (){ int number[30]; int i,n,a,j;

printf ("Enter the value of n\n"); scanf ("%d",&n);

printf ("enter the numbers\n"); for (i=0; i<n; ++i) scanf ("%d", &number[i]);

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

Page 76: C Programs

}

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

/*-------------------------------------------------OutputEnter the value of n5enter the numbers3010405060Enter the position of the element to split the array2The resultant array is4050603010----------------------------------------------------------*/

63. /* Write a C program to find the frequency of odd numbers * * and even numbers in the input of a matrix */

#include <stdio.h>

void main (){ static int ma[10][10]; int i,j,m,n,even=0,odd=0;

printf ("Enter the order ofthe matrix \n"); scanf ("%d %d",&m,&n);

printf ("Enter the coefficients if matrix \n"); 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;}

Page 77: C Programs

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

} /* End of main() */

/*-----------------------------------------------------OutputEnter the order ofthe matrix3 3Enter the coefficients if matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9

The frequency of occurance of odd number = 5The frequency of occurance of even number = 4

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

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 *with that of the secondary diagonal elements */

#include <stdio.h>

void main (){

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)

Page 78: C Programs

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

}

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

a = ma[i][i];ma[i][i] = ma[i][m-i-1];ma[i][m-i-1] = a;

}

printf ("THe matrix after changing the \n");printf ("main diagonal & secondary diagonal\n");for (i=0;i<m;++i){

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

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

}}else

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

} /* end of main() */

/*----------------------------------------------------OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9THe matrix after changing themain diagonal & secondary diagonal 3 2 1 4 5 6 9 8 7

Page 79: C Programs

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

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

if(ht < 150.0) printf("Dwarf\n");else if((ht>=150.0) && (ht<=165.0))

printf(" Average Height\n");else if((ht>=165.0) && (ht <= 195.0)) printf("Taller\n");else printf("Abnormal height\n");

} /* End of main() */

/*-----------------------------------OutputEnter the Height (in centimetres)45Dwarf----------------------------------------*/

66. /* Write a C program to accept a coordinate point in a XY coordinate system * * and determine its quadrant */

#include <stdio.h>

void main(){

int x,y;

printf("Enter the values for X and Y\n");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");

Page 80: C Programs

} /* End of main() */

/*---------------------------------------------OutputRUN 1Enter the values for X and Y3 5point (5,3) lies in the First quandrant

RUN 2Enter the values for X and Y-4-7point (-7, -4) lies in the Third quandrant

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

67. /* Write a C program to accept a figure code and find the ares of different * * geometrical figures such as circle, square, rectangle etc using switch */

#include <stdio.h>

void main(){

int fig_code;float side,base,length,bredth,height,area,radius;

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

printf("Enter the Figure code\n");scanf("%d",&fig_code);

switch(fig_code){

case 1: printf("Enter the radius\n");scanf("%f",&radius);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);

Page 81: C Programs

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;

} /* End of switch */

} /* End of main() *//*----------------------------------------------------OutputRun 1------------------------- 1 --> Circle 2 --> Rectangle 3 --> Triangle 4 --> Square-------------------------Enter the Figure code2Enter the bredth and length26Area of a Reactangle=12.000000

Run 2------------------------- 1 --> Circle 2 --> Rectangle 3 --> Triangle 4 --> Square-------------------------Enter the Figure code3 Enter the base and height57Area 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 */

Page 82: C Programs

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

switch(grade){

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

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

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

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

} /* End of main() */

/*-------------------------------------------OutputRUN 1Enter the gradesRESULT : SUPER

RUN 2Enter the gradeyRESULT : ABSENT-----------------------------------------------*/

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

Page 83: C Programs

#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++){

fact = fact * i;}

} /* End of else */

printf("Factorial of %d =%5d\n", num,fact);

} /* End of main() */

/*-------------------------------------------OutputEnter the number5Factorial of 5 = 120

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

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

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

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

printf("Enter the string containing both digits and alphabet\n");scanf("%s",string);

for(count=0;string[count]!='\0'; count++){

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

Page 84: C Programs

/*-----------------------------------------------------OutputEnter the string containing both digits and alphabetgoodmorning25NO. of Digits in the string=2Sum 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 */

#include <stdio.h>

void main (){

static int m1[10][10];int i,j,m,n,sum=0;

printf ("Enter the order of the matrix\n");scanf ("%d %d", &m,&n);printf ("Enter the co-efficients of the matrix\n");

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

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

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

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

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

sum = sum + m1[i][j] ;}printf ("Sum of the %d row is = %d\n",i,sum);sum = 0;

}

sum=0;

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

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

sum = sum+m1[i][j];}printf ("Sum of the %d column is = %d\n", j,sum);sum = 0;

}} /*End of main() *//*---------------------------------------------------

Page 85: C Programs

OutputEnter the order of the matrix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9Sum of the 0 row is = 6Sum of the 1 row is = 15Sum of the 2 row is = 24Sum of the 0 column is = 12Sum of the 1 column is = 15Sum of the 2 column is = 18

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

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

#include <stdio.h>

void main (){

static int ma[10][10];int i,j,m,n,a=0,sum=0;

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

}

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

sum = sum + ma[i][i];a = a + ma[i][m-i-1];

Page 86: C Programs

}

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

/*--------------------------------------------------------OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9

The sum of the main diagonal elements is = 15The sum of the off diagonal elemets is = 15

Run 2Enetr the order of the matix2 3The givan order is not square matrix

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

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 *

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

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

void main (){

static int ma[10][10];int i,j,m,n,sum=0,sum1=0,a=0,normal;

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

Page 87: C Programs

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() *//*---------------------------------------------------OutputEnter the order of the matrix3 3Enter the ncoefficients of the matrix1 2 34 5 67 8 9The normal of the given matrix is = 16Trace of the matrix is = 15

Run 2Enter the order of the matrix2 2Enter the ncoefficients of the matrix2 46 8The normal of the given matrix is = 10Trace of the matrix is = 10

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

73. /* Write a C program to accept a matric of order MxN and find its transpose */

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

static int ma[10][10];int i,j,m,n;

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

Page 88: C Programs

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

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

}} /* End of main() */

/*------------------------------------------OutputEnter the order of the matrix2 2Enter the coefiicients of the matrix3 -16 0The given matrix is 3 -1 6 0Transpose of matrix is 3 6 -1 0

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

74. /* Write a C program to accept a matrix and determine whether * * it is a sparse matrix. A sparse martix is matrix which *

* has more zero elements than nonzero elements */

#include <stdio.h>

void main (){

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

Page 89: C Programs

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

printf ("There are %d number of zeros",counter);

} /* EN dof main() *//*----------------------------------------------OutputEnter the order of the matix2 2Enter the co-efficients of the matix1 23 4The given matrix is not a sparse matrixThere are 0 number of zeros

Run 2Enter the order of the matix3 3Enter the co-efficients of the matix1 0 00 0 10 1 0The given matrix is sparse matrixThere are 6 number of zeros

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

75. /* Write a C program to accept a matrix of order MxN and sort all rows * * of the matrix in ascending order and all columns in descendng order */

#include <stdio.h>

void main (){

static int ma[10][10],mb[10][10];int i,j,k,a,m,n;

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

Page 90: C Programs

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]);}printf ("\n");

}

printf ("After arranging rows in ascending order\n");for (i=0;i<m;++i){

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

for (k=(j+1);k<n;++k){

if (ma[i][j] > ma[i][k]){

a = ma[i][j];ma[i][j] = ma[i][k];ma[i][k] = a;

}}

}} /* End of outer for loop*/

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

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

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

}

printf ("After arranging the columns in descending order \n");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;

}

Page 91: C Programs

}}

} /* End of outer for loop*/

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

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

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

}

} /*End of main() */

/*-------------------------------------------------Enter the order of the matrix2 2Enter co-efficients of the matrix3 15 2The given matrix is 3 1 5 2After arranging rows in ascending order 1 3 2 5After arranging the columns in descending order 5 2 3 1-----------------------------------------------------*/

76. /* Write a C program to accept a set of names and sort them in * * an alphabetical order, Use structures to store the names */

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

struct person{

char name[10];int rno;

};typedef struct person NAME;NAME stud[10], temp[10];

void main(){

int no,i;

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

clrscr(); fflush(stdin);

Page 92: C Programs

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

}

printf("\n*****************************\n"); printf (" Names before sorting \n"); /* Print the list of names before sorting */ for(i=0;i<no;i++) {

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

sort(no); /* Function call */

printf("\n*****************************\n"); printf (" Names after sorting \n"); printf("\n*****************************\n"); /* Display the sorted names */ for(i=0;i<no;i++) {

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

} printf("\n*****************************\n");

} /* End of main() */

/* Function to sort the given names */void sort(int N){

int i,j; NAME temp;

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;

}}

}

Page 93: C Programs

} /* end of sort() */

/*--------------------------------------------------------Enter the number of students in the list5

Enter the name of person 1 : RajashreeEnter the roll number of 1 : 123

Enter the name of person 2 : JohnEnter the roll number of 2 : 222

Enter the name of person 3 : PriyaEnter the roll number of 3 : 331

Enter the name of person 4 : AnandEnter the roll number of 4 : 411

Enter the name of person 5 : NirmalaEnter the roll number of 5 : 311

***************************** Names before sortingRajashree 123John 222Priya 331Anand 411Nirmala 311

***************************** Names after sorting

*****************************Anand 411John 222Nirmala 311Priya 331Rajashree 123

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

77. /* Write a C Program to convert the lower case letters to upper case * * and vice-versa */

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

void main(){ char sentence[100]; int count, ch, i; clrscr();

Page 94: C Programs

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

putchar(ch); }

} /*End of main()

/*-----------------------------------------OutputEnter a sentencegOOD mORNING

Input sentence is : gOOD mORNING

Resultant sentence is :Good Morning------------------------------------------*/

78. /* Write a C program to find the sum of two one-dimensional arrays using * * Dynamic Memory Allocation */

#include <stdio.h>#include <alloc.h>#include <stdlib.h>

void main(){

int i,n;int *a,*b,*c;

printf("How many Elements in each array...\n");scanf("%d", &n);

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

Page 95: C Programs

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

} /* End of main() */

/*---------------------------------------OutputHow many Elements in each array...4Enter Elements of First List1234Enter Elements of Second List6789Resultant List is791113

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

79. /* Write a C program to find the sum of all elements of * * an array using pointersas arguments */

#include <stdio.h>

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

Page 96: C Programs

} /* End of main() */

int addnum(int *ptr){

int index, total=0;

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

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

}/*-----------------------------------OutputSum of all array elements = 3000------------------------------------*/

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

#include <stdio.h>

void main(){

float x[10];int i,n;

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

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

scanf("%d", &n);

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

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

scanf("%f",x+i);}

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

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

Page 97: C Programs

float temp;

temp = *ptr1;*ptr1 = *ptr2;*ptr2 = temp;

} /* End of Function */

/*-------------------------------------------OutputHow many Elements...10Enter Elements one by one102030405060708090100

Resultant Array...X[0] = 10.000000X[1] = 20.000000X[2] = 40.000000X[3] = 30.000000X[4] = 50.000000X[5] = 60.000000X[6] = 70.000000X[7] = 80.000000X[8] = 90.000000X[9] = 100.000000----------------------------------------------------*/

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

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

void main(){

union number {

int n1;float n2;

};

union number x;

clrscr() ;

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

Page 98: C Programs

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

/*------------------------------------OutputEnter the value of n1: 2Value of n1 =2Enter the value of n2: 3Value of n2 = 0------------------------------------*/

82. /* Write a C program to find the size of a union*/

#include <stdio.h>

void main(){ union sample

{ int m;

float n; char ch;

};

union sample u;

printf("The size of union =%d\n", sizeof(u));

/*initialization */

u.m = 25; printf("%d %f %c\n", u.m, u.n,u.ch);

u.n = 0.2; printf("%d %f %c\n", u.m, u.n,u.ch);

u.ch = 'p'; printf("%d %f %c\n", u.m, u.n,u.ch);

} /*End of main() */

/*-----------------------------------------OutputThe size of union =425 12163373596672.000000 -13107 0.200000 Í-13200 0.199999 p------------------------------------------*/

Page 99: C Programs

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;

fptr = fopen ("emp.rec", "w"); /*open for writing*/

if (fptr == NULL) { printf("File does not exists\n");

return;}

printf("Enter the name\n"); scanf("%s", name);

fprintf(fptr, "Name = %s\n", name);

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

/*---------------------------------------------------------------------------OutputEnter the namePrabhuEnter the age25Enter the salary25000-------------------------------------Please note that you have to open the file called emp.rec in the directory

Name = PrabhuAge = 25Salary = 25000.00-----------------------------------------------------------------------------*/

84. /*Write a C program to illustrate as to how the data stored on the disk is read */

#include <stdio.h>

Page 100: C Programs

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

if (fptr == NULL){

printf("Cannot open file\n");exit(0);

}

ch = fgetc(fptr);

while (ch != EOF){

printf ("%c", ch);ch = fgetc(fptr);

}

fclose(fptr);} /* End of main () *//*----------------------------------------------OutputEnter the filename to be openedemp.recName = PrabhuAge = 25Salary = 25000.00------------------------------------------------*/

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

#include <stdio.h>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;}

Page 101: C Programs

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++) { r=n%i; if(r==0) { flag=1; }/*end if*/ } /*end of for*/ if(flag==0) { printf("tne number is prime\n"); } else { printf("the number is not prime\n"); }return 0;

}

86. Converts a temperature given in cel into far.

#include <stdio.h>int main() {

float f, c; printf("Enter the temp in c: \n"); scanf("%f", &c); f=32 + (9*c)/5; printf("The temperature in f=%f/n", f);return 0;

}

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

#include <stdio.h>int main() {

int num,x,y;printf("Enter a number :\n");scanf("%d",&num);

x = num % 2;y = num % 3;

Page 102: C Programs

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>

int main() {

int sum=0, r, x,y;printf("Enter a number: \n");scanf("%d", &x);y=x;while (x>0) {

r=x%10; sum=sum+r; x=x/10;

}printf("The sum of the degits of %d is: %d\n", y,sum);return 0;

}

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

#include <stdio.h>

int main() {

int sum=0,x=2,num,i;printf("Enter a number to sum upto :\n");scanf("%d",&num);

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)

Page 103: C Programs

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

}

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

}

91. Write a c program to convert given number of days to a measure of timegiven in years, weeks and days. For example 375 days is equal to 1 year1 week and 3 days (ignore leap year)

#include <stdio.h> #define DAYSINWEEK 7

void main() {

int ndays, year, week, days;

printf("Enter the number of days\n"); scanf("%d",&ndays);

year = ndays/365; week = (ndays % 365)/DAYSINWEEK; days = (ndays%365) % DAYSINWEEK;

printf ("%d is equivalent to %d years, %d weeks and %d days\n", ndays, year, week, days);

}

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

Page 104: C Programs

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; num2=r;

}gcd=num2;printf("%d\n",gcd);

return(1); }

93. Write a c program to swap the contents of two numbers using bitwise XOR operation. Don't use either the 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);

}

94. Calculates the factorial of a given number

#include <stdio.h>int main() {

int i,num,x=1;long fact=1;

Page 105: C Programs

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

}

95. Prints n numbers of fibonacci numbers

#include <stdio.h>int main() {

int num,i;long first=0, second=1, new;

printf("How many fibonacci number to print :\n");scanf("%d",&num);printf("Fibonacci of %d numbers :\n",num);printf("Fibonacci [ 0] is : 0\n");printf("Fibonacci [ 1] is : 1\n");

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

}

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

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

Page 106: C Programs

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;

}

97. Converts a decimal number to it's binary equivalent.

#include <stdio.h>int main(){

int num,sum=0,bs=1,r,n;printf("Enter a number :\n");scanf("%d",&num);n=num;while(num>1) {

r = num%2; num /=2; sum += r * bs; bs *=10;

}

sum += num * bs;

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

}

98. Write a C program to convert the given binary number into decimal

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

Page 107: C Programs

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

}

99. Area of the circle without using macro definition

#include <stdio.h>int main() {

float a,r,pi=3.142;printf("enter the value of radius r\n");scanf("%f",&r);a=pi*r*r;printf("area of the circle a=%f\n",a);

return 1; }

100. Area of the circle using macro definitiom

#include<stdio.h># define pi 3.142

int main() {

float a,r;printf("enter the value of radius of r:\n");scanf("%f",&r);a=pi*r*r;printf("area of the circle a=%f\n",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>

Page 108: C Programs

void main() {

char sentence[100]; int count, ch, i;

clrscr();

printf("Enter a sentence\n"); for(i=0; (sentence[i] = getchar())!='\n'; i++) { ; }

sentence[i]='\0';

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

printf("The given sentence is : %s",sentence);

printf("\nCase changed sentence is: "); for(i=0; i < count; i++) { ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]); putchar(ch); }

}

102. Write a C program read a sentence and count the number ofnumber of vowels and consonants in the given sentence. Output the results on two lines with suitable headings

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

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

Page 109: C Programs

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] ==' ') {

special = special + 1; }

}

consonants = consonants - special; printf("No. of vowels in %s = %d\n", sentence, vowels); printf("No. of consonants in %s = %d\n", sentence, consonants);

}

103. Takes input as : INDIA outputs as :

I

I N I N D I N D I I N D I A

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

int main() {

char ip[20];int i,j,l;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]);

}

Page 110: C Programs

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

}

104. Sums the elements of an array.

#include <stdio.h>

int main() {

int a[10], i, sum=0; printf("Enter 10 elements:\n"); for(i=0; i<10; i++) {

scanf("%d", &a[i]); sum=sum+a[i];

}printf("sum of the array=%d\n", sum);return 0;

}

105. Prints the reverse of a given string

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

int main() {

char name[15], rev[15];int n,i=0, l;

printf("Enter a word:\n");gets(name);n=strlen(name);printf("The reverse of the word :");for(i=n-1; i>=0; i--) { printf("%c",name[i]); }printf("\n");

return(1); }

106. Program for concatanation, copy and length of string

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

Page 111: C Programs

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);n=strlen(name);printf("The length of ur name : %d\n",n);x = strncmp(fname,lname,1);if(x==0) 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");

return(1); }

107. Takes the values of an array and prints the max and next max values

#include <stdio.h>int main() {

int nums[10];int max1, max2, n, i, x=1;printf("How many numbers (less than 10) u want to compare :\n");scanf("%d",&n);for(i=0; i<n; i++) { printf("number %d :\n",x); x++; scanf("%d",&nums[i]); }

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

x=0;max1=nums[0];

Page 112: C Programs

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

}

108. To find Max and Min in an array

#include<stdio.h>main() {

float a[20],max,min;int i;for(i=0;i<20;i++) { printf("Enter value:\n"); scanf("%f",&a[i]); }max=min=a[0];for(i=1;i<20;i++) { if(max<a[i]) max=a[i]; if(min>a[i]) min=a[i]; }printf("In the given array max=%f & min=%f\n",max,min);

}

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

#include <stdio.h>#include <string.h>#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')

Page 113: C Programs

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

return(0); }

110. Checks a string if polidrome and and print the string in reverse.

#include <stdio.h>#include <string.h>int main() {

char name[15];int n,i=0, flag;printf("Enter a word:\n");gets(name);n=strlen(name);while(i<=n/2) { if(name[i]==name[n-1]) { flag=1; i++; n--; } else { flag=0; break; } }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]);

Page 114: C Programs

}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 GOODif code is B, then print FAIR

if code is Y, then print ABSENTif code is F, then print FAILS

*/

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

switch(grade) {

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

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

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

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

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

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

} /* End of switch */

Page 115: C Programs

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

char str[80],search[10];int count1=0,count2=0,i,j,flag;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++) {

flag=1;if (str[j]!=search[j-i]) {

flag=0; break; }

}if (flag==1) break;

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

Page 116: C Programs

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;

}

int sumdigits(int x) {

int r, sum=0; while (x>0) {

r=x%10; sum=sum+r; x=x/10;

} /* end of while*/ return sum;

} /* end of fun */

114. Swaps the value of two variables with function \

#include <stdio.h>void swap(int *x,int *y);

int main() {

int x, y;printf("Enter two values of x and y:\n");scanf("%d%d",&x,&y);printf("Before swapping X=%d and Y=%d\n", x,y);swap(&x,&y);printf("After swapping X=%d and Y=%d\n", x,y);return 0;

}

void swap(int *x, int *y) { int t=*x; *x=*y; *y=t; }

Page 117: C Programs

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

printf("Enter the values of X and N\n"); scanf("%ld %ld",&x,&n);

xpown = power (x,n);

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

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

long int power(int x, int n) {

if (n==1) return(x); else if ( n%2 == 0) return (pow(power(x,n/2),2)); /*if n is even*/ else return (x*power(x, n-1)); /* if n is odd*/

}

116. Calculates the GCD of two numbers with function

#include <stdio.h>int gcd(int x, int y);

int main() {

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;

Page 118: C Programs

if(x>y) { max=x; min=y; } else { max=y; min=x; }r=max%min;while(r!=0) { max=min; min=r; r=max%min; } /* end of while */return min;

} /* end of function */

117. Function to concatenate two string

#include <stdio.h>

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

int main() {

char firststr[15], secondstr[15];int i=0;printf("enter one string:\n");scanf("%s", firststr);printf("enter the second string:\n");scanf("%s", secondstr);str_concat(firststr, secondstr);printf("the new string is:\n%s\n", firststr);return 0;

}

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

Page 119: C Programs

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);int leg_str(char *source);

int main() {

char firststr[15], secondstr[15];int i=0;printf("enter one string:\n");scanf("%s", firststr);str_copy(firststr, secondstr);printf("the copied string is:\n%s\n", secondstr);i=leg_str(firststr);printf("the length of the string is :%d\n", i);return 0;

}

void str_copy(char *source, char *destination) { int i=0; while(source[i]!='\0') { destination[i]= source[i]; i++; } /* end of while loop */ destination[i]='\0'; } /* end of function */

int leg_str(char *source) {

int c=0; while(source[c]!='\0') { c++; } /* end of while */ return(c);

} /*end of function */

Page 120: C Programs

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); system("PAUSE"); return 0;

}

int strlength(char a[]) {

int l=0; while (a[l]!='\0') { l++; } /* end of while loop */ return l;

} /* end of function */

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

#include <stdio.h>long fact(long x);int main() {

long factorial;long num;printf("Enter a number :\n");scanf("%ld",&num);factorial = fact(num);printf("The factorial of %ld is : %ld\n",num,factorial);return(0);

}

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

Page 121: C Programs

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

scanf("%d", &n);printf("Enter Elements one by one\n");for(i=0;i<n;i++) {

scanf("%f",x+i); }

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

printf("\nResultant Array...\n");for(i=0;i<n;i++) {

printf("X[%d] = %f\n",i,x[i]); }

}

121. Write a C program to find the sum of all elements of an array using pointers as arguments

#include <stdio.h>

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;

Page 122: C Programs

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>

int main() {

float *a,sum=0.0,avg;int i,l;printf("How many real numbers u want to put in to array:\n");scanf("%d",&l);a=(float*)malloc(l*sizeof(float));for(i=0;i<l;i++) {

printf("Enter no %d value:\n",i+1); scanf("%f",&a[i]); sum+=a[i]; avg=sum/(float)l; }printf("The avarage of the elements in array is : %f\n",avg);

return 1; }

123. Write a C program to read N names, store them in the formof an array and sort them in alphabetical order. Output thegive names and the sorted names in two columns side by side with suitable heading

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

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

Page 123: C Programs

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

}

printf("\n----------------------------------------\n"); printf("Input Names\tSorted names\n"); printf("------------------------------------------\n"); for(i=0; i< N ; i++) {

printf("%s\t\t%s\n",Tname[i], name[i]); } printf("------------------------------------------\n");

}

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

#include<stdio.h>main() {

int i,j,rsum=0,csum=0,d1sum=0,d2sum=0,vals[4][4]; for(i=0;i<4;i++) { for(j=0;j<4;j++) {

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

Page 124: C Programs

{ 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]; csum+=vals[j][i]; } printf("row sum for row %d is %d",i+1,rsum); printf("\n"); printf("col sum for row %d is %d",i+1,csum); printf("\n"); }

}

125. Takes the elements from the user and puts into a matrix

#include <stdio.h>#define m 3#define n 3

int main() {

int a[m][n];int i,j;printf("enter %d elements:\n", m*n);for(i=0; i<m; i++) { for(j=0; j<n; j++) { scanf("%d", &a[i][j]); } }for(i=0; i<m; i++) { for(j=0; j<n; j++) { printf("%d ", a[i][j]); } printf("\n"); }

system("PAUSE");return 0;

Page 125: C Programs

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

printf("Enter the elements of matrix A\n"); for(i=0; i<R; i++) { for(j=0; j<C; j++) {

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

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

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

for(i=0; i<R; i++) { for(j=0; j<C; j++) {

if(A[i][j] != 1 && A[j][i] !=0) { flag = 0; break; } }

}

if(flag == 1 )printf("It is identity matrix\n");

elseprintf("It is not a identity matrix\n");

Page 126: C Programs

}

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

static char *a1[]={"zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Furteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"};

static char *a2[]={"Zero", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty"};int i;

int main() {

double num, num1;long rs;printf("Enter the amount in Rs :\n");scanf("%lf", &num);rs=num;printf("Rupees ");if(rs<20) f1(rs);else if((rs>19) && (rs<100)) f2(rs);else if((rs>99) && (rs<1000)) f3(rs);else if((rs>999) && (rs<100000)) f4(rs);else if((rs>99999)&&(rs<10000000)) f5(rs);else if((rs>9999999)&&(rs<1000000000)) f6(rs);

num1=num-rs;if(num1!=0) { printf("and "); num1 *=100; rs=num1; if (rs>19) f2(rs); else f1(rs); printf("Paise "); }

printf("Only....\n");return 0;

Page 127: C Programs

}

void f1(int x) { printf("%s ", a1[x]); }

void f2(int x) { i=x/10; printf("%s ",a2[i]); i=x%10; if(i!=0) printf("%s ", a1[i]); }

void f3(int x) { i=x/100; printf("%s Hundred ", a1[i]); i=x%100; if((i!=0)&&(i<20)) f1(i); else if((i!=0)&&(i>19)) f2(i); }

void f4(int x) { i=x/1000; if(i<20) f1(i); else f2(i); printf("Thousand "); i=x%1000; if((i!=0)&&(i<20)) f1(i); else if ((i!=0)&&(i<100)) f2(i); else if (i!=0) f3(i); }

void f5(int x) { i=x/100000; 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)

Page 128: C Programs

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

128. Write a C program to read N integers and store themin an array A, and so find the sum of all these elements using pointer. Output the given array and and the computed sum with suitable heading

#include <stdio.h>#include <malloc.h>

void main() {

int i,n,sum=0; int *a;

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

/*Compute the sum of all elements in the given array*/ for(i=0;i<n;i++) { 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

Page 129: C Programs

#include <stdio.h>#include <string.h>typedef struct { char fname[20]; char lname[15]; long emp_code; } EMP;

int main() {

EMP nist[20];int i,n;printf("Enter the number of employees :\n");scanf("%d",&n);for(i=0; i<n; i++) {

printf("Enter the first name :\n"); scanf("%s",nist[i].fname); printf("Enter the last name :\n"); scanf("%s",nist[i].lname); printf("Enter the employee code :\n"); scanf("%ld",&nist[i].emp_code);

}

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

printf("The name of the employee : %s %s and the employee code :%ld\n", nist[i].fname,nist[i].lname,nist[i].emp_code);

}

return(0); }

130. Write a C program to illustrate as to how the data stored in the file 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*/

Page 130: C Programs

if (fptr == NULL) {

printf("Cannot open file\n");exit(0);

}

ch = fgetc(fptr);

while (ch != EOF) {

printf ("%c", ch);ch = fgetc(fptr);

}

fclose(fptr); }

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.

#include <stdio.h>

void main() {

FILE *fptr;char name[20];

int age;float salary;

fptr = fopen ("emp.rec", "w"); /*open for writing*/

if (fptr == NULL) { printf("File does not exists\n");

return; }

printf("Enter the name\n"); scanf("%s", name);

fprintf(fptr, "Name = %s\n", name);

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

Page 131: C Programs