Sei sulla pagina 1di 60

INSERT ELEMENT IN AN ARRAY

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

32. #include <stdio.h>


33.
34. int main()
35. {
36. int array[100], position, c, n;
37.
38. printf("Enter number of elements in array\n");
39. scanf("%d", &n);
40.
41. printf("Enter %d elements\n", n);
42.
43. for (c = 0; c < n; c++)
44. scanf("%d", &array[c]);
45.
46. printf("Enter the location where you wish to delete element\n");
47. scanf("%d", &position);
48.
49. if (position >= n+1)
50. printf("Deletion not possible.\n");
51. else
52. {
53. for (c = position - 1; c < n - 1; c++)
54. array[c] = array[c+1];
55.
56. printf("Resultant array:\n");
57.
58. for (c = 0; c < n - 1; c++)
59. printf("%d\n", array[c]);
60. }
61.
62. return 0;
63. }
64.
SWAPPING ELEMENTS 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 is a C Program to sort an array in ascending order.


Problem Description

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

1. Create an array of fixed size (maximum capacity), lets say 10.


2. Take n, a variable which stores the number of elements of the array, less than maximum
capacity of array.
3. Iterate via for loop to take array elements as input, and print them.
4. The array elements are in unsorted fashion, to sort them, make a nested loop.
5. In the nested loop, the each element will be compared to all the elements below it.
6. In case the element is greater than the element present below it, then they are interchanged
7. After executing the nested loop, we will obtain an array in ascending order arranged
elements.
Program/Source Code

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. Declare an array of some fixed capacity, lets say 30.


2. From users, take a number N as input, which will indicate the number of elements in the
array (N <= maximum capacity)
3. Iterating through for loops (from [0 to N) ), take integers as input from user and print them.
These input are the elements of the array.
4. Now, create a nested for loop with i and j as iterators.
5. Start the sorting in ascending order by extracting each element at position i of outer loop.
6. This element is being compared to every element from position i+1 to size-1 (means all
elements present below this extracted element)
7. In case any of the extracted element is greater than the element below it, then these two
interchange their position, else the loop continues.
8. After this nested loop gets executed, we get all the elements of the array sorted in
ascending order.
LARGEST ELEMENT IN AN ARRAY

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. }

Displaying hello world indefinitely

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. }

Displaying hello world using printf

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. }

C program to store an integer in a string


1. #include <stdio.h>
2.
3. int main ()
4. {
5. char n[1000];
6.
7. printf("Input an integer\n");
8. scanf("%s", n);
9.
10. printf("%s", n);
11.
12. return 0;
13. }
Addition program in C
1. #include<stdio.h>
2.
3. int main()
4. {
5. int a, b, c;
6.
7. printf("Enter two numbers to add\n");
8. scanf("%d%d", &a, &b);
9.
10. c = a + b;
11.
12. printf("Sum of the numbers = %d\n", c);
13.
14. return 0;
15. }

C program to add two numbers repeatedly


1. #include <stdio.h>
2.
3. int main()
4. {
5. int a, b, c;
6. char ch;
7.
8. while (1) {
9. printf("Inut two integers\n");
10. scanf("%d%d", &a, &b);
11. getchar();
12.
13. c = a + b;
14.
15. printf("(%d) + (%d) = (%d)\n", a, b, c);
16.
17. printf("Do you wish to add more numbers (y/n)\n");
18. scanf("%c", &ch);
19.
20. if (ch == 'y' || ch == 'Y')
21. continue;
22. else
23. break;
24. }
25.
26. return 0;
27. }
Odd or even program in C using modulus operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. if (n%2 == 0)
11. printf("Even\n");
12. else
13. printf("Odd\n");
14.
15. return 0;
16. }

C program to find odd or even using bitwise


operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Input an integer\n");
8. scanf("%d", &n);
9.
10. if (n & 1 == 1)
11. printf("Odd\n");
12. else
13. printf("Even\n");
14. return 0;
15. }

C program to check odd or even without using


bitwise or modulus operator
16. int main()
17. {
18. int n;
19.
20. printf("Enter an integer\n");
21. scanf("%d", &n);
22. if ((n/2)*2 == n)
23. printf("Even\n");
24. else
25. printf("Odd\n");
26.
27. return 0;}
Check vowel using if else
1. #include <stdio.h>
2.
3. int main()
4. {
5. char ch;
6.
7. printf("Enter a character\n");
8. scanf("%c", &ch);
9.
10. // Checking both lower and upper case
11.
12. if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch =
= 'i' || ch == 'I' || ch =='o' || ch=='O' || ch =='u' || ch == 'U')
13. printf("%c is a vowel.\n", ch);
14. else
15. printf("%c isn't a vowel.\n", ch);
16.
17. return 0;
18. }

