Sei sulla pagina 1di 53

List of C Programs

Simple Programs 1. 2. 3. 4. 5. 6. 7. 8. 9. WAP to find the average of three numbers. (Pgm No. 1) WAP to convert Celsius to Fahrenheit. (Pgm No. 2) WAP to calculate the factorial of a given number. (Pgm No. 3) WAP to print whether the given number is even or odd. (Pgm No. 4) WAP to swap two numbers using temporary variable. (Pgm No. 5) WAP to swap two numbers without using temporary variable. (Pgm No. 6) WAP to find whether the given number is prime or not. (Pgm No. 7) WAP to check whether the given number is Armstrong or not. (Pgm No. 8) WAP to check whether the given number is palindrome or not. (Pgm No. 9)

10. WAP to print the Fibonacci series up to a given input number. (Pgm No. 10)

sv

ie

tk

11.

WAP to print the greatest of the three numbers. (Pgm No. 11)

Programs using Switch-Case

12. WAP to verify the given alphabet is vowel or not. (using switch-case) (Pgm No. 12) 13. Write a menu driven program to perform addition, subtraction, multiplication, division, modulo division

an

ur

of two numbers. (Pgm No. 13) Programs on Conditional Operator.

14. WAP to print the ascending order of three numbers. (Pgm No. 14)

15. WAP to print y = 1 for x > 0, y = 0 for x = 0, y = -1 for x < 0 using conditional operator. (Pgm No. 15)

Program to illustrate increment and decrement operators

16. WAP to illustrate the operations of increment and decrement operators. (Pgm No. 16) Programs to illustrate Mathematical library functions and #define

17. WAP to calculate the value of (i). ax2+bx+c (ii). b2-4ac / 2a (iii). xlogx (iv). cos2x +sin2x (Pgm No. 17) 18. WAP to find the area of the triangle with (i). given sides a, b, c (ii). given height and No. 18) 19. WAP to find (i). area of circle (ii). circumference of the circle. (Pgm No. 19) 20. WAP for the following series 1 1/22 + 1/32 ..1/n2 using mathematical functions. 21. WAP for the following series ex = 1 + x + x2/2! + x3/3! + .. using mathematical functions. breadth. (Pgm

u.

tk

22. WAP for the following series sinx = x x3/3! + x5/5! x7/7!.. using mathematical functions. Programs on Single Dimensional Arrays 23. WAP to find out the maximum & minimum element from the given array. (Pgm No.20) 24. WAP to display the position of a given number in an array. (Pgm No.21) 25. WAP to insert a number in to an array at the required position. (Pgm No.22) 26. WAP to delete a number in an array at the given position. (Pgm No.23) 27. WAP to calculate the frequency of a given number in an array. (Pgm No.24) 28. WAP to calculate the mean, variance, standard deviation for the given array elements. (Pgm No.25) 29. WAP to reversing the elements of the array. (Pgm No.26) 30. WAP to count the no. of even numbers and no. of odd numbers in a given array. (Pgm No.27) 31. WAP to find the sum of all diagonal elements in a matrix. (Pgm No.28) Programs on Double Dimensional Arrays 32. WAP to enter and print the elements in a double dimensional array. (Pgm No. 29) 33. WAP to print the sum of (i) all row elements (ii) all column elements in an array. (Pgm No.30) 34. WAP to evaluate the addition of two mxn and mxn matrices. (Pgm No.31) 35. WAP to evaluate the matrix multiplication of two mxn and nxq matrices. (Pgm No.32) 36. WAP to display the upper, lower matrix for a given array. (Pgm No.33) 37. WAP to find the transpose of a given matrix. (Pgm No.34) Programs on Strings

38. i). WAP to copy one string into another string with the use of built in functions. (Pgm No.35 a) ii). WAP to copy one string into another string without using any built in functions. (Pgm No.35 b) 39. i). WAP to concatenate two strings with the use of built in functions. (Pgm No.36 a) ii). WAP to concatenate two strings without using any built-in functions. (Pgm No.36 b) 40. i). WAP to compare two strings with the use of built in functions. (Pgm No.37 a) ii). WAP to compare two strings without using any built in functions. (Pgm No.37 b) 41. i). WAP to find length of a string with the use of built-in functions. (Pgm No.38 a) ii). WAP to find length of a string without using any built in functions. (Pgm No.38 b) 42. i). WAP to convert strings from lower case to upper case with the use of built-in functions. (Pgm No.39 a) ii). WAP to convert strings from lower case to upper case without using any built in functions. (Pgm No.39 b)

sv

ie

tk

an

ur

u.

tk

43. i). WAP to convert strings from upper case to lower case with the use of built-in functions. (Pgm No.40 a) ii). WAP to convert strings from upper case to lower case without using any built in functions. (Pgm No.40 b) 44. i). WAP to reverse a string with the use of built-in functions. (Pgm No.41 a) ii). WAP to reverse a string without using any built in functions. (Pgm No.41 b) 45. i). WAP to check whether a given string is palindrome or not with the use of built-in functions. (Pgm No.42 a) ii). WAP to check whether a given string is palindrome or not without using any built in functions. (Pgm

sv
2 4

No.42 b)

Programs to illustrate I/O functions 46. WAP to print the following format (Pgm No.43) 1

ie
3 5

tk
2 4

3 4 5 5

3 4 5

an
5

Programs on Structures 47.

WAP to create student data structure that accepts as well as to display Student Name (First Name, Last Name), Branch, Regd No, Address(D.No, Address Line 1,Street Name, District, Pin Code), Contact Number (Land Line, Mobile 1, Mobile 2) of 15 students. Programs to illustrate function calls

48. WAP to menu driven program to perform the operations add, subtract, multiply, divide, modulo division of two complex numbers x+iy, a+ib using functions with arguments and return values. (Pgm No.44) 49. WAP to find the sum of all elements in an array by using functions with arguments and no return values. (Pgm No.45) 50. WAP to swap two number using call by reference concept. (Pgm No. 46) Programs to illustrate the concept of recursion 51. WAP to calculate the factorial of a given number using recursion. (Pgm No.47) 52. WAP to calculate the Fibonacci series by using recursion. (Pgm No.48)

