Professional Documents
Culture Documents
C programming examples
Hello world Print Integer Addition Odd or Even Add, subtract, multiply and divide Check vowel Leap year Add digits Factorial HCF and LCM Decimal to binary conversion ncR and nPr Add n numbers Swapping Reverse number Palindrome number Print Pattern Diamond Prime numbers Find armstrong number Generate armstrong number Fibonacci series Print floyd's triangle Print pascal triangle Addition using pointers Maximum element in array Minimum element in array Linear search Binary search Reverse array Insert element in array Delete element from array Merge arrays Bubble sort Insertion sort Selection sort Add matrices Subtract matrices Transpose matrix Multiply two matrices Print string String length Compare strings Copy string Concatenate strings Reverse string Find palindrome Delete vowels C substring Sort a string Remove spaces Change case Swap strings Character's frequency Anagrams Read file Copy files Merge two files List files in a directory Delete file Random numbers Add complex numbers Print date Get IP address Shutdown computer
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
C programming code
#include <stdio.h> int main() { int a; printf("Enter an integer\n"); scanf("%d", &a); printf("Integer that you have entered is %d\n", a); return 0; }
Output of program:
C programming code
#include<stdio.h> int main() { int a, b, c; printf("Enter two numbers to add\n"); scanf("%d%d",&a,&b); c = a + b; printf("Sum of entered numbers = %d\n",c);
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
3
return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
4
printf("Even\n"); else printf("Odd\n"); return 0; }
We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7 (0111) when we perform 7 & 1 the result will be one and you may observe that the least significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also ( even_number & 1 ) is zero.
5
#include<stdio.h> main() { int n; printf("Enter an integer\n"); scanf("%d",&n); if ( (n/2)*2 == n ) printf("Even\n"); else printf("Odd\n"); return 0; }
In c programming language when we divide two integers we get an integer result, For example the result of 7/3 will be 2.So we can take advantage of this and may use it to find whether the number is odd or even. Consider an integer n we can first divide by 2 and then multiply it by 2 if the result is the original number then the number is even otherwise the number is odd. For example 11/2 = 5, 5*2 = 10 ( which is not equal to eleven), now consider 12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help you in finding if a number is odd or not.
C programming code
#include <stdio.h> int main() { int first, second, add, subtract, multiply; float divide; printf("Enter two integers\n"); scanf("%d%d", &first, &second); add = first subtract = first multiply = first * divide = first + second; second; second; / (float)second;
//typecasting
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also write float in numerator. This explicit conversion is known as typecasting.
C programming code
#include <stdio.h> int main() { int year; printf("Enter a year to check if it is a leap year\n"); scanf("%d", &year); if ( year%400 == 0)
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
7
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; }
Please read the leap year article at Wikipedia, it will help you to understand the program. This code is based on Gregorian Calendar.
Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses for loop, second uses a function to find factorial and third using recursion. Factorial is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Also n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
Recursion is a technique in which a function calls itself, for example in above code factorial function is calling itself. To solve a problem using recursion you must first express its solution in recursive form.
C programming code
#include <stdio.h> int main() { int a, b, x, y, t, gcd, lcm;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
10
gcd = a; lcm = (x*y)/gcd; printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd); printf("Least common multiple of %d and %d = %d\n", x, y, lcm); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
11
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
12
C programming code
#include <stdio.h> int main() { int n, c, k; printf("Enter an integer in decimal number system\n"); 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; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
13
Above code only prints binary of integer, but we may wish to perform operations on binary so in the code below we are storing the binary in a string. We create a function which returns a pointer to string which is the binary of the number passed as argument to the function.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
14
Memory is allocated dynamically because we can't return a pointer to a local variable (character array in this case). If we return a pointer to local variable then program may crash or we get incorrect result.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
15
int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }
C programming code
#include <stdio.h> int main() { int n, sum = 0, c, value; printf("Enter the number of integers you want to add\n"); scanf("%d", &n); printf("Enter %d integers\n",n); for (c = 1; c <= n; c++) { scanf("%d",&value); sum = sum + value; } printf("Sum of entered integers = %d\n",sum); return 0;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
16
}
17
long data type or long long data type if your compiler supports it, if you still have large numbers then use strings or other data structure.
C programming code
#include <stdio.h> int main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); return 0; }
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for palindrome number is given below.
18
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.
19
nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
* *** ***** ******* *********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.
C programming code
#include <stdio.h> int main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
20
Output of program:
For more patterns or shapes on numbers and characters see comments below and also see codes on following pages: Floyd triangle Pascal triangle Consider the pattern * ** *** **** ***** to print above pattern see the code below:
#include <stdio.h> int main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("*"); printf("\n"); } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
21
Using these examples you are in a better position to create your desired pattern for yourself. Creating a pattern involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.
C pattern programs
Pattern:
* *A* *A*A* *A*A*A*
Pattern:
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
22
1 232 34543 4567654 567898765
C program:
#include<stdio.h> main() { int n, c, d, num = 1, space; scanf("%d",&n); space = n - 1; for ( d = 1 ; d <= n ; d++ ) { num = d; for ( c = 1 ; c <= space ; c++ ) printf(" "); space--; for ( c = 1 ; c <= d ; c++ ) { printf("%d", num); num++; } num--; num--; for ( c = 1 ; c < d ; c++) { printf("%d", num); num--; } printf("\n"); } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
23
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
24
Output of program:
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
25
int n, result; printf("Enter an integer to check whether it is prime or not.\n"); scanf("%d",&n); result = check_prime(n); if ( result == 1 ) printf("%d is prime.\n", n); else printf("%d is not prime.\n", n); return 0; } int check_prime(int a) { int c; for ( c = 2 ; c <= a - 1 ; c++ ) { if ( a%c == 0 ) return 0; } if ( c == a ) return 1; }
There are many logic to check prime numbers, one given below is more efficient then above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
Only checking from 2 to square root of number is sufficient. There are many more efficient logic available.
C programming code
#include <stdio.h> int main() {
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
26
int number, sum = 0, temp, remainder; printf("Enter an integer\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10; sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number.\n"); else printf("Entered number is not an armstrong number.\n"); return 0; }
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 numbers 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.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
27
int n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); 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("%d\n",next); } return 0; }
Output of program:
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
28
for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }
Recursion method is less efficient as it involves function calls which uses stack, also there are chances of stack overflow if function is called frequently for calculating larger Fibonacci numbers.
C programming code
#include <stdio.h> int main() { int first, second, *p, *q, sum; printf("Enter two integers to add\n"); scanf("%d%d", &first, &second); p = &first; q = &second; sum = *p + *q; printf("Sum of entered numbers = %d\n",sum); return 0;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
29
}
C programming code
#include <stdio.h> int main() { int array[100], maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d", &size); printf("Enter %d integers\n", size); for (c = 0; c < size; c++) scanf("%d", &array[c]); maximum = array[0]; for (c = 1; c < size; c++) { if (array[c] > maximum) { maximum = array[c]; location = c+1; } } printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum); return 0;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
30
}
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
31
The complexity of above code is O(n) as the time used depends on the size of input array or in other words time to find maximum increases linearly as array size grows.
C programming code
#include <stdio.h> int main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
32
Output of program:
Our algorithm first assumes first element as minimum and then compare it with other elements if an element is smaller than it then it becomes the new minimum and this process is repeated till complete array is scanned.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
33
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.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
34
Output of program:
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
35
return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
36
printf("%d is present at location %d.\n", search, position+1); return 0; } int linear_search(int *pointer, int n, int find) { int c; for ( c = 0 ; c < n ; c++ ) { if ( *(pointer+c) == find ) return c; } return -1; }
Time required to search an element using linear search algorithm depends on size of list. In best case element is present at beginning of list and in worst case element is present at the end. Time complexity of linear search is O(n).
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
37
scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; 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; }
C program for linear search Download Binary search program. Output of program:
Binary search is faster than linear search but list should be sorted, hashing is faster than binary search and perform searches in constant time.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
38
C programming code
#include <stdio.h> int main() { int n, c, d, a[100], b[100]; printf("Enter the number of elements in array\n"); scanf("%d", &n); printf("Enter the array elements\n"); for (c = 0; c < n ; c++) scanf("%d", &a[c]); /* * Copying elements into array b starting from end of array a */ for (c = n - 1, d = 0; c >= 0; c--, d++) b[d] = a[c]; /* * Copying reversed array into original. * Here we are modifying original array, this is optional. */ for (c = 0; c < n; c++) a[c] = b[c]; printf("Reverse array is\n"); for (c = 0; c < n; c++) printf("%d\n", a[c]); return 0;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
39
}
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
40
return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
41
Array is passed to function and a new array is created and contents of passed array (in reverse order) are copied into it and finally contents of new array are copied into array passed to function.
C programming code
#include <stdio.h> int main() { int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for (c = 0; c <= n; c++) printf("%d\n", array[c]); return 0; }
42
Output of program:
C programming code
#include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n");
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
43
else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
44
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 < { for (d = 0 ; d { if (array[d] { swap array[d] array[d+1] } } } ( n - 1 ); c++) < n - c - 1; d++) > array[d+1]) /* For decreasing order use < */ = array[d]; = array[d+1]; = swap;
printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
45
You can also sort strings using Bubble sort, it is less efficient as its average and worst case complexity is high, there are many other fast sorting algorithms.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
46
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.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
47
Output of program:
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.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
48
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; }
49
matrices. For example if the user entered order as 2, 2 i.e. two rows and two columns and matrices as First Matrix :12 34 Second matrix :45 -1 5 then output of the program ( sum of First and Second matrix ) will be 57 29
C programming code
#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]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t", sum[c][d]); printf("\n"); } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
50
Subtract matrices
C code to subtract matrices of any order. This program finds difference between corresponding elements of two matrices and then print the resultant matrix.
C programming code
#include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], difference[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]); for (c = 0; c < m; c++) for (d = 0; d < n; d++) difference[c][d] = first[c][d] - second[c][d];
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
51
printf("difference of entered matrices:-\n"); for (c = 0; c < m; c++) { for (d = 0; d < n; d++) printf("%d\t",difference[c][d]); printf("\n"); } return 0; }
C programming code
#include <stdio.h>
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
52
int main() { int m, n, c, d, matrix[10][10], transpose[10][10]; printf("Enter the number of rows and columns of matrix "); scanf("%d%d",&m,&n); printf("Enter the elements of matrix \n"); for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) { scanf("%d",&matrix[c][d]); } } for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) { transpose[d][c] = matrix[c][d]; } } printf("Transpose of entered matrix :-\n"); for( c = 0 ; c < n ; c++ ) { for( d = 0 ; d < m ; d++ ) { printf("%d\t",transpose[c][d]); } printf("\n"); } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
53
Output of program:
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 can't be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
54
for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = { for ( d { for ( { sum } 0 ; c < m ; c++ ) = 0 ; d < q ; d++ ) k = 0 ; k < p ; k++ ) = 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("%d\t", multiply[c][d]); printf("\n"); } } return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
55
Matrices are frequently used while doing programming and are used to represent graph data structure, in solving system of linear equations and many more. Lot of research is being done on how to multiply matrices using minimum number of operations. You can also implement it using pointers.
C programming code
#include <stdio.h> int main() { char array[20] = "Hello World"; printf("%s\n",array); return 0; }
C programming code
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
56
#include <stdio.h> int main() { char array[100]; printf("Enter a string\n"); scanf("%s", array); printf("You entered the string %s\n",array); return 0; }
Note that scanf can only input single word strings, to receive strings containing spaces use gets function.
String length
This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is not counted when calculating string length. To find string length we use strlen function of string.h.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
57
You can also find string length using pointer or without strlen function. Following program shows how to achieve this.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
58
while(*(s+c)) c++; return c; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
59
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
60
{ if ( *first == '\0' || *second == '\0' ) break; first++; second++; } if( *first == '\0' && *second == '\0' ) return 0; else return -1; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
61
printf("Enter source string\n"); gets(source); copy_string(target, source); printf("Target string is \"%s\"\n", target); return 0; } void copy_string(char *target, char *source) { while(*source) { *target = *source; source++; target++; } *target = '\0'; }
C programming code
#include <stdio.h> #include <string.h> int main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); strcat(a,b); printf("String obtained on concatenation is %s\n",a); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
62
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
63
Reverse string
This program reverses a string entered by the user. For example if a user enters a string "reverse me" then on reversing the string will be "em esrever". We show you three different methods to reverse string the first one uses strrev library function of string.h header file and in second we make our own function to reverse string using pointers, reverse string using recursion and Reverse words in string. If you are using first method then you must include string.h in your program.
C programming code
/* String reverse in c*/
#include <stdio.h> #include <string.h> int main() { char arr[100]; printf("Enter a string to reverse\n"); gets(arr); strrev(arr); printf("Reverse of entered string is \n%s\n",arr); return 0; }
/* Second method */
64
#include<stdio.h> int string_length(char*); void reverse(char*); main() { char string[100]; printf("Enter a string\n"); gets(string); reverse(string); printf("Reverse of entered string is \"%s\".\n", string); return 0; } void reverse(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = { temp = *end = *begin 0 ; c < length/2 ; c++ ) *end; *begin; = temp;
begin++; end--; } } int string_length(char *pointer) { int c = 0; while( *(pointer+c) != '\0' ) c++; return c; }
65
#include <stdio.h> #include <string.h> void reverse(char*, int, int); int main() { char a[100]; gets(a); reverse(a, 0, strlen(a)-1); printf("%s\n",a); return 0; } void reverse(char *x, int begin, int end) { char c; if (begin >= end) return; c = *(x+begin); *(x+begin) = *(x+end); *(x+end) = c; reverse(x, ++begin, --end); }
In recursion method we swap characters at the begin and at the end and then move towards the middle of the string. This method is inefficient due to repeated function calls but useful in practicing recursion.
66
#include <stdio.h> #include <string.h> int main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); return 0; }
Palindrome number in c
#include <stdio.h> main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
67
if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
68
char string[100]; int result; printf("Enter a string\n"); gets(string); result = is_palindrome(string); if ( result == 1 ) printf("\"%s\" is a palindrome string.\n", string); else printf("\"%s\" is not a palindrome string.\n", string); return 0; } int is_palindrome(char *string) { int check, length; char *reverse; length = string_length(string); reverse = (char*)malloc(length+1); copy_string(reverse, string); reverse_string(reverse); check = compare_string(string, reverse); free(reverse); if ( check == 0 ) return 1; else return 0; } int string_length(char *string) { int length = 0; while(*string) { length++; string++; } return length; } void copy_string(char *target, char *source) { while(*source) { *target = *source;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
69
source++; target++; } *target = '\0'; } void reverse_string(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = { temp = *end = *begin 0 ; c < length/2 ; c++ ) *end; *begin; = temp;
begin++; end--; } } int compare_string(char *first, char *second) { while(*first==*second) { if ( *first == '\0' || *second == '\0' ) break; first++; second++; } if( *first == '\0' && *second == '\0' ) return 0; else return -1; }
Pointers are used in functions, you can develop your code without using pointers.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
70
C programming code
#include <stdio.h> #include <stdlib.h> #include <string.h> void sort_string(char*); int main() { char string[100]; printf("Enter some text\n"); gets(string); sort_string(string); printf("%s\n", string); return 0; } void sort_string(char *s) { int c, d = 0, length; char *pointer, *result, ch; length = strlen(s); result = (char*)malloc(length+1); pointer = s; for ( ch = 'a' ; ch <= 'z' ; ch++ ) { for ( c = 0 ; c < length ; c++ ) { if ( *pointer == ch ) { *(result+d) = *pointer; d++; } pointer++; } pointer = s; } *(result+d) = '\0';
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
71
strlwr, strupr in c
Here we will change string case with and without strlwr, strupr functions.
strlwr in c
#include <stdio.h> #include <string.h> int main() { char string[] = "Strlwr in C"; printf("%s\n",strlwr(string)); return } 0;
strupr in c
#include <stdio.h> #include <string.h> int main() { char string[] = "strupr in c"; printf("%s\n",strupr(string)); return } 0;
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
72
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
73
{ while(*string) { if ( *string >= 'A' && *string <= 'Z' ) { *string = *string + 32; } string++; } }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
74
fclose(fp); return 0; }
There are blank lines present at end of file. In our program we have opened only one file but you can open multiple files in a single program and in different modes as desired. File handling is very important when we wish to store data permanently on a storage device. All variables and data of program is lost when program exits so if that data is required later we need to use files.
C programming code
#include <stdio.h> #include <stdlib.h> int main() { char ch, source_file[20], target_file[20]; FILE *source, *target; printf("Enter name of file to copy\n"); gets(source_file); source = fopen(source_file, "r"); if( source == NULL ) {
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
75
printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } printf("Enter name of target file\n"); gets(target_file); target = fopen(target_file, "w"); if( target == NULL ) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while( ( ch = fgetc(source) ) != EOF ) fputc(ch, target); printf("File copied successfully.\n"); fclose(source); fclose(target); return 0; }
C programming code
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
76
#include <stdio.h> #include <stdlib.h> int main() { FILE *fs1, *fs2, *ft; char ch, file1[20], file2[20], file3[20]; printf("Enter name of first file\n"); gets(file1); printf("Enter name of second file\n"); gets(file2); printf("Enter name of file which will store contents of two files\n"); gets(file3); fs1 = fopen(file1,"r"); fs2 = fopen(file2,"r"); if( fs1 == NULL || fs2 == NULL ) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } ft = fopen(file3,"w"); if( ft == NULL ) { perror("Error "); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while( ( ch = fgetc(fs1) ) != EOF ) fputc(ch,ft); while( ( ch = fgetc(fs2) ) != EOF ) fputc(ch,ft); printf("Two files were merged into %s file successfully.\n",file3); fclose(fs1); fclose(fs2); fclose(ft); return 0; }
77
Output of program:
Obviously you will get a different output when you will execute this file on your computer.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
78
C programming code
#include<stdio.h> main() { int status; char file_name[25]; printf("Enter the name of file you wish to delete\n"); gets(file_name); status = remove(file_name); if( status == 0 ) printf("%s file deleted successfully.\n",file_name); else { printf("Unable to delete the file\n"); perror("Error"); } return 0; }
Deleted file doesn't go to trash or recycle bin so you may not be able to recover it. Deleted files can be recovered using special recovery software if the files are not overwritten on the storage medium.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
79
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
80
getch(); return 0; }
C programming code
#include <stdio.h> struct complex { int real, img; }; int main() { struct complex a, b, c; printf("Enter a and b where a + ib is the first complex number.\n"); printf("a = "); scanf("%d", &a.real); printf("b = "); scanf("%d", &a.img); printf("Enter c and d where c + id is the second complex number.\n"); printf("c = "); scanf("%d", &b.real); printf("d = "); scanf("%d", &b.img); c.real = a.real + b.real; c.img = a.img + b.img; if ( c.img >= 0 ) printf("Sum of two complex numbers = %d + %di\n",c.real,c.img); else printf("Sum of two complex numbers = %d %di\n",c.real,c.img); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
81
Download add complex numbers program executable. Output of c program to add two complex numbers is shown in image below:
This code works in Turbo C only because it supports dos.h header file.
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
82
To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart use /r instead of /s.
You need to be logged in as root user for above program to execute otherwise you will get the message shutdown: Need to be root, now specifies that you want to shutdown immediately. '-P' option specifies you want to power off your machine. You can specify minutes as: shutdown -P "number of minutes" For more help or options type at terminal: man shutdown.
83
execute program from folder, it may not work when you are working in compiler and press Ctrl+F9 to run your program.
C programming code
#include<stdlib.h> int main() { system("C:\\Windows\\System32\\ipconfig"); return 0; }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
84
Pascal triangle in c
#include <stdio.h> long factorial(int); int main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } return 0; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
85
Output of program:
For more patterns or shapes on numbers and characters see codes on following pages:
86
//Main Function int main() { // Variable Declaration char ch; //Get Input Value printf("Enter the Vowel (In Capital Letter):"); scanf("%c",&ch); //Switch Case Check switch( ch ) { case 'A' : printf( "Your Character Is A\n" ); break; case 'E' : printf( "Your Character Is E\n" ); break; case 'I' : printf( "Your Character Is I\n" ); break; case 'O' : printf( "Your Character Is O\n" ); break; case 'U' : printf( "Your Character Is U\n" ); break; default : printf( "Your Character is Not Vowel.Otherwise Not a Capital Letter\n" ); break; } // Wait For Output Screen getch(); //Main Function return Statement return 0; }
Sample Output:
Enter the Vowel (In Capital Letter):A Your Character Is A Enter the Vowel (In Capital Letter):O Your Character Is O Enter the Vowel (In Capital Letter):h
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN
87
C PROGRAMMING CODE FOR MCA & MSC CST- SRM ARTS & SCIENCE COLLEGE COMPILED BY R.KARTHIKEYAN