C program to check vowel or consonant using if


else
In this program we will check whether a character is a vowel or consonant or a
special character

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. }

C program to find HCF and LCM


1. #include <stdio.h>
2.
3. int main() {
4. int a, b, x, y, t, gcd, lcm;
5.
6. printf("Enter two integers\n");
7. scanf("%d%d", &x, &y);
8.
9. a = x;
10. b = y;
11.
12. while (b != 0) {
13. t = b;
14. b = a % b;
15. a = t;
16. }
17.
18. gcd = a;
19. lcm = (x*y)/gcd;
20.
21. printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
22. printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
23.
24. return 0;
25. }
C program to convert decimal to binary
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, k;
6.
7. printf("Enter an integer in decimal number system\n");
8. scanf("%d", &n);
9.
10. printf("%d in binary number system is:\n", n);
11.
12. for (c = 31; c >= 0; c--)
13. {
14. k = n >> c;
15.
16. if (k & 1)
17. printf("1");
18. else
19. printf("0");
20. }
21.
22. printf("\n");
23.
24. return 0;
25. }

C program to sum n numbers using a for loop


1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, sum = 0, c, value;
6.
7. printf("How many numbers you want to add?\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integers\n", n);
11.
12. for (c = 1; c <= n; c++)
13. {
14. scanf("%d", &value);
15. sum = sum + value;
16. }
17.
18. printf("Sum of the integers = %d\n", sum);
19.
20. return 0;
21. }
Swapping of two numbers in C
1. #include <stdio.h>
2.
3. int main()
4. {
5. int x, y, t;
6.
7. printf("Enter two integers\n");
8. scanf("%d%d", &x, &y);
9.
10. printf("Before Swapping\nFirst integer = %d\nSecond integer
= %d\n", x, y);
11.
12. t = x;
13. x = y;
14. y = t;
15.
16. printf("After Swapping\nFirst integer = %d\nSecond integer =
%d\n", x, y);
17.
18. return 0;
19. }

Swap function in C language


In this method we will make a function to swap numbers. We will use call by
reference.

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. }

C program for palindrome number


1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, reverse = 0, t;
6.
7. printf("Enter a number to check if it is a palindrome or not\n");
8. scanf("%d", &n);
9.
10. t = n;
11.
12. while (t != 0)
13. {
14. reverse = reverse * 10;
15. reverse = reverse + t%10;
16. t = t/10;
17. }
18.
19. if (n == reverse)
20. printf("%d is a palindrome number.\n", n);
21. else
22. printf("%d isn't a palindrome number.\n", n);
23.
24. return 0;
25. }

*
***
*****
*******
*********
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*

C pattern program of stars and alphabets:

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 program for prime number or not


1. #include<stdio.h>
2. main()
3. {
4. int n, c = 2;
5. printf("Enter a number to check if it is prime\n");
6. scanf("%d",&n);
7. for ( c = 2 ; c <= n - 1 ; c++ )
8. {
9. if ( n%c == 0 )
10. {
11. printf("%d isn't prime.\n", n);
12. break;
13. }
14. }
15. if ( c == n )
16. printf("%d is prime.\n", n);
17. return 0;}
7 = 7^1
371 = 3^3 + 7^3 + 1^3 (27 + 343 +1)
8208 = 8^4 + 2^4 +0^4 + 8^4 (4096 + 16 + 0 + 4096).
1741725 = 1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 +5^7 (1 + 823543 + 16384 + 1 +
823543 +128 + 78125)

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).

Fibonacci series C program using a for loop


1. #include <stdio.h>
2. int main()
3. {
4. int n, first = 0, second = 1, next, c;
5. printf("Enter the number of terms\n");
6. scanf("%d", &n);
7. printf("First %d terms of Fibonacci series are:\n", n);
8. for (c = 0; c < n; c++)
9. { if (c <= 1)
10. next = c;
11. else
12. {
13. next = first + second;
14. first = second;
15. second = next;
16. }
17. printf("%d\n", next);
18. }
19. return 0;
20. }

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. }

C programming code for binary search