ur

u.

tk

Programs on Numerical Methods 53. WAP for Bisection Method. 54. WAP for Newton Raphson Method. 55. WAP for Lagranges Interpolation. 56. WAP for Iteration Method. 57. WAP for Regula Falsi Method.

sv ie tk an ur u. tk

C Programs
Simple Programs 1. /* PROGRAM TO FIND THE AVERAGE OF THREE NUMBERS */

#include<stdio.h> main() { int a, b, c; float avg; printf("\n Enter any three numbers of your choice: "); scanf("%d %d %d", &a, &b, &c); avg = (a + b + c)/3.0;

printf("\n The average of three numbers %d, %d and %d is %f",a,b,c,avg); } 2.

/* PROGRAM TO CONVERT CELSIUS TEMPERATURE TO FAHRENHEIT TEMPERATURE */

#include<stdio.h> main() { float cel, fah;

printf("\n Enter the value of the temperature in celsius: "); scanf("%f", &cel); fah = cel * 1.8 + 32;

printf("\n The value of temperature in fahrenheit is %f\n", fah); } 3.

/* PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER */

#include<stdio.h> main() { int n, i, fact=1; printf("\n Enter a number of your choice :"); scanf("%d",&n); for(i = 1;i <= n; i++) { fact = fact * i;

sv

ie

tk

an ur u. tk

} printf("\n The factorial of the given number is %d",fact); } 4. /* PROGRAM TO CHCK WHETHER THE GIVEN NUMBER IS EVEN OR ODD */

#include<stdio.h> main() { int num; printf("\n Enter a number of your choice :"); scanf("%d", &num); if(num % 2 == 0)

printf("\n The entered number is an EVEN number");

sv

ie

else

printf("\n The entered number is an ODD number");

tk

} 5.

/* PROGRAM TO SWAP TWO VALUES WITH USING TEMPORARY VARIABLE*/

an

#include<stdio.h> void main() { int a,b,temp;

printf("\n Enter two numbers of your choice: "); scanf("%d %d",&a,&b);

printf("\n The values before swapping are a = %d and b = %d", a, b); t = a; a = b; b = t;

printf("\n The values after swapping are a = %d and b = %d", a, b); } 6. /* PROGRAM TO SWAP TWO VALUES WITHOUT USING TEMPORARY VARIABLE*/

ur u. tk

#include<stdio.h> void main() { int a,b;

printf("\n Enter two numbers of your choice: "); scanf("%d %d",&a,&b); printf("\n The values before swapping are a = %d and b = %d", a, b); a=a+b; b=a-b; a=a-b; printf("\n The values after swapping are a = %d and b = %d", a, b); } 7. /* PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS PRIME OR NOT*/

#include<stdio.h> main() {

int n,i,rem,count=0;

printf("\n Enter a number you want to check: ");

scanf("%d",&n); if(n==1)

printf("\n %d is neither prime nor composite",n); else { for(i=2;i<=n;i++) { rem=n%i; if(rem==0) { count = count+1; } } if (count==1) printf("\n The given number %d is a prime number", n); else printf("\n The given number %d is not a prime number", n); } }

sv ie tk an ur u. tk

8.

/* PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT */

#include<stdio.h> main() { int num, rem, temp, i, sum = 0; printf("\n Enter the number of your choice: "); scanf("%d", &num); temp = num; while(num != 0)

sv

rem = num % 10; num = num / 10;

sum = sum+(rem * rem * rem); } if(sum == temp)

printf("\n The given number %d is armstrong number",temp);

ie

tk

an

else

printf("\n The given number %d is not an armstrong number", temp); } 9.

/* PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS PALINDROME OR NOT */

ur

#include<stdio.h> main() { int num, temp, rev = 0, rem;

u. tk

printf("\n Please enter a number of your choice: "); scanf("%d",&num); temp = num; while(num != 0) { rem = num % 10; rev = rev * 10 + rem; num = num / 10; }

if(rev == temp) printf("\n The given number %d is a palindrome",temp); else printf("\n The given number %d is not a palindrome", temp); } 10. /* PROGRAM TO PRINT THE FIBONACCI SERIES */

#include<stdio.h> main() { int i=0,j=1,k=2,r,f;

printf("Enter the number range:"); scanf("%d",&r);

printf("\nFIBONACCI SERIES: "); printf("\t%d\t %d",i,j); while(k<r) { f=i+j; i=j; j=f; printf(" \t%d",j); k++; } } 11.

/* PROGRAM TO FIND THE GREATEST OF THREE NUMBERS */

#include<stdio.h> main() { int a,b,c,big; printf("\nEnter three numbers of your choice:"); scanf("%d %d %d",&a,&b,&c); big=(a>b&&a>c?a:b>c?b:c); printf("\nThe biggest number is: %d",big); }

sv

ie tk an ur u. tk

SWITCH-CASE
12. /* PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS VOWEL OR NOT */ #include<stdio.h> main() { char ch; printf("\n Please enter a character of your choice: "); scanf("%c", &ch); switch(ch) {

case 'a' : case 'e' : case 'i' : case 'o' : case 'u' :

case 'A' : case 'E' : case 'I' : case 'O' :

case 'U' : printf("\n The entered character is a VOWEL");

default : printf("\n The entered character is a CONSONANT");

sv ie tk
break; break;

an ur u. tk

} }

13. /* MENU DRIVEN PROGRAM TO PERFORM ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, MODULO DIVISION OF TWO NUMBERS*/ #include<stdio.h> main() { int num1, num2, choice, ch; do

