1.Write a C program to find maximum and minimum of given set of numbers #include<stdio.h> int main() { int i, n, lar,sm, tot; printf ("Enter total number of elements \n"); scanf ("%d", &tot); printf ("Enter first number \n"); scanf ("%d", &n); lar = n; sm=n; for (i=1; i<= tot ; i++) { printf ("\n Enter another number \n"); scanf ("%d",&n); if (n>lar) lar=n; if (n<sm) sm=n; } printf ("\n The largest number is %d", lar); printf ("\n The smallest number is %d", sm); return 0; } Output: Enter total number of elements 4 Enter first number 5 Enter another number 7 Enter another number 1 Enter another number 10 The largest number is 10 The smallest number is 1 2.Write a C Program to find roots of quadratic equation The standard form of a quadratic equation is: ax2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0 The term b2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots. If discriminant is greater than 0, the roots are real and different. If discriminant is equal to 0, the roots are real and equal. If discriminant is less than 0, the roots are complex and different. #include <stdio.h> #include <math.h> int main() { double a, b, c, discriminant, root1, root2, real, imaginary; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf",&a, &b, &c); discriminant = b*b-4*a*c; // condition for real and different roots if (discriminant > 0) { // sqrt() function returns square root root1 = (-b+sqrt(discriminant))/(2*a); root2 = (-b-sqrt(discriminant))/(2*a); printf("root1 = %.2lf and root2 = %.2lf",root1 , root2); } //condition for real and equal roots else if (discriminant == 0) { root1 = root2 = -b/(2*a); printf("root1 = root2 = %.2lf;", root1); } // if roots are not real else { realPart = -b/(2*a); imaginaryPart = sqrt(-discriminant)/(2*a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", real, imaginary, real,imaginary); } return 0; } Output Enter coefficients a, b and c: 2.3 4 5.6 Roots are: -0.87+1.30i and -0.87-1.30i 3.Write a C program to convert a decimal number to a binary number #include <stdio.h> int main() { long num, decimal_num, remainder, base = 1, binary = 0; printf("Enter a decimal integer \n"); scanf("%ld", &num); decimal_num = num; while (num > 0) { remainder = num % 2; binary = binary + remainder * base; num = num / 2; base = base * 10; } printf("Input number is = %ld\n", decimal_num); printf("Its binary equivalent is = %ld\n", binary); } Output: Enter a decimal integer 10 Input number is = 10 Its binary equivalent is = 1010 Enter a decimal integer 10 Input number is = 10 Its binary equivalent is = 1010 4.Write a program in C to convert a decimal number to hexadecimal. Pictorial Presentation: #include <stdio.h> int main() { long int decnum,rmd,q,dn=0,m,l; int i=1,j,tmp; char s; printf("Input any Decimal number: "); scanf("%ld",&decnum); q = decnum; for(l=q;l>0;l=l/16) { tmp = l % 16; if( tmp < 10) tmp =tmp + 48; else tmp = tmp + 55; dn=dn*100+tmp; } printf("\nThe equivalent Hexadecimal Number : "); for(m=dn;m>0;m=m/100) { s=m % 100; printf("%c",s); } return 0; } Output: Input any Decimal number: 79 The equivalent Hexadecimal Number : 4F 5. C program to Convert Decimal to Octal #include <stdio.h> int main() { long decimalnum, remainder, quotient; int octalNumber[100], i = 1, j; printf("Enter the decimal number: "); scanf("%ld", &decimalnum); quotient = decimalnum; while (quotient != 0) { octalNumber[i++] = quotient % 8; quotient = quotient / 8; } printf("Equivalent octal value of decimal no %d: ", decimalnum); for (j = i - 1; j > 0; j--) printf("%d", octalNumber[j]); return 0; } Output Enter a decimal number: 68 78 in decimal = 104 in octal Program Explanation 1. Take a decimal number as input and store it in the variable decimalnum. 2. Copy the variable decimalnum to the variable quotient. 3. Divide the variable quotient and obtain its remainder and quotient. Store the remainder in the array octalNumber and override the variable quotient with the quotient obtained. 4. Repeat the step 3 until the quotient becomes zero. 5. When it becomes zero, print the array octalNumber in the reverse order to get the output. 6. Write a c program to Generating Pascal triangle #include <stdio.h> int factorial(int n) { int f; for(f = 1; n > 1; n--) f *= n; return f; } int ncr(int n,int r) { return factorial(n) / ( factorial(n-r) * factorial(r) ); } int main() { int n, i, j; n = 5; for(i = 0; i <= n; i++) { for(j = 0; j <= n-i; j++) printf(" "); for(j = 0; j <= i; j++) printf(" %3d", ncr(i, j)); printf("\n"); } return 0; } The output should look like this − 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 7.Write a C Program to Construct Pyramid of numbers #include<stdio.h> #include<conio.h> main() { int i,j,n; clrscr(); printf("enter n value\n"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=n-1;j>=i;j--) printf(" "); for(j=1;j<=i;j++) printf("%3d",i); for(j=i-1;j>=1;j--) printf("%3d",i); printf("\n"); } getch(); } Output: enter n value 4 1 222 33333 4444444 8. Find the sum of sin(x) series /* Write a C program to find the sum of sin(x) series */ Logic: 1. First the computer reads the value of x and limit from the user. 2. Now convert x to radian value x=x*(3.1415\180) 3. Then using for loop the value of sin(x) is calculated. 4. Finally the value of sin(x) is printed. 5. Program for sin(x) series in C #include <stdio.h> #include <math.h> int fac(int x) { int i,fac=1; for(i=1;i<=x;i++) fac=fac*i; return fac; } int main() { float x,Q,sum=0; int i,j,limit; printf("Enter the value of x of sinx series: "); scanf("%f",&x); printf("Enter the limit upto which you want to expand the series: "); scanf("%d",&limit); Q=x; x = x*(3.1415/180); for(i=1,j=1;i<=limit;i++,j=j+2) { if(i%2!=0) { sum=sum+pow(x,j)/fac(j); } else sum=sum-pow(x,j)/fac(j); } printf("Sin(%0.1f): %f",Q,sum); return 0; } Output Enter the value of x of sinx series: 40 Enter the limit upto which you want to expand the series: 5 Sin(40.0): 0.642772 9.Find the sum of cos(x) series /* Write a C program to find the sum of cos(x) series */ #include<stdio.h> #include<math.h> void main() { int n,x1,i,j; float x,sign,cosx,fact; printf("Enter the number of the terms in aseries\n"); scanf("%d",&n); printf("Enter the value of x(in degrees)\n"); scanf("%f",&x); x1=x; x=x*(3.142/180.0); /* Degrees to radians*/ cosx=1; sign=-1; for(i=2;i<=n;i=i+2) { fact=1; for(j=1;j<=i;j++) { fact=fact*j; } cosx=cosx+(pow(x,i)/fact)*sign; sign=sign*(-1); } printf("Sum of the cosine series = %7.2f\n",cosx); } Output Enter the number of the terms in aseries 5 Enter the value of x(in degrees) 60 Sum of the cosine series = 0.50 10. C program to find factorial of a number using recursion #include <stdio.h> int factorial(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %d", n, factorial(n)); return 0; } int factorial(int n) { if (n >= 1) return n*factorial(n-1); else return 1; } Output Enter a positive integer: 6 Factorial of 6 = 720 11.C program to print fibonacii series till n terms using recursion #include <stdio.h> void getFibonacii(int a,int b, int n) { int sum; if(n>0) { sum=a+b; printf("%d ",sum); a=b; b=sum; getFibonacii(a,b,n-1); } } int main() { int a,b,sum,n; int i; a=0; //first term b=1; //second term printf("Enter total number of terms: "); scanf("%d",&n); printf("Fibonacii series is : "); printf("%d\t%d\t",a,b); //print a and b as first and second terms of series getFibonacii(a,b,n-2); //call function with (n-2) terms printf("\n"); return 0; } Output Enter total number of terms: 5 Fibonacii series is : 0 1 1 2 3 12.gcd using recursion #include <stdio.h> int gcd(int, int); int main() { int a, b, result; printf("Enter the two numbers in descending order to find their GCD: "); scanf("%d%d", &a, &b); result = gcd(a, b); printf("The GCD of %d and %d is %d.\n", a, b, result); } int gcd(int a, int b) { while (a != b) { if (a > b) { return gcd(a - b, b); } else { return gcd(a, b - a); } } return a; } Output: Enter the two numbers in descending order to find their GCD: 100 70 The GCD of 100 and 70 is 10. 13.Write a C program for matrix addition using arrays #include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &second[c][d]); printf("Sum of entered matrices:-\n"); for (c = 0; c < m; c++) { for (d = 0 ; d < n; d++) { sum[c][d] = first[c][d] + second[c][d]; printf("%d\t", sum[c][d]); } printf("\n"); } return 0; } 14.Write a C program to matrix multiplication using arrays #include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter elements of first matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if (n != p) printf("The matrices can't be multiplied with each other.\n"); else { printf("Enter elements of second matrix\n"); for (c = 0; c < p; c++) for (d = 0; d < q; d++) scanf("%d", &second[c][d]); for (c = 0; c < m; c++) { for (d = 0; d < q; d++) { for (k = 0; k < p; k++) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } printf("Product of the matrices:\n"); for (c = 0; c < m; c++) { for (d = 0; d < q; d++) printf("%d\t", multiply[c][d]); printf("\n"); } } return 0; } 15.Write a C program that uses non recursive function to search for a Key value in a given list of integers using Linear search method. #include<stdio.h> int search(int[],int,int); int main() { int a[50],n,i,s,res; printf("Enter array size "); scanf("%d",&n); printf("Enter %d elements:\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter an element to search:"); scanf("%d",&s); res=search(a,n,s); if(res==-1) printf("Element not found"); else printf("Element found"); return 0; } int search(int x[],int n,int s) { int i; for(i=0;i<n;i++) { if(x[i]==s) return i; } return -1; } Output: Enter Array size 5 Enter 5 elements: 12 45 10 34 44 Enter an element to search:45 Element found 16.Write a C program that uses non recursive function to search for a Key value in a given sorted list of integers. Use binary search method. int binary(int[],int,int,int); void main() { int a[100],i,n,key,pos; printf("enter size"); scanf("%d",&n); printf("enter sorted elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter key"); scanf("%d",&key); pos=binary(a,0,n-1,key); if(pos==-1) printf("element is not found"); else printf("element is found at %d position",pos); } int binary(int a[],int low,int high,int key) { int mid; while(low<=high) { mid=(low+high)/2; if(key==a[mid]) return mid; else if(key>a[mid]) low=mid+1; else high=mid-1; } return -1; } Output: enter size 5 enter sorted elements 11 23 33 45 56 enter key 45 element is found at 3 position 17.Write a C program that uses recursive function to search for a Key value in a given list of integers using Binary search method. include<stdio.h> int bsearch(int[],int,int,int); int main() { int a[50],n,i,s,res; printf("How many elements?"); scanf("%d",&n); printf("Enter %d elements in order:\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter an element to search:"); scanf("%d",&s); res=bsearch(a,0,n-1,s); if(res==-1) printf("Element not found"); else printf("Element found"); return 0; } int bsearch(int x[],int start,int end,int s) { int mid; if(start>end) return -1; mid=(start+end)/2; if(s==x[mid]) return mid; if(s<x[mid]) mid=bsearch(x,start,mid-1,s); if(s>x[mid]) mid=bsearch(x,mid+1,end,s); return mid; } output Enter array size 8 Enter 8 elements: 10 25 37 45 55 62 72 85 Enter an element to search:72 Element found 18.Write a C program that uses recursive function to search for a Key value in a given list of integers using Linear search method. #include <stdio.h> int LinearSearch(int arr[],int search,int index,int n); int main() { int n, search, result, m = 0, arr[100]; printf("Enter the total elements in the array\n"); scanf("%d", &n); printf("Enter the array elements\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter the element to search \n"); scanf("%d", &search); result = LinearSearch(arr, search,0,n); if (result != 0) { printf("Element found at position %d\n ", result); } else { printf("Element not found"); } return 0; } int LinearSearch(int arr[], int search,int index,int n) { int arrpos=0; if(index>=n) { return 0; } if (arr[index] == search) { arrpos = index + 1; return arrpos; } else { return LinearSearch(arr, search,index+1,n); } return arrpos; } Output: Enter the total elements in the array 5 Enter the array elements 5 23 7 10 15 Enter the element to search 7 Element found at position 3 19.Write a C program to sort elements using bubble sort #include <stdio.h> int main() { int array[100], n, i, j, swap; printf("Enter number of elements\n"); scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &array[i]); for (i = 0 ; i < n - 1; i++) { for (j = 0 ; j < n - i - 1; j++) { if (array[j] > array[j+1]) { swap = array[j]; array[j] = array[j+1]; array[j+1] = swap; } } } printf("Sorted list in ascending order:\n"); for (i = 0; i < n; i++) printf("%j\n", array[i]); return 0; } 20.Write a C program to sort elements using selection sort #include <stdio.h> int main() { int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0; c < (n - 1); c++) { position = c; for (d = c + 1; d < n; d++) { if (array[position] > array[d]) position = d; } if (position != c) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n"); for (c = 0; c < n; c++) printf("%d\n", array[c]); return 0; } 21.C program to demonstrate pointer to arrays #include <stdio.h> int main() { int i; int a[5] = {1, 2, 3, 4, 5}; int *p = a; // same as int*p = &a[0] printf(“values stored in the array are\n”); for (i = 0; i < 5; i++) { printf("%d ", *p); p++; } return 0; } Output values stored in the array are 1 2 3 4 5 22.Write a C program to demonstrate pointers to function int sum (int num1, int num2) { return num1+num2; } int main() { int (*f2p) (int, int); //function pointer is declared f2p = sum; //pointer is pointing to function int op1 = f2p(10, 13); //Calling function using function pointer int op2 = sum(10, 13); //Calling function in normal way using function name printf("Output1: Call using function pointer: %d",op1); printf("\nOutput2: Call using function name: %d", op2); return 0; } Output: Output1: Call using function pointer: 23 Output2: Call using function name: 23 Some points regarding function pointer: 1. As mentioned in the comments, you can declare a function pointer and assign a function to it in a single statement like this: void (*fun_ptr)(int) = &fun; 2. You can even remove the ampersand from this statement because a function name alone represents the function address. This means the above statement can also be written like this: void (*fun_ptr)(int) = fun; 23.Write a c program to concatenate two strings #include <stdio.h> #include <string.h> int main () { char source[10]=”Morning”,; char destination[20]=”Good “; strcat(destination, source); printf(“source=%s”,source); printf(“Destination=%s”,destination); return(0); } Output: Source=Morning Destination=Good Morning 24.Write a C program to find length of the string #include <stdio.h> #include <string.h> int main () { char str[50] ="This is India"; int len; len = strlen(str); printf("Length of %s is %d\n", str, len); return(0); } Output: Length of This is India is 13 25.Write a C program to compare two strings #include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; strcpy(str1, "abcdef"); strcpy(str2, "ABCDEF"); ret = strcmp(str1, str2); if(ret < 0) { printf("str1 is less than str2"); } else if(ret > 0) { printf("str2 is less than str1"); } else { printf("str1 is equal to str2"); } return(0); } Output: str2 is less than str1 26.Write a C program to copy string #include <stdio.h> #include <string.h> int main () { char src[10]=”Morning”; char dest[10]; strcpy(dest, src); printf("Final copied string : %s\n", dest); return(0); } Output: Final copied string : Morning 27.C Program to read and print details of a student using Structure. #include <stdio.h> struct student { char name[50]; int roll; float marks; }; int main() { Struct student s; printf("Enter student details:\n"); printf("Enter name:"); scanf("%s", s.name); printf("Enter rollnumber:"); scanf("%d", &s.roll); printf("Enter marks:"); scanf("%f", &s.marks); printf("Student Details Are:\n"); printf("Name:"); printf(“%s\n”,s.name); printf("Roll number:%d\n",s.roll); printf("Marks:%f\n", s.marks); return 0; } Output: Enter student details Enter name:Sam Enter rollnumber:101 Enter marks:75.00 Students Details Are: Name:Sam Roll number:101 Marks:75.00 28.C Program to add two Complex Numbers using structures #include <stdio.h> Struct complex add(struct complex n1,struct complex n2); struct complex { float real; float imag; }; int main() { struct complex n1, n2, sum; printf(“Enter two complex numbers\n”); scanf("%f %f", &n1.real, &n1.imag); scanf("%f %f", &n2.real, &n2.imag); sum= add(n1, n2); printf("Sum:%.1f+%.1fi", sum.real, sum.imag); return 0; } Struct complex add(struct complex n1,struct complex n2) { complex temp; temp.real = n1.real + n2.real; temp.imag = n1.imag + n2.imag; return(temp); }
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-