1. #include <stdio.h>
2. int main()
3. {int c, first, last, middle, n, search, array[100];
4. printf("Enter number of elements\n");
5. scanf("%d",&n);
6. printf("Enter %d integers\n", n);
7. for (c = 0; c < n; c++)
8. scanf("%d",&array[c]);
9. printf("Enter value to find\n");
10. scanf("%d", &search);
11. first = 0;
12. last = n - 1;
13. middle = (first+last)/2;
14. while (first <= last) {
15. if (array[middle] < search)
16. first = middle + 1;
17. else if (array[middle] == search) {
18. printf("%d found at location
%d.\n", search, middle+1);
19. break;
20. }
21. else
22. last = middle - 1;
23. middle = (first + last)/2;
24. }
25. if (first > last)
26. printf("Not found! %d isn't present in the
list.\n", search);
27. return 0; }
Reverse array C program
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, d, a[100], b[100];
6.
7. printf("Enter the number of elements in array\n");
8. scanf("%d", &n);
9.
10. printf("Enter array elements\n");
11.
12. for (c = 0; c < n ; c++)
13. scanf("%d", &a[c]);
14. /*
15. * Copying elements into array b starting from end of array
16. */
17. for (c = n - 1, d = 0; c >= 0; c--, d++)
18. b[d] = a[c];
19. /*
20. * Copying reversed array into the original.
21. * Here we are modifying original array, this is optional.
22. */
23. for (c = 0; c < n; c++)
24. a[c] = b[c];
25.
26. printf("Reverse array is\n");
27.
28. for (c = 0; c < n; c++)
29. printf("%d\n", a[c]);
30.
31. return 0;
32. }
C program merge to merge two sorted arrays
1. #include <stdio.h>
2. void merge(int [], int, int [], int, int []);
3. int main() {
4. int a[100], b[100], m, n, c, sorted[200];
5. printf("Input number of elements in first array\n");
6. scanf("%d", &m);
7. printf("Input %d integers\n", m);
8. for (c = 0; c < m; c++) {
9. scanf("%d", &a[c]);
10. }
11. printf("Input number of elements in second array\n");
12. scanf("%d", &n);
13. printf("Input %d integers\n", n);
14. for (c = 0; c < n; c++) {
15. scanf("%d", &b[c]);
16. }
17. merge(a, m, b, n, sorted);
18. printf("Sorted array:\n");
19.
20. for (c = 0; c < m + n; c++) {
21. printf("%d\n", sorted[c]);
22. }
23. return 0;
24. }
25. void merge(int a[], int m, int b[], int n, int sorted[]) {
26. int i, j, k;
27. j = k = 0;
28. for (i = 0; i < m + n;) {
29. if (j < m && k < n) {
30. if (a[j] < b[k]) {
31. sorted[i] = a[j];
32. j++;
33. }
34. else {
35. sorted[i] = b[k];
36. k++;
37. }
38. i++;
39. }
40. else if (j == m) {
41. for (; i < m + n;) {
42. sorted[i] = b[k];
43. k++;
44. i++;
45. } }
46. else {
47. for (; i < m + n;) {
48. sorted[i] = a[j];
49. j++;
50. i++;
51. } } }}
Bubble sort algorithm implementation in C
1. /* Bubble sort code */
2.
3. #include <stdio.h>
4.
5. int main()
6. {
7. int array[100], n, c, d, swap;
8.
9. printf("Enter number of elements\n");
10. scanf("%d", &n);
11.
12. printf("Enter %d integers\n", n);
13.
14. for (c = 0; c < n; c++)
15. scanf("%d", &array[c]);
16.
17. for (c = 0 ; c < n - 1; c++)
18. {
19. for (d = 0 ; d < n - c - 1; d++)
20. {
21. if (array[d] > array[d+1]) /* For decreasing order use <
*/
22. {
23. swap = array[d];
24. array[d] = array[d+1];
25. array[d+1] = swap;
26. }
27. }
28. }
29. printf("Sorted list in ascending order:\n");
30. for (c = 0; c < n; c++)
31. printf("%d\n", array[c]);
32. return 0;
33. }
Best case complexity of insertion sort is O(n), average and the worst case complexity is
O(n2).

Insertion sort algorithm implementation in C


1. /* Insertion sort ascending order */
2.
3. #include <stdio.h>
4.
5. int main()
6. {
7. int n, array[1000], c, d, t;
8.
9. printf("Enter number of elements\n");
10. scanf("%d", &n);
11.
12. printf("Enter %d integers\n", n);
13.
14. for (c = 0; c < n; c++)
15. scanf("%d", &array[c]);
16.
17. for (c = 1 ; c <= n - 1; c++) {
18. d = c;
19.
20. while ( d > 0 && array[d-1] > array[d]) {
21. t = array[d];
22. array[d] = array[d-1];
23. array[d-1] = t;
24.
25. d--;
26. }
27. }
28.
29. printf("Sorted list in ascending order:\n");
30.
31. for (c = 0; c <= n - 1; c++) {
32. printf("%d\n", array[c]);
33. }
34.
35. return 0;
36. }
Selection sort algorithm implementation in C
1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], n, c, d, position, swap;
6.
7. printf("Enter number of elements\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integers\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. for (c = 0; c < (n - 1); c++)
16. {
17. position = c;
18.
19. for (d = c + 1; d < n; d++)
20. {
21. if (array[position] > array[d])
22. position = d;
23. }
24. if (position != c)
25. {
26. swap = array[c];
27. array[c] = array[position];
28. array[position] = swap;
29. }
30. }
31.
32. printf("Sorted list in ascending order:\n");
33.
34. for (c = 0; c < n; c++)
35. printf("%d\n", array[c]);
36.
37. return 0;
38. }
String length C program
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char a[100];
7. int length;
8.
9. printf("Enter a string to calculate it's length\n");
10. gets(a);
11.
12. length = strlen(a);
13.
14. printf("Length of the string = %d\n", length);
15.
16. return 0;
17. }