{ printf("\n\tMENU"); printf("\n\t----"); printf("\n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Divison \n 5. Modulo Divison \n"); printf("\n Please enter your choice: "); scanf("%d",&choice); printf("\n Enter any two numbers of your choice: "); scanf("%d %d", &num1, &num2); switch(choice) {

case 1: printf("\n The sum of the given numbers %d and %d is %d\n",

num1,num2,num1+num2); break;

case 2: printf("\n The difference of the given numbers %d and %d is %d\n", num1,num2,num1-num2); break;

case 3: printf("\n The product of the given numbers %d and %d is %d\n",

num1,num2,num1*num2); break;

case 4: printf("\n The divison of the given numbers %d and %d is %d\n",

num1,num2,num1/num2); break;

case 5: printf("\n The modulo divison of the given numbers %d and %d is %d\n", num1,num2,num1%num2); break; } printf("\n Do you want to continue....Press \n(1) to continue\n(2) to quit the program\n"); scanf("%d",&ch); }while(ch == 1); }

sv

ie

tk

an

ur

u.

tk

PROGRAMS ON CONDITIONAL OPERATOR


14. /* PROGRAM TO PRINT GREATEST OF THREE NUMBERS USING CONDITIONAL OPERATOR */

#include<stdio.h> main() { int num1, num2, num3; printf("\n Enter any three numbers of your choice: "); scanf("%d %d %d", &num1, &num2, &num3); printf("\n%d is the greatest number of the given three numbers %d %d and %d\n",((num1 > num2)&&(num1 > num3))? num1 : ((num2 > num3) && (num2 > num1))? num2 : num3, num1, num2, num3);

sv

ie

15. /* PROGRAM TO PRINT Y = 1 FOR X > 0, Y = 0 FOR X = 0, Y = -1 FOR X < 0 USING CONDITIONAL

tk

OPERATOR */ #include<stdio.h> main() { int x, y;

an ur u. tk

printf("\n Enter the value of x: "); scanf("%d", &x); y = (x > 0) ? 1 : (x < 0) ? -1 : 0; printf("\n y = %d for x = %d \n", y, x); }

PROGRAMS ON INCREMENT AND DECREMENT OPERATORS:


16. /* PROGRAM TO ILLUSTRATE THE OPERATIONS OF INCREMENT AND DECREMENT OPERATORS */ #include<stdio.h> main() { int x;

printf("\n Enter the value of x: "); scanf("%d",&x); printf("\n\n Pre-Increment Operator"); printf("\n ----------------------"); printf("\n %d is the preincrement value for %d", ++x, x); printf("\n\n Post-Increment Operator"); printf("\n -----------------------"); printf("\n %d is the postincrement value for %d", x++, x); printf("\n\n Pre-Decrement Operator"); printf("\n ----------------------"); printf("\n %d is the predecrement value for %d", --x, x); printf("\n\n Post-Decrement Operator"); printf("\n -----------------------"); printf("\n %d is the postdecrement value for %d\n", x--, x); }

PROGRAMS ON MATHEMATICAL FUNCTIONS AND #DEFINE


17. /* PROGRAM TO CALCULATE THE VALUE OF (I). AX2+BX+C (II). B2-4AC / 2A (III). XLOGX (IV). COS2X +SIN2X */ #include<stdio.h> #include<math.h> #define pi 3.1456 main() { int ch, choice, a, b, c, x; float equ, root, log, trig; do { printf("\n\t\tMENU"); printf("\n\t\t----"); printf("\n 1. ax2+bx+c \n 2. b2-4ac / 2a \n 3. xlogx \n 4. cos2x +sin2x \n 5. Quit \n"); printf("\n Please enter your choice: ");

sv

ie

tk

an

ur u. tk

scanf("%d",&choice); switch(choice) { case 1: printf("\n Please enter the values of a, b, c and x: "); scanf("%d %d %d %d", &a, &b, &c, &x); equ = (a * pow(x,2) + b * x + c); printf("\n The value of ax2+bx+c is %f", equ); break; case 2: printf("\n Please enter the values of a, b, and c: ");

case 3: printf("\n Please enter the value of x: "); scanf("%d", &x); log = x*log10(x);

case 4: printf("\n Please enter the value of x: "); scanf("%d", &x); x = x * (180/pi);

case 5: exit(0); break; default: printf("\n Please enter correct choice...."); }

printf("\n Do you want to continue....Press \n(1) to continue\n(2) to quit the program\n"); scanf("%d",&ch); }while(ch == 1); }

sv

scanf("%d %d %d", &a, &b, &c); root = (pow(b,2) - 4*a*c)/(2*a); printf("\n The value of (b2-4ac)/2a is %f", root); break;

ie
break; break;

printf("\n The value of xlogx is %f", log);

trig = pow(cos(x),2) + pow(sin(x),2);

printf("\n The value of cos2x+sin2x is %f", trig);

tk

an

ur

u.

tk

18. /* PROGRAM TO FIND THE AREA OF THE TRIANGLE WITH (I). GIVEN SIDES A, B, C (II). GIVEN HEIGHT AND BREADTH */ #include<stdio.h> #include<math.h> main() { int choice, ch, a, b, c; float x, s, area; do {

printf("\n\t\tMENU"); printf("\n\t\t----");

printf("\n 1. Area of Triangle given sides a, b and c \n 2. Area of triangle given breadth and height

sv

ie
break; break; break;

tk an ur u. tk

\n 3. Quit \n");

printf("\n Please enter your choice: "); scanf("%d",&choice); switch(choice) {

case 1: printf("\n Please enter the values of a, b, c: "); scanf("%d %d %d", &a, &b, &c); s = (float)((a+b+c)/2); x = s*(s-a)*(s-b)*(s-c); area = sqrt(x);

printf("\n The area of triangle is %f", area);

case 2: printf("\n Please enter the values of breadth and height: "); scanf("%d %d", &b, &c); area = (0.5 * b * c); printf("\n The area of triangle is %f", area);

case 3: exit(0);

default: printf("\n Please enter correct choice...."); } printf("\n Do you want to continue....Press \n(1) to continue\n(2) to quit the program\n"); scanf("%d",&ch); }while(ch == 1); } 19. /* PROGRAM TO FIND (I). AREA OF CIRCLE (II). CIRCUMFERENCE OF THE CIRCLE */ #include<stdio.h> #include<math.h>

