Upcoming SlideShare
×

# All important c programby makhan kumbhkar

1,007 views

Published on

All important c programby makhan kumbhkar

Published in: Education, Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,007
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
56
0
Likes
1
Embeds 0
No embeds

No notes for slide

### All important c programby makhan kumbhkar

1. 1. C programming codes Prof Makhan kumbhkar Christian Eminent College Indore(mp)
2. 2. Leap year #include <stdio.h> main() { int year; printf("Enter a year to check if it is a leap yearn"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.n", year); else if ( year%100 == 0) printf("%d is not a leap year.n", year); else if ( year%4 == 0 ) printf("%d is a leap year.n", year); else printf("%d is not a leap year.n", year); return 0; }
3. 3. add digits of number in c #include <stdio.h> main() { int n, sum = 0, remainder; printf("Enter an integern"); scanf("%d",&n); while(n != 0) { remainder = n % 10; sum = sum + remainder; n = n / 10; } printf("Sum of digits of entered number = %dn",sum); return 0; }
4. 4. Decimal to binary conversion #include <stdio.h> main() { int n, c, k; printf("Enter an integer in decimal number systemn"); scanf("%d",&n); printf("%d in binary number system is:n", n); for ( c = 31 ; c >= 0 ; c-- ) { k = n >> c; if ( k & 1 ) printf("1"); else printf("0"); } printf("n"); return 0; }
5. 5. C code to store decimal to binary conversion in astring #include <stdio.h> #include <stdlib.h> char *decimal_to_binary(int); main() { int n, c, k; char *pointer; printf("Enter an integer in decimal number systemn"); scanf("%d",&n); pointer = decimal_to_binary(n); printf("Binary string of %d is: %sn", n, t); free(pointer); return 0; } char *decimal_to_binary(int n) {
6. 6. C code to store decimal to binary conversion in astring int c, d, count; char *pointer; count = 0; pointer = (char*)malloc(32+1); if ( pointer == NULL ) exit(EXIT_FAILURE); for ( c = 31 ; c >= 0 ; c-- ) { d = n >> c; if ( d & 1 ) *(pointer+count) = 1 + 0; else *(pointer+count) = 0 + 0; count++; } *(pointer+count) = 0; return pointer; }
7. 7. Palindrome Numbers Palindrome number algorithm 1. Get the number from user. 2. Reverse it. 3. Compare it with the number entered by the user. 4. If both are same then print palindrome number 5. Else print not a palindrome number.
8. 8. Palindrome number program c #include<stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or notn"); scanf("%d",&n); temp = n; while( temp != 0 )
9. 9. Palindrome number program c { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.n", n); else printf("%d is not a palindrome number.n", n); return 0; }
10. 10. * *** ***** *** * printf("Enter number of rowsn"); scanf("%d", &n); space = n - 1; for (k = 1; k <= n; k++) { for (c = 1; c <= space; c++) printf(" "); space--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("n"); }
11. 11. Fibonacci series in c Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can print as many number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.
12. 12. * *** ***** *** * space = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= space; c++) printf(" "); space++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("n"); } return 0; }
13. 13. Fibonacci series in c #include<stdio.h> main() { int n, first = 0, second = 1, next, c; printf("Enter the number of termsn"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf("%dn",next); } return 0; }
14. 14. Linear search in c Linear search in c programming: The following code implements linear search ( Searching algorithm ) which is used to find whether a given number is present in an array and if it is present then at what location it occurs.It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends. Linear search in c language for multiple occurrences and using function.
15. 15. linear search in c #include<stdio.h> main() { int array[100], search, c, number; printf("Enter the number of elements in arrayn"); scanf("%d",&number); printf("Enter %d numbersn", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to searchn");
16. 16. linear search in c scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) { if ( array[c] == search ) /* if required element found */ { printf("%d is present at location %d.n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.n", search); return 0; }
17. 17. C program for binary search C program for binary search: This code implements binary search in c language. It can only be used for sorted arrays, but its fast as compared to linear search. If you wish to use binary search on an array which is not sorted then you must sort it using some sorting technique say merge sort and then use binary search algorithm to find the desired element in the list. If the element to be searched is found then its position is printed.
18. 18. C program for binary search #include<stdio.h> main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integersn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to findn"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2;
19. 19. c program to insert an element in an array This code will insert an element into an array, For example consider an array a[10] having three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step below so after insertion a[1] = 1 which was a[0] initially, and a[2] = 2 and a[3] = 3. Array insertion does not mean increasing its size i.e array will not be containing 11 elements.
20. 20. c program to insert an element in an array #include <stdio.h> main() { int array[100], position, c, n, value; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an elementn"); scanf("%d", &position);
21. 21. c program to insert an element in an array printf("Enter the value to insertn"); scanf("%d", &value); for ( c = n - 1 ; c >= position - 1 ; c-- ) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array isn"); for( c = 0 ; c <= n ; c++ ) printf("%dn", array[c]); return 0; }
22. 22. C program for binary search while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.n", search); return 0; }
23. 23. c program for bubble sort: c program for bubble sort: c programming code for bubble sort to sort numbers or arrange them in ascending order. You can easily modify it to print numbers in descending order.
24. 24. c program for bubble sort /* Bubble sort code */ #include<stdio.h> main() { int array[100], n, c, d, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) {
25. 25. c program for bubble sort for ( d = 0 ; d < n - c - 1 ; d++ ) { if ( array[d] > array[d+1] ) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:n"); for ( c = 0 ; c < n ; c++ ) printf("%dn", array[c]); return 0; }
26. 26. insertion sort in c Insertion sort in c: c program for insertion sort to sort numbers. This code implements insertion sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
27. 27. insertion sort in c * insertion sort ascending order */ #include <stdio.h> int main() { int n, array[1000], c, d, t; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } for (c = 1 ; c <= n - 1; c++) {
28. 28. insertion sort in c d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:n"); for (c = 0; c <= n - 1; c++) { printf("%dn", array[c]); } return 0; }
29. 29. selection sort in c Selection sort in c: c program for selection sort to sort numbers. This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
30. 30. selection sort in c #include<stdio.h> main() { int array[100], n, c, d, position, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", 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++ )
31. 31. selection sort in c { 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("%dn", array[c]); return 0; }
32. 32. Matrix multiplication in c Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they cant be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics. Matrices are frequently used while doing programming and are used to represent graph data structure, in solving system of linear equations and many more.
33. 33. Matrix multiplication in c #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 the number of rows and columns of first matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrixn"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders cant be multiplied with each other.n"); else { printf("Enter the elements of second matrixn"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]);
34. 34. Matrix multiplication in c 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 entered matrices:-n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%dt", multiply[c][d]); printf("n"); } } return 0; }