String length in C without strlen


1. #include <stdio.h>
2.
3. int main()
4. {
5. char s[1000];
6. int c = 0;
7.
8. printf("Input a string\n");
9. gets(s);
10.
11. while (s[c] != '\0')
12. c++;
13.
14. printf("Length of the string: %d\n", c);
15.
16. return 0;
17. }
String comparison C program
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\n");
9. gets(a);
10.
11. printf("Enter a string\n");
12. gets(b);
13.
14. if (strcmp(a,b) == 0)
15. printf("The strings are equal.\n");
16. else
17. printf("The strings are not equal.\n");
18. return 0;
19. }

C string comparison program


1. #include <stdio.h>
2. int compare_strings(char [], char []);
3. int main()
4. {
5. char a[1000], b[1000];
6. printf("Input a string\n");
7. gets(a);
8. printf("Input a string\n");
9. gets(b);
10. if (compare_strings(a, b) == 0)
11. printf("Equal strings.\n");
12. else
13. printf("Unequal strings.\n");
14. return 0;
15. }
16. int compare_strings(char a[], char b[])
17. {
18. int c = 0;
19. while (a[c] == b[c]) {
20. if (a[c] == '\0' || b[c] == '\0')
21. break;
22. c++;
23. }
24. if (a[c] == '\0' && b[c] == '\0')
25. return 0;
26. else
27. return -1;
28. }
String copy C program
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char source[1000], destination[1000];
7.
8. printf("Input a string\n");
9. gets(source);
10.
11. strcpy(destination, source);
12.
13. printf("Source string: %s\n", source);
14. printf("Destination string: %s\n", destination);
15.
16. return 0;
17. }

Copy string in C without using strcpy


1. #include <stdio.h>
2.
3. int main()
4. {
5. int c = 0;
6. char s[1000], d[1000] = "What can I say about my programming
skills?";
7.
8. printf("Before copying, the string: %s\n", d);
9.
10. printf("Input a string to copy\n");
11. gets(s);
12.
13. while (s[c] != '\0') {
14. d[c] = s[c];
15. c++;
16. }
17.
18. d[c] = '\0';
19.
20. printf("After copying, the string: %s\n", d);
21.
22. return 0;
23. }
C oncatenation:
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char a[1000], b[1000];
7.
8. printf("Enter the first string\n");
9. gets(a);
10.
11. printf("Enter the second string\n");
12. gets(b);
13.
14. strcat(a, b);
15.
16. printf("String obtained on concatenation: %s\n", a);
17.
18. return 0;
19. }

Concatenate strings without strcat function