#define pi 3.14159 main() {

int choice, ch, r; float circum, area; do {

printf("\n\t\tMENU"); printf("\n\t\t----");

printf("\n 1. Area of Circle \n 2. Circumference of Circle \n 3. Quit \n"); printf("\n Please enter your choice: "); scanf("%d",&choice); switch(choice) {

case 1: printf("\n Please enter the value of radius: "); scanf("%d",&r); area = pi * pow(r,2);

case 2: printf("\n Please enter the value of radius: "); scanf("%d", &r); circum = 2 * pi * r; printf("\n The Circumference of triangle is %f", circum);

sv ie
printf("\n The Area of triangle is %f", area); break;

tk an ur u. tk

break; case 3: exit(0); break; default: printf("\n Please enter correct choice...."); } printf("\n Do you want to continue....Press \n(1) to continue\n(2) to quit the program\n"); scanf("%d",&ch); }while(ch == 1); }

20. /* PROGRAM TO PRINT THE MAXIMUM AND MINIMUM ELEMENT FROM GIVEN ARRAY */ #include<stdio.h> main() {

int a[10], n, max, min, i;

printf("\n Enter array size n = "); scanf("%d",&n);

printf("\n Enter the elements of the array: "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } min = a[0]; max = a[0]; for(i=0;i<n;i++) { if(a[i] > max) { max = a[i]; } if(a[i] < min) { min = a[i];

sv

ie tk an ur u. tk

} } printf("\n The given array is "); for(i=0;i<n;i++) { printf("\t%d",a[i]); } printf("\n Maximum element in the given array is %d", max); printf("\n Minimum element in the given array is %d", min);

sv

21. /* PROGRAM TO DISPLAY THE POSITION OF A GIVEN NUMBER IN AN ARRAY*/

ie tk an ur u. tk

#include<stdio.h> main() {

int n, a[10], i, search, pos = 0;

printf("\n Enter array size: "); scanf("%d",&n);

printf("\n Enter the elements of the array: "); for(i=0;i<n;i++) { scanf("%d",&a[i]); }

printf("\n Please enter the number for which you want find the position: "); scanf("%d", &search); for(i=0;i<n;i++) { if(a[i] == search) { pos += 1;

printf("\n The search element is present at position %d of the given array", i); } }

if(pos == 0) { printf("\n Sorry....The search element is not present in the given array"); } } 22. /* PROGRAM TO INSERT A NUMBER IN TO AN ARRAY AT THE REQUIRED POSITION */ #include<stdio.h> main() {

int x[10], n, i, a, pos;

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

scanf("%d", &n);

printf("\n Please enter the elements of the array: ");

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

printf("\n The given array before inserting is :"); for(i=0;i<n;i++) { printf("\t%d",x[i]); }

printf("\n Please enter the number and position you want to enter into the array: "); scanf("%d %d", &a, &pos); if(pos < (n-1)) { for(i=n-1;i>=0;i--) { x[i]= x[i-1]; if(i == pos) { x[i]=a;

sv

ie

tk an ur u. tk

break; } } } else { printf("\n Sorry....position exceeds array size....the element cannot be inserted"); } printf("\n The array after inserting the element is :"); for(i=0;i<n;i++) {

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

23. /* PROGRAM TO DELETE A NUMBER IN AN ARRAY AT THE GIVEN POSITION */ #include<stdio.h> main() { int x[10], n, i, a, pos = 0; printf("\n Enter size of the array: "); scanf("%d", &n); printf("\n Please enter the elements of the array: "); for(i=0;i<n;i++) { scanf("%d", &x[i]); } printf("\n The given array before deleting the element is :"); for(i=0;i<n;i++) { printf("\t%d",x[i]); } printf("\n Please enter the number you want to delete from the array: ");

sv

ie tk an ur u. tk

scanf("%d", &a); for(i=0;i<n;i++) { if(x[i] == a) { pos = i; printf("\n The element %d has been removed from position %d of the array", a, pos); for(i=pos;i<n;i++) {

sv

x[i] = x[i+1];

x[n-1] = 0; } } if(pos == 0) {

printf("\n Sorry....The element is not present in the array"); }

printf("\n The array after deleting the element is :"); for(i=0;i<n;i++) { printf("\t%d",x[i]); } }

24. /* PROGRAM TO CALCULATE THE FREQUENCY OF A GIVEN NUMBER IN AN ARRAY */ #include<stdio.h> main() { int x[10], n, i, search, count=0, err=0; printf("\n Please enter array size: "); scanf("%d",&n); printf("\n Please enter elements in to array:");

ie tk an ur u. tk

for(i=0;i<n;i++) { scanf("%d",&x[i]); } printf("\n The given array is "); for(i=0;i<n;i++) { printf("\t%d",x[i]); }

printf("\n Please enter search element: "); scanf("%d", &search); for(i=0;i<n;i++) { if(search == x[i]) { count += 1; err += 1; } } if(err == 0) {

printf("\n Sorry.... The element is not present in the array"); } else { printf("\n The frequency of %d is %d", search, count); } }

25. /* PROGRAM TO CALCULATE THE MEAN, VARIANCE, STANDARD DEVIATION FOR THE GIVEN ARRAY ELEMENTS*/ #include<stdio.h>

sv

ie tk an ur u. tk

#include<math.h> main() { int x[10], n, i; double total=0.0; float sum=0.0,M,V,SD; printf("\n Please enter size of the array: "); scanf("%d",&n); printf("\n Please enter array elements: "); for(i=0;i<n;i++) {

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

