C Program
C Program
1. #include <stdio.h>
2. int main()
3. {
4. int array[100], position, c, n, value;
5.
6. printf("Enter number of elements in array\n");
7. scanf("%d", &n);
8.
9. printf("Enter %d elements\n", n);
10.
11. for (c = 0; c < n; c++)
12. scanf("%d", &array[c]);
13.
14. printf("Enter the location where you wish to insert an element\n");
15. scanf("%d", &position);
16.
17. printf("Enter the value to insert\n");
18. scanf("%d", &value);
19.
20. for (c = n - 1; c >= position - 1; c--)
21. array[c+1] = array[c];
22.
23. array[position-1] = value;
24.
25. printf("Resultant array is\n");
26.
27. for (c = 0; c <= n; c++)
28. printf("%d\n", array[c]);
29.
30. return 0;
31. }
DELETE ELEMENT IN AN ARRAY
1. #include<stdio.h>
2. int main(){
3. int a[10],b[10],c[10],i;
4. printf("Enter First array->");
5. for(i=0;i<10;i++)
6. scanf("%d",&a[i]);
7. printf("\nEnter Second array->");
8. for(i=0;i<10;i++)
9. scanf("%d",&b[i]);
10. printf("Arrays before swapping");
11. printf("\nFirst array->");
12. for(i=0;i<10;i++){
13. printf("%d",a[i]);
14. }
15. printf("\nSecond array->");
16. for(i=0;i<10;i++){
17. printf("%d",b[i]);
18. }
19. for(i=0;i<10;i++){
20. //write any swapping technique
21. c[i]=a[i];
22. a[i]=b[i];
23. b[i]=c[i];
24. }
25. printf("\nArrays after swapping");
26. printf("\nFirst array->");
27. for(i=0;i<10;i++){
28. printf("%d",a[i]);
29. }
30. printf("\nSecond array->");
31. for(i=0;i<10;i++){
32. printf("%d",b[i]);
33. }
34. return0;
35. }
C Program to Sort the Array in an Ascending Order
This program will implement a one-dimentional array of some fixed size, filled with some
random numbers, then will sort all the filled elements of the array.
Problem Solution
Here is source code of the C program to sort the array in an ascending order. The program is
successfully compiled and tested using Turbo C compiler in windows environment. The
program output is also shown below.
1.
2. /*
3. * C program to accept N numbers and arrange them in an ascending order
4. */
5.
6. #include <stdio.h>
7. void main()
8. {
9.
10. int i, j, a, n, number[30];
11. printf("Enter the value of N \n");
12. scanf("%d",&n);
13.
14. printf("Enter the numbers \n");
15. for(i =0; i < n;++i)
16. scanf("%d",&number[i]);
17.
18. for(i =0; i < n;++i)
19. {
20.
21. for(j = i +1; j < n;++j)
22. {
23.
24. if(number[i]> number[j])
25. {
26.
27. a = number[i];
28. number[i]= number[j];
29. number[j]= a;
30.
31. }
32.
33. }
34.
35. }
36.
37. printf("The numbers arranged in ascending order are given below \n");
38. for(i =0; i < n;++i)
39. printf("%d\n", number[i]);
40.
41. }
Program Explanation
1. #include <stdio.h>
2.
3. int main()
4. {
5.
6. int array[50], size, i, largest;
7.
8. printf("\n Enter the size of the array: ");
9. scanf("%d",&size);
10.
11. printf("\n Enter %d elements of the array: ", size);
12.
13. for(i =0; i < size; i++)
14. scanf("%d",&array[i]);
15.
16. largest = array[0];
17.
18. for(i =1; i < size; i++)
19. {
20. if(largest < array[i])
21. largest = array[i];
22. }
23.
24. printf("\n largest element present in the given array is : %d", largest);
25.
26. return0;
27.
28. }
C program for matrix addition:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int m, n, c, d, first[10][10], second[10][10], sum[10][10];
6.
7. printf("Enter the number of rows and columns of matrix\n");
8. scanf("%d%d", &m, &n);
9. printf("Enter the elements of first matrix\n");
10.
11. for (c = 0; c < m; c++)
12. for (d = 0; d < n; d++)
13. scanf("%d", &first[c][d]);
14.
15. printf("Enter the elements of second matrix\n");
16.
17. for (c = 0; c < m; c++)
18. for (d = 0 ; d < n; d++)
19. scanf("%d", &second[c][d]);
20.
21. printf("Sum of entered matrices:-\n");
22.
23. for (c = 0; c < m; c++) {
24. for (d = 0 ; d < n; d++) {
25. sum[c][d] = first[c][d] + second[c][d];
26. printf("%d\t", sum[c][d]);
27. }
28. printf("\n");
29. }
30.
31. return 0;
32. }
Matrix multiplication in C language
1. #include <stdio.h>
2.
3. int main()
4. {
5. int m, n, p, q, c, d, k, sum = 0;
6. int first[10][10], second[10][10], multiply[10][10]
;
7.
8. printf("Enter number of rows and columns of first
matrix\n");
9. scanf("%d%d", &m, &n);
10. printf("Enter elements of first matrix\n");
11.
12. for (c = 0; c < m; c++)
13. for (d = 0; d < n; d++)
14. scanf("%d", &first[c][d]);
15.
16. printf("Enter number of rows and columns of
second matrix\n");
17. scanf("%d%d", &p, &q);
18.
19. if (n != p) //COLUMNS OF FIRST MATRIX =ROWS OF
SECOND MATRIX..(CONDITION FOR MATRIX MUL)//
20. printf("The matrices can't be multiplied with
each other.\n");
21. else
22. {
23. printf("Enter elements of second matrix\n");
24.
25. for (c = 0; c < p; c++)
26. for (d = 0; d < q; d++)
27. scanf("%d", &second[c][d]);
28.
29. for (c = 0; c < m; c++) {
30. for (d = 0; d < q; d++) {
31. for (k = 0; k < p; k++) {
32. sum = sum + first[c][k]*second[k][d];
33. }
34.
35. multiply[c][d] = sum;
36. sum = 0;
37. }
38. }
39.
40. printf("Product of the matrices:\n");
41.
42. for (c = 0; c < m; c++) {
43. for (d = 0; d < q; d++)
44. printf("%d\t", multiply[c][d]);
45.
46. printf("\n");
47. }
48. }
49.
50. return 0;
51. }
C program to find transpose of a matrix
1. #include <stdio.h>
2.
3. int main()
4. {
5. int m, n, c, d, matrix[10][10], transpose[10][10];
6.
7. printf("Enter the number of rows and columns of
matrix\n");
8. scanf("%d%d", &m, &n);
9.
10. printf("Enter elements of the matrix\n");
11.
12. for (c = 0; c < m; c++)
13. for(d = 0; d < n; d++)
14. scanf("%d", &matrix[c][d]);
15.
16. for (c = 0; c < m; c++)
17. for( d = 0 ; d < n ; d++ )
18. transpose[d][c] = matrix[c][d];
19.
20. printf("Transpose of the matrix:\n");
21.
22. for (c = 0; c < n; c++) {
23. for (d = 0; d < m; d++)
24. printf("%d\t", transpose[c][d]);
25. printf("\n");
26. }
27.
28. return 0;
29. }
C hello world program using character variables
1. #include <stdio.h>
2.
3. int main()
4. {
5. char a = 'H', b = 'e', c = 'l', d = 'o';
6. char e = 'w', f = 'r', g = 'd';
7.
8. printf("%c%c%c%c%c %c%c%c%c%c", a, b, c, c, d, e, d, f, c, g);
9.
10. return 0;
11. }
We may store "hello world" in a string (A character array) and then print it.
1. #include <stdio.h>
2.
3. int main()
4. {
5. char s[] = {'H','e','l','l','o','
','w','o','r','l','d','\0'};
6.
7. printf("%s\n", s);
8.
9. return 0;
10. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. while (1) // This is always true so the loop will execute forever
6. printf("Hello World\n");
7.
8. return 0;
9. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. printf("Hello world\n");
6. return 0;
7. }
C program to print an integer
1. #include <stdio.h>
2.
3. int main()
4. {
5. int a;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &a);
9.
10. printf("The integer is %d\n", a);
11.
12. return 0;
13. }
C program to print first hundred integers [1, 100] using a for loop:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c;
6.
7. for (c = 1; c <= 100; c++)
8. printf("%d ", c);
9.
10. return 0;
11. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. char ch;
6.
7. printf("Input a character\n");
8. scanf("%c", &ch);
9.
10. if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')) {
11.
if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I'
|| ch=='o' || ch=='O' || ch== 'u' ||ch=='U')
12. printf("%c is a vowel.\n", ch);
13. else
14. printf("%c is a consonant.\n", ch);
15. }
16. else
17. printf("%c is neither a vowel nor a consonant.\n", ch);
18.
19. return 0;
20. }
C program to check leap year
1.
2. #include <stdio.h>
3.
4. int main()
5. {
6. int year;
7.
8. printf("Enter a year to check if it is a leap year\n");
9. scanf("%d", &year);
10.
11. if (year%400 == 0) // Exactly divisible by 400 e.g. 1600,
2000
12. printf("%d is a leap year.\n", year);
13. else if (year%100 == 0) // Exactly divisible by 100 and not
by 400 e.g. 1900, 2100
14. printf("%d isn't a leap year.\n", year);
15. else if (year%4 == 0) // Exactly divisible by 4 and neither
by 100 nor 400 e.g. 2016, 2020
16. printf("%d is a leap year.\n", year);
17. else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018,
2019
18. printf("%d isn't a leap year.\n", year);
19.
20. return 0;
21. }
Sum of digits in C
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, t, sum = 0, remainder;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. t = n;
11.
12. while (t != 0)
13. {
14. remainder = t % 10;
15. sum = sum + remainder;
16. t = t / 10;
17. }
18.
19. printf("Sum of digits of %d = %d\n", n, sum);
20.
21. return 0;
22. }
Factorial program in C using a for loop
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c, n, fact = 1;
6.
7. printf("Enter a number to calculate its factorial\n");
8. scanf("%d", &n);
9.
10. for (c = 1; c <= n; c++)
11. fact = fact * c;
12.
13. printf("Factorial of %d = %d\n", n, fact);
14.
15. return 0;
16. }
1. #include <stdio.h>
2.
3. void swap(int*, int*); //Swap function declaration
4.
5. int main()
6. {
7. int x, y;
8.
9. printf("Enter the value of x and y\n");
10. scanf("%d%d",&x,&y);
11.
12. printf("Before Swapping\nx = %d\ny = %d\n", x, y);
13. swap(&x, &y);
14. printf("After Swapping\nx = %d\ny = %d\n", x, y);
15. return 0;
16. }
17. //Swap function definition
18. void swap(int *a, int *b)
19. {
20. int t;
21.
22. t = *b;
23. *b = *a;
24. *a = t;
25. }
C program to find reverse of a number
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, reverse = 0;
6.
7. printf("Enter a number to reverse\n");
8. scanf("%d", &n);
9.
10. while (n != 0)
11. {
12. reverse = reverse * 10;
13. reverse = reverse + n%10;
14. n = n/10;
15. }
16.
17. printf("Reverse of entered number is = %d\n", reverse);
18.
19. return 0;
20. }
*
***
*****
*******
*********
Pattern program in C
1. #include <stdio.h>
2. int main()
3. {
4. int row, c, n, s;
5. printf("Enter the number of rows in pyramid of stars you wish to
see\n");
6. scanf("%d", &n);
7. s = n;
8. for (row = 1; row <= n; row++) // Loop to print rows
9. {
10. for (c = 1; c < s; c++) // Loop to print spaces in a row
11. printf(" ");
12. s--;
13. for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a
row printf("*");
14. printf("\n");
15. }
16. return 0;
17. }
*
**
***
****
*****
Star pattern in C
1. #include <stdio.h>
2. int main()
3. {
4. int n, c, k;
5. printf("Enter number of rows\n");
6. scanf("%d", &n);
7. for (c = 1; c <= n; c++)
8. {
9. for(k = 1; k <= c; k++)
10. printf("*");
11.
12. printf("\n");
13. } return 0;
} *
*A*
*A*A*
*A*A*A*
1. #include<stdio.h>
2.
3. int main()
4. {
5. int n, c, k, space, count = 1;
6.
7. printf("Enter number of rows\n");
8. scanf("%d", &n);
9.
10. space = n;
11.
12. for (c = 1; c <= n; c++)
13. {
14. for (k = 1; k < space; k++)
15. printf(" ");
16.
17. for (k = 1; k <= c; k++)
18. {
19. printf("*");
20.
21. if (c > 1 && count < c)
22. {
23. printf("A");
24. count++;
25. }
26. }
27.
28. printf("\n");
29. space--;
30. count = 1;
31. }
32. return 0;
33. }
Pattern:
1
232
34543
4567654
567898765
C program:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, row, num = 1, space;
6.
7. scanf("%d", &n);
8.
9. space = n - 1;
10.
11. for (row = 1; row <= n; row++)
12. {
13. num = row;
14.
15. for (c = 1; c <= space; c++)
16. printf(" ");
17.
18. space--;
19.
20. for (c = 1; c <= row; c++)
21. {
22. printf("%d", num);
23. num++;
24. }
25.
26. num = num - 2;
27.
28. for (c = 1 ; c < row; c++)
29. {
30. printf("%d", num);
31. num--;
32. }
33.
34. printf("\n");
35. }
36.
37. return 0;
38. }
*
***
*****
***
*
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, k, space = 1;
6.
7. printf("Enter number of rows\n");
8. scanf("%d", &n);
9.
10. space = n - 1;
11.
12. for (k = 1; k <= n; k++)
13. {
14. for (c = 1; c <= space; c++)
15. printf(" ");
16.
17. space--;
18.
19. for (c = 1; c <= 2*k-1; c++)
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. space = 1;
26.
27. for (k = 1; k <= n - 1; k++)
28. {
29. for (c = 1; c <= space; c++)
30. printf(" ");
31.
32. space++;
33.
34. for (c = 1 ; c <= 2*(n-k)-1; c++)
35. printf("*");
36.
37. printf("\n");
38. }
39. return 0;
40. }
Prime number program in C language
1. #include<stdio.h>
2.
3. int main()
4. {
5. int n, i = 3, count, c;
6.
7. printf("Enter the number of prime numbers required\n");
8. scanf("%d",&n);
9.
10. if ( n >= 1 )
11. {
12. printf("First %d prime numbers are :\n",n);
13. printf("2\n");
14. }
15.
16. for ( count = 2 ; count <= n ; )
17. {
18. for ( c = 2 ; c <= i - 1 ; c++ )
19. {
20. if ( i%c == 0 )
21. break;
22. }
23. if ( c == i )
24. {
25. printf("%d\n", i);
26. count++;
27. }
28. i++;
29. }
30.
31. return 0;
32. }
C programming code
1. #include <stdio.h>
2.
3. int power(int, int);
4.
5. int main()
6. {
7. int n, sum = 0, temp, remainder, digits = 0;
8.
9. printf("Input an integer\n");
10. scanf("%d", &n);
11.
12. temp = n;
13. // Count number of digits
14. while (temp != 0) {
15. digits++;
16. temp = temp/10;
17. }
18.
19. temp = n;
20.
21. while (temp != 0) {
22. remainder = temp%10;
23. sum = sum + power(remainder, digits);
24. temp = temp/10;
25. }
26.
27. if (n == sum)
28. printf("%d is an Armstrong number.\n", n);
29. else
30. printf("%d isn't an Armstrong number.\n", n);
31.
32. return 0;
33. }
34.
35. int power(int n, int r) {
36. int c, p = 1;
37.
38. for (c = 1; c <= r; c++)
39. p = p*n;
40.
41. return p;
42. }
C program to generate and print armstrong
numbers
1. #include <stdio.h>
2.
3. int check_armstrong(int);
4. int power(int, int);
5.
6. int main () {
7. int c, a, b;
8.
9. printf("Input two integers\n");
10. scanf("%d%d", &a, &b);
11.
12. for (c = a; c <= b; c++) {
13. if (check_armstrong(c) == 1)
14. printf("%d\n", c);
15. }
16.
17. return 0;
18. }
19.
20. int check_armstrong(int n) {
21. long long sum = 0, temp;
22. int remainder, digits = 0;
23. temp = n;
24. while (temp != 0) {
25. digits++;
26. temp = temp/10;
27. }
28. temp = n;
29. while (temp != 0) {
30. remainder = temp%10;
31. sum = sum + power(remainder, digits);
32. temp = temp/10;
33. }
34. if (n == sum)
35. return 1;
36. else
37. return 0;
38. }
39. int power(int n, int r) {
40. int c, p = 1;
41. for (c = 1; c <= r; c++)
42. p = p*n;
43.
44. return p;
45. }
The first few numbers of the series are 0, 1, 1, 2, 3, 5, 8, ...,. Except for the first two terms of
the sequence, every other term is the sum of the previous two terms, for example, 8 = 3 + 5
(addition of 3 and 5).
1
23
456
7 8 9 10
It's clear that in Floyd's triangle, nth row contains n numbers.
C programming code
1. #include <stdio.h>
2. int main()
3. {
4. int n, i, c, a = 1;
5. printf("Enter the number of rows of Floyd's triangle to print\n");
6. scanf("%d", &n);
7. for (i = 1; i <= n; i++)
8. {
9. for (c = 1; c <= i; c++)
10. {
11. printf("%d ",a);
12. a++;
13. }
14. printf("\n");
15. }
16. return 0;
17. }
1
1 1
1 2 1
1 3 3 1
Pascal triangle in C
1. #include <stdio.h>
2.
3. long factorial(int);
4.
5. int main()
6. {
7. int i, n, c;
8.
9. printf("Enter the number of rows you wish to see in pascal
triangle\n");
10. scanf("%d",&n);
11.
12. for (i = 0; i < n; i++)
13. {
14. for (c = 0; c <= (n - i - 2); c++)
15. printf(" ");
16.
17. for (c = 0 ; c <= i; c++)
18. printf("%ld ",factorial(i)/(factorial(c)*factorial(i-
c)));
19.
20. printf("\n");
21. }
22.
23. return 0;
24. }
25.
26. long factorial(int n)
27. {
28. int c;
29. long result = 1;
30.
31. for (c = 1; c <= n; c++)
32. result = result*c;
33.
34. return result;
35. }
Linear search C program
1. #include <stdio.h>
2. int main()
3. { int array[100], search, c, n;
4. printf("Enter number of elements in array\n");
5. scanf("%d", &n);
6. printf("Enter %d integer(s)\n", n);
7. for (c = 0; c < n; c++)
8. scanf("%d", &array[c]);
9. printf("Enter a number to search\n");
10. scanf("%d", &search); for (c = 0; c < n; c++)
11. {
12. if (array[c] == search) /* Iffound*/
13. {
14. printf("%d is present at location %d.\n", search, c+1);
15. break;
16. } }
17. if (c == n)
18. printf("%d isn't present in the array.\n", search);
19. return 0;
20. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. char s[1000], r[1000];
6. int begin, end, count = 0;
7.
8. printf("Input a string\n");
9. gets(s);
10.
11. // Calculating string length
12.
13. while (s[count] != '\0')
14. count++;
15.
16. end = count - 1;
17.
18. for (begin = 0; begin < count; begin++) {
19. r[begin] = s[end];
20. end--;
21. }
22.
23. r[begin] = '\0';
24.
25. printf("%s\n", r);
26.
27. return 0;
28. }
Palindrome program in C language
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char a[100], b[100];
7.
8. printf("Enter a string to check if it is a palindrome\n");
9. gets(a);
10.
11. strcpy(b, a); // Copying input string
12. strrev(b); // Reversing the string
13.
14. if (strcmp(a, b) == 0) // Comparing input string with the
reverse string
15. printf("The string is a palindrome.\n");
16. else
17. printf("The string isn't a palindrome.\n");
18.
19. return 0;
20. }
Palindrome number in C
21. #include <stdio.h>
22.
23. int main()
24. {
25. int n, r = 0, t;
26.
27. printf("Enter an integer to check if it is palindrome or not\n");
28. scanf("%d", &n);
29.
30. t = n;
31.
32. while (t != 0)
33. {
34. r = r * 10;
35. r = r + t%10;
36. t = t/10;
37. }
38.
39. if (n == r)
40. printf("%d is a palindrome number.\n", n);
41. else
42. printf("%d isn't a palindrome number.\n", n);
43.
44. return 0;
45. }
C programming code
1. #include <stdio.h>
2. #include <string.h>
3. int check_vowel(char);
4. int main()
5. {
6. char s[100], t[100];
7. int c, d = 0;
8. printf("Enter a string to delete vowels\n");
9. gets(s);
10. for(c = 0; s[c] != '\0'; c++) {
11. if(check_vowel(s[c]) == 0) { // If not a vowel
12. t[d] = s[c];
13. d++;
14. }
15. }
16.
17. t[d] = '\0';
18.
19. strcpy(s, t); // We are changing initial string. This is
optional.
20.
21. printf("String after deleting vowels: %s\n", s);
22.
23. return 0;
24. }
25. int check_vowel(char ch)
26. {
27. if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch =
= 'i' || ch == 'I' || ch =='o' || ch=='O' || ch =='u' || ch == 'U')
28. return 1;
29. else
30. return 0;
31. }
C substring program
1. #include <stdio.h>
2. int main()
3. {
4. char string[1000], sub[1000];
5. int position, length, c = 0;
6. printf("Input a string\n");
7. gets(string);
8. printf("Enter the position and length of substring\n");
9. scanf("%d%d", &position, &length);
10. while (c < length) {
11. sub[c] = string[position+c-1];
12. c++;
13. }
14. sub[c] = '\0';
15. printf("Required substring is \"%s\"\n", sub);
16. return 0; }
C programming code
1. #include <stdio.h>
2. #include <string.h>
3. int check_subsequence (char [], char[]);
4. int main () {
5. int flag;
6. char s1[1000], s2[1000];
7. printf("Input first string\n");
8. gets(s1);
9. printf("Input second string\n");
10. gets(s2);
11. /** Passing smaller length string first */
12. if (strlen(s1) < strlen(s2))
13. flag = check_subsequence(s1, s2);
14. else
15. flag = check_subsequence(s2, s1);
16. if (flag)
17. printf("YES\n");
18. else
19. printf("NO\n");
20. return 0;
21. }
22. int check_subsequence (char a[], char b[]) {
23. int c, d;
24. c = d = 0;
25. while (a[c] != '\0') {
26. while ((a[c] != b[d]) && b[d] != '\0') {
27. d++;}
28. if (b[d] == '\0')
29. break;
30. d++;
31. c++;
32. }
33. if (a[c] == '\0')
34. return 1;
35. else
36. return 0;}
Function strupr in C
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[1000];
7.
8. printf("Input a string to convert to upper case\n");
9. gets(string);
10.
11. printf("The string in upper case:
%s\n", strupr(string));
12.
13. return 0;
14. }
1. #include <stdio.h>
2.
3. int main ()
4. {
5. int c = 0;
6. char ch, s[1000];
7.
8. printf("Input a string\n");
9. gets(s);
10. while (s[c] != '\0') {
11. ch = s[c];
12. if (ch >= 'A' && ch <= 'Z')
13. s[c] = s[c] + 32;
14. else if (ch >= 'a' && ch <= 'z')
15. s[c] = s[c] - 32;
16. c++;
17. }
18. printf("%s\n", s);
19. return 0;
20. }
1. Input a string
2. abcdefghijklmnopqrstuvwxyz{0123456789}ABCDEFGHIJKLMNOPQRS
TUVWXYZ
3. ABCDEFGHIJKLMNOPQRSTUVWXYZ{0123456789}abcdefghijklmnopqrs
tuvwxyz
C programming code
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[100];
7. int c = 0, count[26] = {0}, x;
8.
9. printf("Enter a string\n");
10. gets(string);
11.
12. while (string[c] != '\0') {
13. /** Considering characters from 'a' to 'z' only and
ignoring others. */
14.
15. if (string[c] >= 'a' && string[c] <= 'z') {
16. x = string[c] - 'a';
17. count[x]++;
18. }
19.
20. c++;
21. }
22.
23. for (c = 0; c < 26; c++)
24. printf("%c occurs %d times in the
string.\n", c + 'a', count[c]);
25.
26. return 0;
27. }
Anagram program in C
Anagram program in C: C program to check whether two strings are anagrams or
not, a string is assumed to consist of lower case alphabets only. Two words are said
to be anagrams of each other if the letters of one word can be rearranged to form the
other word. So, in anagram strings, all characters occur the same number of times.
For example, "abc" and "cab" are anagram strings, as every character 'a,' 'b,' and 'c'
occur the same number of times (one time here) in both the strings. A user will input
two strings, and our algorithm counts how many times each character ('a' to 'z')
appear in both the strings and then compare their corresponding counts. The
frequency of an alphabet in a string is how many times that alphabet appears in the
string. For example, the frequency of 'm' in the string "programming" is '2' as it is
present two times in "programming."
C anagram program
1. #include <stdio.h>
2.
3. int check_anagram(char [], char []);
4.
5. int main()
6. {
7. char a[100], b[100];
8.
9. printf("Enter a string\n");
10. gets(a);
11.
12. printf("Enter a string\n");
13. gets(b);
14.
15. if (check_anagram(a, b) == 1)
16. printf("The strings are anagrams.\n");
17. else
18. printf("The strings aren't anagrams.\n");
19.
20. return 0;
21. }
22.
23. int check_anagram(char a[], char b[])
24. {
25. int first[26] = {0}, second[26] = {0}, c=0;
26.
27. // Calculating frequency of characters of first string
28.
29. while (a[c] != '\0')
30. {
31. first[a[c]-'a']++;
32. c++;
33. }
34.
35. c = 0;
36.
37. while (b[c] != '\0')
38. {
39. second[b[c]-'a']++;
40. c++;
41. }
42.
43. // Comparing frequency of characters
44.
45. for (c = 0; c < 26; c++)
46. {
47. if (first[c] != second[c])
48. return 0;
49. }
50.
51. return 1;
52. }
File reading program in C
C programming code to open a file and print its contents on screen.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. char ch, file_name[25];
7. FILE *fp;
8.
9. printf("Enter name of a file you wish to see\n");
10. gets(file_name);
11.
12. fp = fopen(file_name, "r"); // read mode
13.
14. if (fp == NULL)
15. {
16. perror("Error while opening the file.\n");
17. exit(EXIT_FAILURE);
18. }
19.
20. printf("The contents of %s file are:\n", file_name);
21.
22. while((ch = fgetc(fp)) != EOF)
23. printf("%c", ch);
24.
25. fclose(fp);
26. return 0;
27. }
C program to copy a file: This program copies a file, firstly you will specify a file to
copy, and then you will enter name and extension of target file . We will open the file
that we wish to copy in "read" mode and target file in "write" mode.
C programming code
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. char ch, source_file[20], target_file[20];
7. FILE *source, *target;
8.
9. printf("Enter name of file to copy\n");
10. gets(source_file);
11.
12. source = fopen(source_file, "r");
13.
14. if (source == NULL)
15. {
16. printf("Press any key to exit...\n");
17. exit(EXIT_FAILURE);
18. }
19.
20. printf("Enter name of target file\n");
21. gets(target_file);
22.
23. target = fopen(target_file, "w");
24.
25. if (target == NULL)
26. {
27. fclose(source);
28. printf("Press any key to exit...\n");
29. exit(EXIT_FAILURE);
30. }
31.
32. while ((ch = fgetc(source)) != EOF)
33. fputc(ch, target);
34.
35. printf("File copied successfully.\n");
36.
37. fclose(source);
38. fclose(target);
39.
40. return 0;
41. }
C program to merge two files and store their contents in another file. The files which
are to be merged are opened in "read" mode and the file which contains content of
both the files is opened in "write" mode. To merge two files first we open a file and
read it character by character and store the read contents in the merged file then we
read the contents of another file and store it in merged file, we read two files until
EOF (end of file) is reached.
C programming code
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. FILE *fs1, *fs2, *ft;
7.
8. char ch, file1[20], file2[20], file3[20];
9.
10. printf("Enter name of first file\n");
11. gets(file1);
12.
13. printf("Enter name of second file\n");
14. gets(file2);
15.
16. printf("Enter name of file which will store contents of the
two files\n");
17. gets(file3);
18.
19. fs1 = fopen(file1, "r");
20. fs2 = fopen(file2, "r");
21. if(fs1 == NULL || fs2 == NULL)
22. {
23. perror("Error ");
24. printf("Press any key to exit...\n");
25. exit(EXIT_FAILURE);
26. }
27. ft = fopen(file3, "w"); // Opening in write mode
28. if(ft == NULL)
29. {
30. perror("Error ");
31. printf("Press any key to exit...\n");
32. exit(EXIT_FAILURE);
33. }
34. while((ch = fgetc(fs1)) != EOF)
35. fputc(ch,ft);
36. while((ch = fgetc(fs2)) != EOF)
37. fputc(ch,ft);
38. printf("The two files were merged into %s file
successfully.\n", file3);
39. fclose(fs1);
40. fclose(fs2);
41. fclose(ft);
42. return 0;
43. }
C program to list all files present in a directory/folder in which executable file of this
program is present. For example, if the executable file is present in C:\\TC\\BIN then
it will list all the files present in C:\\TC\\BIN.
Apparently, you will get a different output when you execute the program on your
computer.
C program to delete a file whose name a user will input, the file to be deleted must be
present in the directory in which the executable file of this program is present.
Extension of the file should also be entered, remove macro is used to delete the file. If
there is an error in deleting the file, then the error will be displayed by perror
function.
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int status;
6. char file_name[25];
7.
8. printf("Enter name of a file you wish to delete\n");
9. gets(file_name);
10.
11. status = remove(file_name);
12.
13. if (status == 0)
14. printf("%s file deleted successfully.\n", file_name);
15. else
16. {
17. printf("Unable to delete the file\n");
18. perror("Following error occurred");
19. }
20.
21. return 0;
22. }
In our program we print pseudo random numbers in range [0, 100]. So we calculate
rand() % 100 which will return a number in [0, 99] so we add 1 to get the desired
range.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main() {
5. int c, n;
6.
7. printf("Ten random numbers in [1,100]\n");
8.
9. for (c = 1; c <= 10; c++) {
10. n = rand() % 100 + 1;
11. printf("%d\n", n);
12. }
13.
14. return 0;
15. }
If you rerun this program, you will get the same set of numbers. To get different
numbers every time you can use: srand(unsigned int seed) function; here seed is an
unsigned integer. So you will need a different value of seed every time you run the
program for that you can use current time which will always be different so you will
get a different set of numbers. By default, seed = 1 if you do not use srand function.
C programming code
1. #include<stdlib.h>
2.
3. int main()
4. {
5. system("C:\\Windows\\System32\\ipconfig");
6.
7. return 0;
8. }
C program to shut down or turn off
computer
C program to shut down your computer: This program turns off, i.e., shut down your
computer system. System function of "stdlib.h" is used to run an executable file
shutdown.exe which is present in C:\WINDOWS\system32 folder in Windows 7 &
XP. See Windows XP and Linux programs at the bottom of this page.
You can use various options while executing shutdown.exe, for example, you can use
/t option to specify the number of seconds after which shutdown occurs.
Syntax: "shutdown /s /t x"; where x is the number of seconds after which shutdown
will occur.
By default, shutdown occurs after 30 seconds. To shutdown immediately you can
write "shutdown /s /t 0". If you wish to restart your computer, then you can use
"shutdown /r."
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. char ch;
7.
8. printf("Do you want to shutdown your computer now
(y/n)\n");
9. scanf("%c", &ch);
10.
11. if (ch == 'y' || ch == 'Y')
12. system("C:\\WINDOWS\\System32\\shutdown -s");
13.
14. return 0;
15. }
If you are using Turbo C Compiler then execute your program from command prompt
or by opening the executable file from the folder. Press F9 to build your executable
file from source program. When you run program from within the compiler by
pressing Ctrl+F9 it may not work.
You need to be logged in as root user for this program to execute otherwise you will
get the message shutdown: Need to be root, now specifies that you want to shut down
immediately. '-P' option specifies you want to power off your machine. You can
specify minutes as:
shutdown -P "number of minutes."