1. #include <stdio.h>
2. void concatenate(char [], char []);
3. int main()
4. {
5. char p[100], q[100];
6. printf("Input a string\n");
7. gets(p);
8. printf("Input a string to concatenate\n");
9. gets(q);
10. concatenate(p, q);
11. printf("String obtained on concatenation: \"%s\"\n", p);
12. return 0;
13. }
14. void concatenate(char p[], char q[]) {
15. int c, d;
16. c = 0;
17. while (p[c] != '\0') {
18. c++;
19. }
20. d = 0;
21. while (q[d] != '\0') {
22. p[c] = q[d];
23. d++;
24. c++;
25. }
26. p[c] = '\0';
27. }
Reverse a string in C using strrev
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char arr[100];
7.
8. printf("Enter a string to reverse\n");
9. gets(arr);
10.
11. strrev(arr);
12.
13. printf("Reverse of the string is \n%s\n", arr);
14.
15. return 0;
16. }

String reversal without strrev function


First we calculate length of the string without using strlen function and then copy its
characters in reverse order (from end to beginning) to a new string using a for loop.

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;}

An output of the program:

1. Input first string


2. tree
3. Input second string
4. Computer science is awesome
5. YES
C programming to remove spaces
1. #include <stdio.h>
2.
3. int main()
4. {
5. char text[1000], blank[1000];
6. int c = 0, d = 0;
7.
8. printf("Enter some text\n");
9. gets(text);
10.
11. while (text[c] != '\0') {
12. if (text[c] == ' ') {
13. int temp = c + 1;
14. if (text[temp] != '\0') {
15. while (text[temp] == ' ' && text[temp] != '\0') {
16. if (text[temp] == ' ') {
17. c++;
18. }
19. temp++;
20. }
21. }
22. }
23. blank[d] = text[c];
24. c++;
25. d++;
26. }
27.
28. blank[d] = '\0';
29.
30. printf("Text after removing blanks\n%s\n", blank);
31.
32. return 0;
33. }
Function strlwr 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 lower case\n");
9. gets(string);
10.
11. printf("The string in lower case:
%s\n", strlwr(string));
12.
13. return 0;
14. }

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. }

Change string to upper case without strupr


1. #include <stdio.h>
2.
3. void upper_string(char []);
4.
5. int main()
6. {
7. char string[100];
8.
9. printf("Enter a string to convert it into upper
case\n");
10. gets(string);
11.
12. upper_string(string);
13.
14. printf("The string in upper case: %s\n", string);
15.
16. return 0;
17. }
18.
19. void upper_string(char s[]) {
20. int c = 0;
21.
22. while (s[c] != '\0') {
23. if (s[c] >= 'a' && s[c] <= 'z') {
24. s[c] = s[c] - 32;
25. }
26. c++;
27. }
28. }

Change string to lower case without strlwr


1. #include <stdio.h>
2.
3. void lower_string(char []);
4.
5. int main()
6. {
7. char string[100];
8.
9. printf("Enter a string to convert it into lower
case\n");
10. gets(string);
11.
12. lower_string(string);
13.
14. printf("The string in lower case: %s\n", string);
15.
16. return 0;
17. }
18.
19. void lower_string(char s[]) {
20. int c = 0;
21.
22. while (s[c] != '\0') {
23. if (s[c] >= 'A' && s[c] <= 'Z') {
24. s[c] = s[c] + 32;
25. }
26. c++;
27. }
28. }

You can also implement functions using pointers.

C program to change case from upper to lower and lower to


upper
Below program changes case of alphabets if a lower case alphabet is found it is
converted to upper and if an upper case is found it is converted to lower case.

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. }

An output of the program:

1. Input a string
2. abcdefghijklmnopqrstuvwxyz{0123456789}ABCDEFGHIJKLMNOPQRS
TUVWXYZ
3. ABCDEFGHIJKLMNOPQRSTUVWXYZ{0123456789}abcdefghijklmnopqrs
tuvwxyz

If a digit or special character is present in a string, it is left as it is.


C programming code to swap strings
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char first[100], second[100], temp[100];
7.
8. printf("Enter first string\n");
9. gets(first);
10.
11. printf("Enter second string\n");
12. gets(second);
13.
14. printf("\nBefore Swapping\n");
15. printf("First string: %s\n", first);
16. printf("Second string: %s\n\n", second);
17.
18. strcpy(temp, first);
19. strcpy(first, second);
20. strcpy(second, temp);
21.
22. printf("After Swapping\n");
23. printf("First string: %s\n", first);
24. printf("Second string: %s\n", second);
25.
26. return 0;
27. }

C program to find the frequency of


characters in a string
C program to find the frequency of characters in a string: This program counts the
frequency of characters in a string, i.e., which character is present how many times in
the string. For example, in the string "code" each of the characters 'c,' 'd,' 'e,' and 'o'
has occurred one time. Only lower case alphabets are considered, other
characters (uppercase and special characters) are ignored. You can easily modify this
program to handle uppercase and special symbols.

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. }