printf("\n The array elements are: "); for(i=0;i<n;i++) { printf("\t%d", x[i]); } for(i=0;i<n;i++) { sum=sum+x[i]; total=total+pow(x[i],2); } M=sum/n; V=total-pow(M,2); SD=sqrt(V);

printf("\n Mean=%f\n Variance=%f\n Standard Deviation=%f", M,V,SD); } 26. /* PROGRAM TO REVERSE THE ELEMENTS OF THE ARRAY */ #include<stdio.h> main()

sv

ie tk an ur u. tk

{ int a[10], i, n, t; printf("\n Please enter array size: "); scanf("%d",&n); printf("\n Please enter the elements of the array: "); for(i=0;i<n;i++) { scanf("%d",&a[i]); }

printf("\n Elements before reversing are: "); for(i=0;i<n;i++) {

printf("\t%d",a[i]); } for(i=0;i<n/2;i++) { t=a[i]; a[i]=a[n-1-i]; a[n-1-i]=t; }

printf("\n Elements after reversing are: "); for(i=0;i<n;i++) { printf("\t%d",a[i]); } }

27. /* PROGRAM TO COUNT THE NO. OF EVEN NUMBERS AND NO. OF ODD NUMBERS IN A GIVEN ARRAY */ #include<stdio.h> main() {

sv

ie tk an ur u. tk

int a[10], i, n, t, even=0, odd=0; printf("\n Please enter array size: "); scanf("%d",&n); printf("\n Please enter the elements of the array: "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\n Array elements are: "); for(i=0;i<n;i++) {

printf("\t%d",a[i]); } for(i=0;i<n;i++) { if((a[i]%2) == 0) { even += 1; } else { odd += 1; } }

printf("\n The number of even elements in the given array are %d", even); printf("\n The number of odd elements in the given array are %d", odd); }

28. /* PROGRAM TO FIND THE SUM OF ALL DIAGONAL ELEMENTS IN A MATRIX */ #include<stdio.h> main() { int a[10][10], m, n, i, j, sum = 0;

sv

ie tk an ur u. tk

printf("\n Please enter the row size of the array: "); scanf("%d", &m); printf("\n Please enter the column size of the array: "); scanf("%d", &n); printf("\n Please enter the elements of the array: "); for(i=0; i<m; i++) { for(j=0; j<n; j++) {

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

printf("\n The array elements are....\n");

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

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

printf("\n The sum of all the diagonal elements is %d\n", sum);

sv

ie

tk an ur u. tk

29. /* PROGRAM TO ENTER AND PRINT THE ELEMENTS IN A DOUBLE DIMENSIONAL ARRAY */ #include<stdio.h> main() { int a[10][10], rs, cs, i, j; printf("\n Please enter the row size and column size respectively: "); scanf("%d %d", &rs, &cs); printf("\n The size of the array is %d", rs*cs); printf("\n Enter %d elements of the array: ", rs*cs); for(i=0; i<rs ; i++) { for(j=0; j<cs; j++) { scanf("%d", &a[i][j]); } }

printf("\n The elements of the given array are: "); for(i=0; i<rs ; i++) { for(j=0; j<cs; j++) { printf("\t%d", a[i][j]); } }

printf("\n The elements of the given array in matrix form is: \n"); for(i=0; i<rs ; i++) { for(j=0; j<cs; j++)

sv

ie

tk an ur u. tk

{ printf("\t%d", a[i][j]); } printf("\n"); } } 30. /* PROGRAM TO PRINT THE SUM OF (I) ALL ROW ELEMENTS (II) ALL COLUMN ELEMENTS IN AN ARRAY */ #include<stdio.h> main() {

int a[10][10], rs, cs, i, j, sr, sc;

printf("\n Please enter the row size and column size respectively: ");

scanf("%d %d", &rs, &cs);

printf("\n The size of the array is %d", rs*cs); printf("\n Enter %d elements of the array: ", rs*cs);

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

printf("\n The elements of the given array in matrix form is: \n"); for(i=0; i<rs ; i++) { for(j=0; j<cs; j++) { printf("\t%d", a[i][j]); } printf("\n"); }

sv ie tk an ur u. tk

for(i=0; i<rs ; i++) { sr = 0; for(j=0; j<cs; j++) { sr += a[i][j]; } printf("\n The sum of elements of row %d is %d", i, sr); }

for(i=0; i<cs ; i++) { sc = 0;

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

sc += a[j][i]; }

printf("\n The sum of elements of column %d is %d", i, sc); } }

31. /* PROGRAM TO EVALUATE THE ADDITION OF TWO MATRICES */

#include<stdio.h> main() {

int a[10][10], b[10][10], c[10][10], rs1, cs1, rs2, cs2, i, j, k, test=0;

printf("\n Please enter the row size and column size of matrix1 respectively: "); scanf("%d %d", &rs1, &cs1); printf("\n The size of the array is %d", rs1*cs1); printf("\n Enter %d elements of the array: ", rs1*cs1); for(i=0; i<rs1 ; i++) { for(j=0; j<cs1; j++) {

sv ie tk an ur u. tk

scanf("%d", &a[i][j]); } } printf("\n First matrix elements are: \n"); for(i=0; i<rs1 ; i++) { for(j=0; j<cs1; j++) { printf("\t%d", a[i][j]);

sv

printf("\n"); }

printf("\n Please enter the row size and column size of matrix2 respectively: ");

scanf("%d %d", &rs2, &cs2);

printf("\n The size of the array is %d", rs2*cs2); printf("\n Enter %d elements of the array: ", rs2*cs2);

ie

tk

an

for(i=0; i<rs2 ; i++) { for(j=0; j<cs2; j++) { scanf("%d", &b[i][j]); } }

ur u. tk

printf("\n Second matrix elements are: \n"); for(i=0; i<rs2 ; i++) { for(j=0; j<cs2; j++) { printf("\t%d", b[i][j]); } printf("\n"); }

if((rs1 == rs2) && (cs1 == cs2)) { printf("\n The matrix addition is possible and the result is \n"); for(i=0; i<rs1; i++) { for(j=0; j<cs2; j++) { c[i][j] = a[i][j]+b[i][j]; test += 1;

sv

} }

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

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

printf("\t%d", c[i][j]); } printf("\n"); } } if(test == 0) {

printf("\n Matrix addition is not possible"); } }

32. /* PROGRAM TO EVALUATE THE MATRIX MULTIPLICATION OF TWO MATRICES */ #include<stdio.h> main() { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; printf("\n Enter row size and column size for first matrix: "); scanf("%d %d", &r1, &c1);

ie tk an ur u. tk

printf("\n Enter %d elements of the First matrix: ", r1*c1); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d", &a[i][j]); } printf("\nThe first matrix is :\n"); for(i=0;i<r1;i++) {

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

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

printf("\n Enter row size and column size for second matrix: "); scanf("%d %d", &r2, &c2);

printf("\n Enter %d elements of Second matrix: ", r2*c2);

for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&b[i][j]); } printf("\n The second matrix is:\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",b[i][j]); printf("\n"); } if(c1 == r2) {

printf("\n Matrix multiplication is possible...."); printf("The result of the multiplication is as follows:\n");

sv ie tk an ur u. tk

for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { mult[i][j]=0; for(k=0;k<r1;k++) { mult[i][j] += a[i][k]*b[k][j]; }

printf("%d\t",mult[i][j]); }

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

printf("Sorry....Matrix multiplication cannot be performed"); } }

33. /* PROGRAM TO DISPLAY THE UPPER, LOWER MATRIX FOR A GIVEN ARRAY */

/*prgm for upper & lower triangular matrix*/ #include<stdio.h> main() { int x[10][10],i,j,k,rs,cs;

printf("\n Enter row size and column size of the matrix: "); scanf("%d %d",&rs, &cs); if(rs == cs) { printf("\n Enter %d elements into matrix: ", rs*cs); for(i=0;i<rs;i++) {

sv

ie tk an ur u. tk

for(j=0;j<cs;j++) { scanf("%d",&x[i][j]); } } printf("\n The given matrix is:\n"); for(i=0;i<rs;i++) { for(j=0;j<cs;j++)

sv
{ } else { }

printf("\t%d",x[i][j]);

ie tk an ur u. tk
printf("\t%d",x[i][j]); printf("\t%d",0);

printf("\n"); }

printf("\nLower Triangular Matrix"); printf("\n-----------------------\n"); for(i=0;i<rs;i++) { for(j=0;j<cs;j++) { if(i >= j)

} printf("\n"); } printf("\nUpper Triangular Matrix");

printf("\n-----------------------\n"); for(i=0;i<rs;i++) { for(j=0;j<cs;j++) { if(i <= j) { printf("\t%d",x[i][j]); }

sv
else {

ie

printf("\t%d",0);

} }

tk an ur u. tk

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

printf("\n Row size and Column size are not equal....");

printf("\n So triangular matrices cannot be formed");

printf("\n Please try again with equal row size and column size\n"); } }

34. /* PROGRAM TO FIND THE TRANSPOSE OF A GIVEN MATRIX */ #include<stdio.h> main() { int x[10][10],i,j,k,rs,cs; printf("\n Enter row size and column size of the matrix: "); scanf("%d %d",&rs, &cs); printf("\n Enter %d elements into matrix: ", rs*cs);

for(i=0;i<rs;i++) { for(j=0;j<cs;j++) { scanf("%d",&x[i][j]); } } printf("\n The given matrix is:\n"); for(i=0;i<rs;i++) {

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

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

printf("\n The transpose of the given matrix is \n"); for(i=0;i<cs;i++) { for(j=0;j<rs;j++) { printf("\t%d",x[j][i]); } printf("\n"); } } 35. a. /* PROGRAM TO COPY ONE STRING INTO ANOTHER STRING WITH USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main() {

sv ie tk an ur u. tk

char str1[25], str2[25]; printf("\n Please enter the first string: "); gets(str1); printf("\n Please enter the second string: "); gets(str2); printf("\n First string before copying is "); puts(str1); printf("\n Second string before copying is "); puts(str2);

strcpy(str1,str2);

printf("\n First string after copying is ");

sv

ie

puts(str1);

printf("\n Second string after copying is ");

tk

puts(str2); }

b. /* PROGRAM TO COPY ONE STRING INTO ANOTHER STRING WITH OUT THE USE OF BUILT-

an

IN FUNCTIONS */ #include<stdio.h> main() { char str1[40], str2[20], i = 0, j;

ur u.

printf("\n Please enter the first string: "); gets(str1); printf("\n Please enter the second string: "); gets(str2); printf("\n The first string before copying is "); puts(str1); printf("\n The second string before copying is "); puts(str2); for(i=0; str2[i] != '\0'; i++) { str1[i] = str2[i];

tk

} str1[i] = '\0'; str2[i] = '\0'; printf("\n The first string after copying is "); puts(str1); printf("\n The second string after copying is "); puts(str2); } 36. a. /* PROGRAM TO CONCATENATE ONE STRING INTO ANOTHER STRING WITH USE OF BUILT-IN FUNCTIONS */ #include<stdio.h>

#include<string.h> main() {

char str1[25], str2[25];

printf("\n Please enter the first string: "); gets(str1);

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

printf("\n First string before concatenatin is "); puts(str1);

printf("\n Second string before concatenation is "); puts(str2); strcat(str1,str2); printf("\n First string after concatenation is "); puts(str1); printf("\n Second string after concatenation is "); puts(str2); }

sv

ie tk an ur u. tk