Explanation of "count[string[c]-'a']++", suppose input string begins with 'a' so c is 0 initially


and string[0] = 'a' and string[0] - 'a' = 0 and we increment count[0] i.e. 'a' has occurred one
time and repeat this till the complete string is scanned.

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.

C programming code (Turbo C compiler only)


1. #include <stdio.h>
2. #include <conio.h>
3. #include <dir.h>
4.
5. int main()
6. {
7. int done;
8. struct ffblk a;
9.
10. printf("Press any key to view the files in the
current directory\n");
11.
12. getch();
13.
14. done = findfirst("*.*", &a, 0); // The first '*'
is for all file names and the second one is for all file
extensions
15.
16. while(!done)
17. {
18. printf("%s\n", a.ff_name);
19. done = findnext(&a);
20. }
21.
22. getch();
23. return 0;
24. }

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. }

C program to generate random numbers


C program to generate pseudo-random numbers using rand and random function
(Turbo C compiler only). As the random numbers are generated by an algorithm used
in a function they are pseudo-random, this is the reason that word pseudo is used.
Function rand() returns a pseudo-random number between 0 and RAND_MAX.
RAND_MAX is a constant which is platform dependent and equals the maximum
value returned by rand function.

C programming code using rand


We use modulus operator in our program. If you evaluate a % b where a and b are
integers then result will always be less than b for any set of values of a and b. For
example
For a = 1243 and b = 100
a % b = 1243 % 100 = 43
For a = 99 and b = 100
a % b = 99 % 100 = 99
For a = 1000 and b = 100
a % b = 1000 % 100 = 0

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 using random function (Turbo C


compiler only)
Function randomize is used to initialize random number generator. If you don't use it,
then you will get same random numbers each time you run the program.
1. #include <stdio.h>
2. #include <conio.h>
3. #include <stdlib.h>
4.
5. int main()
6. {
7. int n, max, num, c;
8.
9. printf("Enter the number of random numbers you
want\n");
10. scanf("%d", &n);
11.
12. printf("Enter the maximum value of random
number\n");
13. scanf("%d", &max);
14.
15. printf("%d random numbers from 0 to %d
are:\n", n, max);
16.
17. randomize();
18. for (c = 1; c <= n; c++)
19. {
20. num = random(max);
21. printf("%d\n",num);
22. }
23. getch();
24. return 0;
25. }

Complex numbers program in C language


1. #include <stdio.h>
2. struct complex
3. {
4. int real, img;
5. };
6. int main()
7. {struct complex a, b, c;
8. printf("Enter a and b where a + ib is the first complex
number.\n");
9. scanf("%d%d", &a.real, &a.img);
10. printf("Enter c and d where c + id is the second complex
number.\n");
11. scanf("%d%d", &b.real, &b.img);
12. c.real = a.real + b.real;
13. c.img = a.img + b.img;
14. printf("Sum of the complex numbers: (%d) +
(%di)\n", c.real, c.img);
15. return 0;}
C program to print current system date. To print date, we will use getdate function.

C programming code (Works in Turbo C only)


1. #include <stdio.h>
2. #include <conio.h>
3. #include <dos.h>
4.
5. int main()
6. {
7. struct date d;
8.
9. getdate(&d);
10.
11. printf("Current system date:
%d/%d/%d", d.da_day, d.da_mon, d.da_year);
12. getch();
13. return 0;
14. }

C program to print IP (Internet Protocol) address of your computer, system function


is used to execute the command "ipconfig" which prints IP address, subnet mask,
and default gateway. The code given below works for Windows XP and Windows 7. If
you are using Turbo C compiler then execute this program from the folder, it may not
work when you are working in a compiler and trying to run it by Ctrl + F9.

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.

C programming code for Windows 7


1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. system("C:\\WINDOWS\\System32\\shutdown /s");
7.
8. return 0;
9. }

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."

C programming code for Windows XP


It will ask if you want to shutdown your computer, if you press 'y' then your computer
will shutdown in 30 seconds.

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. }

To shutdown immediately use "C:\\WINDOWS\\System32\\shutdown -s -t 0". To


restart use "-r" instead of "-s".

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.

C programming code for Ubuntu Linux


1. #include <stdio.h>
2.
3. int main() {
4. system("shutdown -P now");
5. return 0;
6. }

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."

For more options or help type "man shutdown" in terminal.

Potrebbero piacerti anche