b. /* PROGRAM TO CONCATENATE ONE STRING INTO ANOTHER STRING WITHOUT THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> main() { char str1[40], str2[20], i = 0, j; printf("\n Please enter the first string: "); gets(str1); printf("\n Please enter the second string: ");

sv

gets(str2);

printf("\n The first string before concatenations is ");

ie

puts(str1);

printf("\n The second string before concatenations is ");

tk an ur u. tk

puts(str2); while(str1[i] != '\0') { i++; }

for(j = 0; str2[j] != '\0'; j++) { str1[i] = str2[j]; i++; }

printf("\n The first string after concatenations is "); puts(str1); printf("\n The second string after concatenations is "); puts(str2); }

37. a. /* PROGRAM TO COMPARE TWO STRINGS WITH USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main()

{ char str1[25], str2[25]; int i; printf("\n Please enter the first string: "); gets(str1); printf("\n Please enter the second string: "); gets(str2); printf("\n First string before comparison is "); puts(str1);

printf("\n Second string before comparison is "); puts(str2);

i = strcmp(str1,str2); if(i == 0)

printf("\n Given two strings are equal"); else if(i < 0)

printf("\n First string %s is smaller than second string %s ", str1, str2);

sv

ie tk an ur u. tk

else

printf("\n First string %s is greater than second string %s ", str1, str2); }

b. /* PROGRAM TO COMPARE TWO STRINGS WITH OUT THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> main() { char str1[20], str2[20], i, count; printf("\n Please enter first string: "); gets(str1); printf("\n Please enter second string: "); gets(str2); printf("\n You entered first string as: "); puts(str1); printf("\n You entered second string as: "); puts(str2);

for(i=0; str1[i] != '\0' || str2[i] != '\0'; i++) { if(str1[i] == str2[i]) count = 0; else count = 1; } if(count == 0) printf("\n The two strings are equal");

sv

else

printf("\n Two stirngs are different");

ie

38. a. /* PROGRAM TO FIND THE LENGTH OF STRING WITH USE OF BUILT-IN FUNCTIONS */

tk an

#include<stdio.h> #include<string.h> main() { char str1[25]; int len;

ur u.

printf("\n Please enter a string: "); gets(str1); printf("\n The entered string is "); puts(str1); len = strlen(str1);

tk

printf("\n The length of the given string is %d", len); }

b. /* PROGRAM TO FIND THE LENGTH OF STRING WITH OUT THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> main() { char str[25], i = 0, len = 0;

printf("\n Please enter a string: "); gets(str); while(str[i] != '\0') { len += 1; i += 1; } printf("\n The length of the entered string is %d", len); }

39. a. /* PROGRAM TO CONVERT A STRING FROM LOWER-CASE TO UPPER-CASE WITH USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main() { char str1[25];

printf("\n Please enter a string in lower case: "); gets(str1); printf("\n The entered string is "); puts(str1); strupr(str1);

printf("\n The entered string after conversion is "); puts(str1); }

b. /* PROGRAM TO CONVERT STRINGS FROM LOWER CASE TO UPPER CASE WITH OUT THE USE OF BUILT-IN FUNCTION */ #include<stdio.h> main() { char str[25], i; printf("\n Please enter a string in lower case: "); scanf("%s", str);

sv

ie tk an ur u. tk

printf("\n The entered string is "); printf("%s", str); for(i=0; str[i] != '\0'; i++) { if(str[i] >= 97 && str[i] <= 122) { str[i] = str[i] - 32; } }

str[i] = '\0';

printf("\n The string after conversion is ");

printf("%s", str); }

40. a. /* PROGRAM TO CONVERT A STRING FROM UPPER-CASE TO LOWER-CASE WITH USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main() { char str1[25];

printf("\n Please enter a string in upper case: "); gets(str1); printf("\n The entered string is "); puts(str1); strlwr(str1); printf("\n The entered string after conversion is "); puts(str1); }

sv

ie tk an ur u. tk

b. /* PROGRAM TO CONVERT A STRING FROM UPPER-CASE TO LOWER-CASE WITH OUT THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> main() { char str[25], i; printf("\n Please enter a string in upper case: "); scanf("%s", str); printf("\n The entered string is "); printf("%s", str);

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

sv

ie tk an ur u. tk

if(str[i] >= 65 && str[i] <= 90) {

str[i] = str[i] + 32; } } str[i] = '\0';

printf("\n The string after conversion is "); printf("%s", str); }

41. a. /* PROGRAM TO REVERSE A STRING WITH USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main() { char str1[25]; printf("\n Please enter a string: "); gets(str1); printf("\n The entered string is "); puts(str1); strrev(str1);

printf("\n The entered string after reversing is "); puts(str1); } b. /* PROGRAM TO REVERSE A STRING WITH OUT THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> main() { char str[20], len = 0, temp, i; printf("\n Please enter a string: ");

sv

gets(str);

printf("\n The entered string is: ");

ie tk an ur u. tk

puts(str);

for(i=0; str[i] != '\0'; i++) { len++; } for(i=0; i<len/2;i++) { temp = str[len-1-i]; str[len-1-i]= str[i]; str[i] = temp; }

printf("\n The string after reversing is: "); puts(str); }

42. a. /* PROGRAM TO CHECK WHETHER A GIVEN STRING IS PALINDROME OR NOT WITH THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main() { char str1[25], temp[25];

int i; printf("\n Please enter a string: "); gets(str1); printf("\n The entered string is "); puts(str1); strcpy(temp,str1); strrev(str1); i = strcmp(temp,str1); if(i == 0)

printf("\n The entered string %s is a palindrome", temp); else

printf("\n The entered string %s is not a palindrome", temp); }

b. /* PROGRAM TO CHECK WHETHER A GIVEN STRING IS PALINDROME OR NOT WITH OUT THE USE OF BUILT-IN FUNCTIONS */ #include<stdio.h> #include<string.h> main() { char str1[25], temp[25]; int i, len = 0, temp1 = 0, count; printf("\n Please enter a string: "); gets(str1); printf("\n The entered string is "); puts(str1); /* STRING COPYING */ for(i=0; str1[i] != '\0'; i++) { temp[i] = str1[i]; } temp[i] = '\0';

sv

ie

tk

an ur u. tk

/* STRING REVERSING */ for(i=0; str1[i] != '\0'; i++) { len++; } for(i=0; i<len/2;i++) { temp1 = str1[len-1-i]; str1[len-1-i]= str1[i]; str1[i] = temp1; }

/* STRING COMPARISON */

for(i=0; str1[i] != '\0' || temp[i] != '\0'; i++)

sv ie
1 2 2 3 4 4 5 5 3 4 5 */

tk an ur u. tk

{ if(str1[i] == temp[i]) count = 0; else count = 1; } if(count == 0)

printf("\n The given string %s is a palindrome", temp); else

printf("\n The given string %s is not a palindrome", temp); } 43. /* PROGRAM TO PRINT THE FOLLOWING FORMAT

3 4 5 5

#include<stdio.h>

main() { int i, j, k, n; printf("\n Please enter a number: "); scanf("%d", &n); for(i=0; i<=n; i++) { for(k=1; k<=n-i;k++) {

printf(" "); }

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

printf("%2d", i); } printf("\n"); } }

44. /* PROGRAM TO MENU DRIVEN PROGRAM TO PERFORM THE OPERATIONS ADD, SUBTRACT, MULTIPLY, DIVIDE OF TWO COMPLEX NUMBERS X+IY, A+IB USING FUNCTIONS */

#include<stdio.h> void add(int x1, int y1, int a1, int b1) {

printf("\n The sum of the two complex numbers is %d+i%d", x1+a1, y1+b1); } void sub(int *x2, int *y2, int *a2, int *b2) { *x2 = *x2 - *a2; *y2 = *y2 - *b2; } void mult() {

sv ie tk an ur u. tk

int x, y, a, b; printf("\n Enter two numbers for the first complex number: "); scanf("%d %d", &x, &y); printf("\n Enter two numbers for the second complex number: "); scanf("%d %d", &a, &b); printf("\n The Multiplication of the two complex numbers is %d+i%d", x*a - y*b, x*b + y*a); } void div() {

int x, y, a, b;

printf("\n Enter two numbers for the first complex number: "); scanf("%d %d", &x, &y);

printf("\n Enter two numbers for the second complex number: ");

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

printf("\n The Division of the two complex numbers is %d+i%d", (x*a + y*b)/(a*a + b*b), (y*a x*b)/(a*a + b*b)); } main() { int x, y, a, b, choice, ch, res; float res1; do {

printf("\n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Exit"); printf("\n Please Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("\n Enter two numbers for the first complex number: "); scanf("%d %d", &x, &y); printf("\n Enter two numbers for the second complex number: "); scanf("%d %d", &a, &b);

sv

ie

tk

an ur u. tk

add(x, y, a, b); break; case 2: printf("\n Enter two numbers for the first complex number: "); scanf("%d %d", &x, &y); printf("\n Enter two numbers for the second complex number: "); scanf("%d %d", &a, &b); sub(&x, &y, &a, &b); printf("\n The difference of the two complex numbers is %d+i%d", x, y); break;

case 3: mult(); break;

case 4: div();

case 5: return(0);

default: printf("\n Please enter correct choice"); }

printf("\n Do you want to continue....\n Press 1 for YES \n Press 2 for NO\n "); scanf("%d",&ch); }while(ch == 1); }

45. /* PROGRAM TO FIND THE SUM OF ALL ELEMENTS IN AN ARRAY BY USING FUNCTIONS WITH ARGUMENTS AND NO RETURN VALUES */ #include<stdio.h> main() { void arraysum(int [], int ); int a[10], n, i; printf("\n Please enter size of the array: "); scanf("%d", &n); printf("\n Please enter the values of the array \n"); for(i=0; i<n; i++) {

sv

ie
break;

tk

an

ur u. tk

scanf("%d",&a[i]); } printf("\n The array elements are...."); for(i=0; i<n; i++) { printf("\t%d",a[i]); } arraysum(a,n); }

void arraysum(int b[], int size) {

int i, sum = 0; for(i=0; i<size; i++) { sum = sum + b[i]; }

printf("\n The sum of all the elements in the array is %d\n", sum); }

46. /* PROGRAM TO SWAP TWO NUMBER USING CALL BY REFERENCE CONCEPT */ #include<stdio.h> main() { void swap(int *, int *); int num1, num2; printf("\n Please enter two values that you want to swap \n"); scanf("%d %d", &num1,&num2); printf("\n The values before swapping are....%d and %d", num1, num2); swap(&num1, &num2); printf("\n The values before swapping are....%d and %d", num1, num2); } void swap(int *p1, int *p2) {

sv

ie tk an ur u. tk

*p1 = *p1 + *p2; *p2 = *p1 - *p2; *p1 = *p1 - *p2; } 47. /* PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER USING RECURSION */ #include<stdio.h> main() { int factorial(int ); int n; do {

printf("\n Please enter a number: ");

scanf("%d", &n); if(n<0)

printf("\n Sorry.... You can not enter a negative number");

sv ie tk an ur u. tk

}while(n<0);

printf("\n Factorial of %d is %d ", n, factorial(n)); } int factorial(int x) { if(x==0) return 1; else return x*factorial(x-1); }

48. /* PROGRAM TO CALCULATE THE FIBONACCI SERIES BY USING RECURSION */ #include<stdio.h> main() { int fib(int oldvalue, int newvalue, int no); int range, fib1 = 0,fib2 = 1;

printf("\nEnter the number of elements in the series: "); scanf("%d", &range); printf("%4d", fib1); printf("%4d", fib2); fib(fib1, fib2, range); } int fib(int old_val, int new_val, int n) { int sum; while (n!=2) {

sum = old_val + new_val; printf("%4d", sum); old_val = new_val; new_val = sum; n -= 1;

fib(old_val , new_val, n); return(0); } }

sv ie tk an ur u. tk

Potrebbero piacerti anche