Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Programs » Top 100 C programs for geeks

  • Binary search in Data structures In C (Recursive and Iterative)
    Binary search algorithm in C, Recursive, Iterative, examples Data Structure
  • Java Keywords
    Java Keywords Java
  • Visual Paradigm Online and integration with Visual Studio
    Visual Paradigm Online and integration with Visual Studio Important
  • Anupama TV serial and Cast name | Anuj kapadia in Anupama
    Anupama Serial Cast & Crew name | Anuj kapadia in Anupama News
  • Difference between Jenkins and TeamCity
    Difference between Jenkins and TeamCity, Maven, Octopus Differences
  • Multithreading in java
    Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
  • Frequently asked Microservices Interview Questions
    Frequently asked Microservices Interview Questions Interview
  • Java Operators
    Java Operators Java

Top 100 C programs for geeks

Posted on September 7, 2021November 17, 2021 By GeekCer Education 1 Comment on Top 100 C programs for geeks
Top 100 C programs for geeks

Here you get top 100 C programs for geeks which will help you in interview for basic question and logic and most importantly it is very important for exams.

This article includes the program of if…else, looping, structure, arrays, pointers, recursion, file writing, file reading and so on.

You can find all the basic programs of C in one page.

Table of Contents

  • Write a program to display “Hello, World!”.
  • C program to perform arithmetic operations.
  • Write a program to swap two numbers using third variable.
  • Write a program to swap two numbers without third variable.
  • Program to compare two numbers using Conditional operator in C.
  • Simple program by using increment and decrement operators in C.
  • Write a program to display the formatted value as string in C.
  • C program to check even number.
  • Write a program to check greater number by using if condition.
  • Write a program to find greater number among three numbers.
  • Program to find greatest number among four numbers.
  • Write a program to print first 10 natural numbers using while loop.
  • Write a program to find the sum of digits using while loop.
  • C program to find armstrong number using while loop.
  • Write a program to display number and its squares using for loop.
  • C program to display array elements on the screen.
  • Write a program to find the sum of array elements.
  • Write a program to find greatest and smallest elements in array.
  • C program for addition of two matrices using arrays.
  • Write a program for multiplication of matrices using arrays.
  • Write a program to add rows and columns using the arrays.
  • Program to find the greatest row sum in the array in C
  • Write a program to read character using getchar and putchar functions.
  • Write a program to count vowels and consonants and the special characters.
  • C program to find string length without using library function.
  • Program to find the length of string using strlen function.
  • Write a program to reverse the string without using library function.
  • Write a program to of string Copy without using library function.
  • Program to copy the string by using strcpy function.
  • Write a program of string Concatenation without using library function.
  • Program to concat the string by using strcat function.
  • Write a program to compare two string by using strcmp function.
  • Write a program to check whether the given string is palindrome or not.
  • C program to calculate power using function.
  • Write a program of function with arguments and no return types.
  • Write a program of function with arguments and return type.
  • Program to find factorial using recursion in C.
  • Write a program of addition of two values using pointers in C.
  • Write a program of pointer arithmetic in C.
  • Write a program of reading and writing one dimensional array using pointer.
  • Write a program of reading and writing two dimensional array using pointer.
  • Program of swap two values using call by reference.
  • Write a program to access the members of a structure.
  • Program to access the members of a structure in C
  • Write a program of pointer to structure.
  • C program of arrays of structures.
  • Write a program of passing a structure to a function.
  • C program to open a file.
  • Write program to display the contents of a file on screen.
  • C program to write contents into the file.
  • Write a program of copy the file content using getc and putc.

Write a program to display “Hello, World!”.

                  #include <stdio.h>
main() 
{
  printf("\nHello, World!");
} 

Output:
Hello, World!

C program to perform arithmetic operations.

                  #include <stdio.h>
main() 
{
  int a, b;
  printf("Enter value for a: ");
  scanf("%d",&a);
  printf("Enter value for b: ");
  scanf("%d",&b );

  printf("\nAddition %d+%d=%d",a,b,a+b);
  printf("\nSubtraction %d-%d=%d",a,b,a-b);
  printf("\nMultiplication %dx%d=%d",a,b,a*b);
  printf("\nDivision %d/%d=%d",a,b,a/b);
} 

Output:
Enter value for a: 10
Enter value for b: 5

Addition 10+5 = 15
Subtraction 10-5 = 5
Multiplication 10x5 = 50
Division 10/5 = 2

Write a program to swap two numbers using third variable.

                  #include <stdio.h>
main() 
{
  int a, b, c;
  printf("\nEnter values for a and b: ");
  scanf("%d%d",&a,&b);
  c=a;
  a=b;
  b=c;
  printf("\nBefore swap value of a=%d and b=%d", a, b);
  printf("\nAfter swap value of a=%d and b=%d", a, b);
} 

Output:
Enter values for a and b: 10
20

Before swap value of a=10 and b=20
After swap value of a=20 and b=10

Write a program to swap two numbers without third variable.

                  #include <stdio.h>
main() 
{
  int a, b;
  printf("\nEnter values for a and b: ");
  scanf("%d%d",&a,&b);
  a=a+b;
  b=a-b;
  a=a-b;
  printf("\nBefore swapping value of a=%d b=%d", a, b);
  printf("\nAfter swapping value of a=%d and b=%d", a, b);
} 

Output:
Enter values for a and b: 10
20

Before swapping value of a=10 b=20
After swapping value of a=20 and b=10

Program to compare two numbers using Conditional operator in C.

                  #include <stdio.h>
main() 
{
  int a, b;
  printf("Please enter values for a and b: ");
  scanf("%d%d",&a,&b);
  (a>b)?printf("%d is greater.", a):printf("%d is greater.", b);
} 

Output:
Please enter values for a and b: 10
20
20 is greater.

Please enter values for a and b: 20
10
20 is greater.

Simple program by using increment and decrement operators in C.

                  #include <stdio.h>
main() 
{
  int a;
  printf("Please enter values for a: ");
  scanf("%d",&a);

  printf("\nValue of a pre increment: %d", ++a);
  printf("\nValue after post increment: %d", a);
  printf("\nValue of a post increment: %d", a++);
  printf("\nValue after post increment: %d" ,a);
  printf("\nValue of a pre decrement: %d", --a);
  printf("\nValue after pre decrement: %d", a);
  printf("\nValue post of post decrement: %d", a--);
  printf("\nValue after post decrement: %d", a);
} 

Output:
Please enter the values for a: 10

Value of a pre increment: 11
Value after post increment: 11
Value of a post increment: 11
Value after post increment: 12
Value of a pre decrement: 11
Value after pre decrement: 11
Value post of post decrement: 11
Value after post decrement: 10

Write a program to display the formatted value as string in C.

                  #include <stdio.h>
main() 
{
  int a; /*Simple integer type */
  long int b; /* long integer type */
  short int c; /* short integer type */
  unsigned int d; /* unsigned integer type */
  char e; /* character type */
  float f; /* floating point type */
  double g; /* double precision floating point */
  a = 1023;
  b = 2222;
  c = 123;
  d = 1234;
  e = 'X';
  f = 3.14159;
  g = 3.1415926535898;


  printf("\na = %d",a); // decimal output 
  printf("\na = %o",a); // octal output 
  printf("\na = %x",a); // hexadecimal output 
  printf("\nb = %1d",b); // decimal long output 
  printf("\nc = %d",c); // decimal short output 
  printf("\nd = %u",d); // unsigned output 
  printf("\ne = %c",e); // character output
  printf("\nf = %f",f); // floating output 
  printf("\ng = %f",g); // double float output 
  printf("\n");
  printf("\na = %d",a); // simple int output 
  printf("\na = %7d",a); // use a field width of 7 
  printf("\na = %-7d",a); // left justify in field of 7 
  printf("\n");
  printf("\nf = %f",f); // simple float output 
  printf("\nf = %12f",f); // use field width of 12
  printf("\nf = %12.3f",f); // use 3 decimal places 
  printf("\nf = %12.6f",f); // use 6 decimal places 
  printf("\nf = %-12.5f",f); // left justify in field 
} 

Output:
a = 1023
a = 1777
a = 3ff
b = 2222
c = 123
d = 1234
e = X
f = 3.141590
g = 3.141593

a = 1023
a =   1023 // Three spaces in left
a = 1023

f = 3.141590
f =    3.141590 // Four spaces in left
f =       3.142 // Seven spaces in left
f =    3.141590 // Four spaces in left
f = 3.14159

C program to check even number.

                  #include <stdio.h>
main() 
{
  int a;
  printf("Enter the value of a: ");
  scanf("%d",&a);
  if (a%2 == 0) 
  {
    printf("%d is even number.", a);
  }
} 

Output:
Enter the value of a: 6
6 is even number.

Write a program to check greater number by using if condition.

                  #include <stdio.h>
main() 
{
  int a, b;
  printf("Please enter the value of a and b: ");
  scanf("%d%d",&a, &b);
  if (a > b) 
  {
    printf("%d is greater.", a);
  }
  if (b > a) 
  {
    printf("%d is greater.", b);
  }
  if (a == b) 
  {
    printf("%d and %d are equal.", a, b);
  }
} 

Output:
Please enter the value of a and b: 10
20
20 is greater.

Please enter the value of a and b: 50
30
50 is greater.

Please enter the value of a and b: 10
10
10 and 10 are equal.

Write a program to find greater number among three numbers.

                  #include <stdio.h>
main() 
{
  int a, b, c;
  printf("Please enter the value of a, b and c: ");
  scanf("%d%d%d",&a, &b, &c);
  if (a > b && a > c) 
  {
    printf("%d is greater.", a);
  }
  else if (b > c) 
  {
    printf("%d is greater.", b);
  }
  else
  {
    printf("%d is greater.", c);
  }
} 

Output:
Enter the values of a, b and c: 10
20
30
30 is greater.

Program to find greatest number among four numbers.

                  #include <stdio.h>
main() 
{
  int a, b, c, d;
  printf("Enter the values of a, b, c and d: ");
  scanf(""%d%d%d%d",&a,&b,&c,&d);
  if (a > b && a > c && a > d) 
  {
    printf("%d is greatest.", a);
  }
  else if (b > c && b > d) 
  {
    printf("%d is greatest.", b);
  }
  else if (c > d) 
  {
    printf("%d is greatest.", c);
  }
  else
  {
    printf("%d is greatest.", d);
  }
} 

Output:
Enter the values of a, b, c and d: 10
20
30
15
30 is greatest.

Write a program to print first 10 natural numbers using while loop.

                  #include <stdio.h>
main() 
{
  int i = 1;
  while(i <= 10);
  {
    printf("\n%d", i);
    i++;
  }
} 

Output:
1
2
3
4
5
6
7
8
9
10

Write a program to find the sum of digits using while loop.

                  #include <stdio.h>
main() 
{
  long int n, d, m;
  long int sum = 0;
  printf("Enter a number: ");
  scanf("%ld", &n);
  m = n;
  while(n != 0);
  {
    d = n % 10;
    sum = sum  + d;
    n = n / 10;
  }
  printf("Sum of digits of %ld = %ld", m, sum);
} 

Output:
Enter a number: 2015
Sum of digits of 2015 = 8

C program to find armstrong number using while loop.

                  #include <stdio.h>
main() 
{
  long int n, d, m;
  long int sum = 0;
  printf("Enter a number: ");
  scanf("%ld", &n);
  m = n;
  while(n != 0);
  {
    d = n % 10;
    sum = sum  + (d * d * d);
    n = n / 10;
  }
  if(m == sum)
  {
    printf("%ld is armstrong number.", m);
  }
  else
  {
    printf("%ld is not armstrong number.", m);
  }
} 

Output:
Enter any number: 123
123 is not armstrong number.

Enter any number: 153
153 is armstrong number.

Write a program to display number and its squares using for loop.

                  #include <stdio.h>
main() 
{
  int i;
  printf("Number\tSquare");
  for(i = 1; i <= 10; i++);
  {
    printf("\n%d\t%d",i, i*i);
  }
} 

Output:
Number  Square
1      1
2      4
3      9
4      16
5      25
6      36
7      49
8      64
9      81
10      100

C program to display array elements on the screen.

                  #include <stdio.h>
main() 
{
  int a[5], i;
  printf("Enter elements of array: ");
  for(i = 0; i < 5; i++)
  {
    scanf("%d", &a[i]);
  }
  printf("Elements of array: ");
  for(i = 0; i < 5; i++)
  {
    printf("%d  ", a[i]);
  }
} 

Output:
Enter elements of array: 1
2
3
4
5
Elements of array:  1  2  3  4  5

Write a program to find the sum of array elements.

                  #include <stdio.h>
main() 
{
  int a[5], i;
  printf("Enter elements of array: ");
  for(i = 0; i < 5; i++)
  {
    scanf("%d", &a[i]);
    sum = sum + a[i];
  }
  printf("Sum of array elements= %d", sum);
} 

Output:
Enter elements of array: 10
20
30
40
50
Sum of array elements= 150

Write a program to find greatest and smallest elements in array.

                  #include <stdio.h>
main() 
{
  int i, g, s, a[5];
  printf("Enter the array elements: ");
  for(i = 0; i < 5; i++)
  {
    scanf("%d", &a[i]);
  }
  g = a[0];
  s = a[0];
  for(i = 0; i < 5; i++)
  {
    if(a[i] > g) 
    { 
      g = a[i];
    }
    if(a[i] < s) 
    {
      s = a[i];
    }
  }
  printf("\nGreatest Element = %d", g);
  printf("\nSmallest Element = %d", s);
} 

Output:
Enter the array elements: 20
60
50
30
10

Greatest Element = 60
Smallest Element = 10

C program for addition of two matrices using arrays.

                  #include <stdio.h>
main() 
{
  int i, j, row, col;
  int a[20][20], b[20][20], sum[20][20];;
  printf("Enter number of rows: ");
  scanf("%d", &row);
  printf("Enter number of columns: ");
  scanf("%d", &col);
  printf("Enter Matrix A: ");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      scanf("%d", &a[i][j]);
    }
  }
  printf("Enter Matrix B: ");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      scanf("%d", &b[i][j]);
    }
  }
  printf("Sum of two Matrices=\n");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      sum[i][j] = a[i][j] + b[i][j];
      printf("%4d", sum[i][j]);
    }
    printf("\n");
  }
}

Output:
Enter number of rows: 3
Enter number of columns: 3
Enter Matrix A: 1
2
3
4
5
6
7
8
9
Enter Matrix B: 1
2
3
4
5
6
7
8
9
Sum of two Matrices=
    2    4    6
    8   10  12
  14   16  18

Write a program for multiplication of matrices using arrays.

                  #include <stdio.h>
main() 
{
  int i, j, rowA, colA, rowB, colB, k;
  int a[20][20], b[20][20], mul[20][20];
  printf("Enter number of rows for Matrix A:");
  scanf("%d", &rowA);
  printf("Enter number of columns for Matrix A:");
  scanf("%d", &colA);
  printf("Enter number of rows for Matrix B:");
  scanf("%d", &rowB);
  printf("Enter number of columns for Matrix B:");
  scanf("%d", &colB);
  if (colA != rowB) 
  {
    printf("Matrix Multiplication Not Possible.");
  } 
  else 
  {
    printf("Enter Matrix A: ");
    for(i = 0; i < rowA; i++)
    {
      for(j = 0; j < colA; j++)
      {
        scanf("%d", &a[i][j]);
      }
    }
    printf("Enter Matrix B: ");
    for(i = 0; i < rowB; i++)
    {
      for(j = 0; j < colB; j++)
      {
        scanf("%d", &b[i][j]);
      }
    }
    printf("Multiplication of two Matrices=\n");
    for(i = 0; i < rowA; i++)
    {
      for(j = 0; j < colB; j++)
      {
        mul[i][j] = 0;
        for(k = 0; k <rowB; k++  )
        {
          mul[i][j] = mul[i][j] + (a[i][k]*b[k][j]);
        }
        printf("%4d", mul[i][j]);
      }
      printf("\n");
    }
  }
}

Output:
Enter number of rows for Matrix A:3
Enter number of columns for Matrix A:3
Enter number of rows for Matrix B:2
Enter number of columns for Matrix B:3
Matrix Multiplication Not Possible.

Enter number of rows for Matrix A:3
Enter number of columns for Matrix A:3
Enter number of rows for Matrix B:3
Enter number of columns for Matrix B:3
Enter Matrix A: 1
2
3
4
5
6
7
8
9
Enter Matrix B: 1
2
3
4
5
6
7
8
9
Multiplication of two Matrices=
    30    36    42
    66    81    96
  102  126   150

Write a program to add rows and columns using the arrays.

                  #include <stdio.h>
main() 
{
  int i, j, row, col;
  int a[10][10], rowSum, colSum;
  printf("Enter the number of rows: ");
  scanf("%d", &row);
  printf("Enter the number of columns: ");
  scanf("%d", &col);
  printf("Enter Matrix Elements: ");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      scanf("%d", &a[i][j]);
    }
  }
  for(i = 0; i < row; i++)
  {
    rowSum = 0;
    for(j = 0; j < col; j++)
    {
      rowSum = rowSum + a[i][j];
    }
    printf("\nSum of row %d is = %d", i + 1, rowSum);
  }
  for(j = 0; j < col; j++)
  {
    colSum = 0;
    for(i = 0; i < row; i++)
    {
      colSum = colSum + a[i][j];
    }
    printf("\nSum of column %d is = %d", j + 1, colSum);
  }
}

Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter Matrix Elements: 1
2
3
4
5
6
7
8
9

Sum of row 1 is = 6
Sum of row 2 is = 15
Sum of row 3 is = 24
Sum of column 1 is = 12
Sum of column 2 is = 15
Sum of column 3 is = 18

Program to find the greatest row sum in the array in C

                  #include <stdio.h>
main() 
{
  int i, j, row, col, l = 0, rowSum;
  int a[10][10];
  printf("Enter the number of rows: ");
  scanf("%d", &row);
  printf("Enter the number of columns: ");
  scanf("%d", &col);
  printf("Enter Matrix Elements: ");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      scanf("%d", &a[i][j]);
    }
  }
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      printf("%4d", a[i][j]);
    }
    printf("\n");
  }
  for(i = 0; i < row; i++)
  {
    rowSum = 0;
    for(j = 0; j < col; j++)
    {
      rowSum = rowSum + a[i][j];
    }
    if (rowSum > l)
    {
      l = rowSum;
    }
  }
  printf("\nGreatest row sum is = %d", l);
}

Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter Matrix Elements: 1
2
3
4
5
6
7
8
9
  1  2  3
  4  5  6
  7  8  9

Greatest row sum is = 24

Write a program to read character using getchar and putchar functions.

                  #include <stdio.h>
main() 
{
  int i = 0;
  char s[50], c;
  printf("Enter the string: ");
  c = getchar();
  while(c != '\n')
  {
    s[i++] = c;
    c = getchar();
  }
  s[i]='\0';
  printf("\nEntered String: ");
  i = 0;
  while(c != '\n')
  {
    putchar(s[i]);
    i++;
  }	
}

Output:
Enter the string: Welcome To GeekCer!

Entered String: Welcome To GeekCer!

Write a program to count vowels and consonants and the special characters.

                  #include <stdio.h>
#include <ctype.h>
main() 
{
  int i, v = 0, c = 0 , sc = 0;
  char  s[20];
  printf("Enter the string: ");
  gets(s);
  for(i = 0; s[i] != '\0'; i++)
  {
    if(isalpha(s[i]))
    {
      if(s[i]=='a' || s[i]=='e' || s[i]=='i'|| s[i]=='o' || s[i]=='u')
      {
        v++;
      }
      else
      {
        c++;
      }
    }
    else 
    {
      sc++;
    }
  }
  printf("\nNumber of Vowels are %d", v);
  printf("\nNumber of Consonants are %d", c);
  printf("\nNumber of Special Characters are %d", sc);
}

Output:
Enter the string: Welcome To GeekCer!

Number of Vowels are 7
Number of Consonants are 9
Number of Special Characters are 3

C program to find string length without using library function.

                  #include <stdio.h>
main() 
{
  int i, len = 0;
  char  s[20];
  printf("Enter the string: ");
  gets(s);
  for(i = 0; s[i] != '\0'; i++)
  {
    len++;
  }
  printf("Length of the string is %d", len);
}

Output:
Enter the string: Welcome To GeekCer!
Length of the string is 19

Program to find the length of string using strlen function.

                  #include <stdio.h>
#include <string.h>
main() 
{
  int len1, len2;
  char name[] = "GeekCer!";
  len1 = strlen(name) ;
  len2 = strlen("Welcome Geek!");
  printf("\nString = %s length = %d", name, len1);
  printf("\nString = %s length = %d", "Welcome Geek!", len2);
}

Output:
String = GeekCer! length = 8
String = Welcome Geek! length = 13

Write a program to reverse the string without using library function.

                  #include <stdio.h>
main() 
{
  int i, len = 0, j = 0;
  char  s[100], rev[100];
  printf("Enter the string: ");
  gets(s);
  for(i = 0; s[i] != '\0'; i++)
  {
    len++;
  }
  for(i = len - 1; i >= 0; i--)
  {
    rev[j++] = s[i];
  }
  rev[j] = '\0';
  printf("Reverse string is %s", rev);
}

Output:
Enter the string: GeekCer!
Reverse string is: !reCkeeG

Write a program to of string Copy without using library function.

                  #include <stdio.h>
main() 
{
  int i;
  char  s[100], c[100];
  printf("Enter the string: ");
  gets(s);
  for(i = 0; s[i] != '\0'; i++)
  {
    c[i] = s[i];
  }
  c[i]='\0';
  printf("Copied string is %s", c);
}

Output:
Enter the string: GeekCer!
Copied string is GeekCer!

Program to copy the string by using strcpy function.

                  #include <stdio.h>
#include <string.h>
main() 
{
  char source[] = "GeekCer!";
  char target[25];
  strcpy(target, source);
  printf("\nSource string = %s", source);
  printf("\nTarget string = %s", target);
}

Output:
Source string = GeekCer!
Target string = GeekCer!

Write a program of string Concatenation without using library function.

                  #include <stdio.h>
main() 
{
  int i, j = 0;
  char  s[100], s1[100], c[250];
  printf("Enter first string: ");
  gets(s);
  printf("Enter second string: ");
  gets(s1);
  for(i = 0; s[i] != '\0'; i++)
  {
    c[j++] = s[i];
  }
  for(i = 0; s1[i] != '\0'; i++)
  {
    c[j++] = s1[i];
  }
  c[j]='\0';
  nbsp;printf("String Concatenation = %s", c);
}

Output:
Enter first string: Welcome To
Enter second string:   GeekCer!
String Concatenation = Welcome To GeekCer!

Program to concat the string by using strcat function.

                  #include <stdio.h>
#include <string.h>
main() 
{
  char source[] = "Geek!";
  char target[30] = "Welcome ";
  strcat(target, source);
  printf("\nSource string = %s", source);
  printf("\nTarget string = %s", target);
}

Output:
Source string = Geek!
Target string = Welcome Geek!

Write a program to compare two string by using strcmp function.

                  #include <stdio.h>
#include <string.h>
main() 
{
  int i, j, k;
  char string1[] = "Hello Geek";
  char string2[] = "Geek";
  i = strcmp(string1, "Hello Geek") ;
  j = strcmp(string1, string2) ;
  k = strcmp(string1, "Test Program") ;
  printf("\n%d %d %d", i, j, k);
}

Output:
0  1  -1

Write a program to check whether the given string is palindrome or not.

                  #include <stdio.h>
main() 
{
  int i, j = 0, len = 0, count = 0;
  char  s[100], rev[100];
  printf("Enter the string: ");
  gets(s);
  for(i = 0; s[i] != '\0'; i++)
  {
    len++;
  }
  for(i = len - 1; i >= 0; i--)
  {
    rev[j++] = s[i];
  }
  rev[j] = '\0';
  for(i = 0; s[i] != '\0'; i++)
  {
    if(s[i] == rev[i]) 
    {
      count++;
    }
  }
  if(len == count) 
  {
    printf("String is palindrome.");
  }
  else
  {
    printf("String is not palindrome.");
  }
}

Output:
Enter the string: GeekCer
String is not palindrome.

Enter the string: abcba
String is palindrome.

C program to calculate power using function.

                  #include <stdio.h>
long int calculatePower(int, int) ;
void main() 
{
  int base, power;
  long int result;
  printf("Enter base and power values: ");
  scanf("%d%d",&base, &power);
  result = calculatePower(base, power);
  printf("\n%d^%d = %ld",base, power, result);
}
long int calculatePower(int base, int power) 
{
  long int i, result=1;
  for(i = 1; i <= power; i++)
  {
    result = result * base;
  }
  return result;
}

Output:
Enter base and power value: 5
3

5^3=125

Write a program of function with arguments and no return types.

                  #include <stdio.h>
void armstrong(int);
void main()
{
  int n;
  printf("Enter any number: ");
  scanf("%ld", &n);
  armstrong(n);
}
void armstrong(int a) 
{
  int c, s = 0, d;
  c = a;
  while(a != 0	)
  {
    d = a % 10;
    s = s + (d * d * d);
    a = a / 10;
  }
  if (s == c) 
  {
    printf("Armstrong number.");
  }
  else 
  {
    printf("Not armstrong number.");
  }
}

Output:
Enter any number: 153
Armstrong number.

Enter any number: 111
Not armstrong number.

Write a program of function with arguments and return type.

                  #include <stdio.h>
int factorial(int);
void main()
{
  int n, f;
  printf("Enter any number: ");
  scanf("%d", &n);
  f = factorial(n);
  printf("Factorial of %d is %d ", n, f);
}
int factorial(int n) 
{
  int i, f = 1;
  for(i = n; i >= 1; i--)
  {
    f = f * i;
  }
  return f;
}

Output:
Enter any number: 5
Factorial of 5 is 120

Program to find factorial using recursion in C.

                  #include <stdio.h>
int factorial(int);
void main() 
{
  int n;
  printf("Enter any number: ");
  scanf("%d", &n);
  printf("Factorial of %d is %d", n, factorial(n));
}
int factorial(int n) 
{
  if(n == 0)
  {
    return 1;
  }
  else 
  {
    return n * factorial(n - 1);
  }
}

Output:
Enter any number: 5
Factorial of 5 is 120

You may also like Top C++ programs for geeks.

Write a program of addition of two values using pointers in C.

                  #include <stdio.h>
void main() 
{
  int a, b;
  int *p1, *p2;
  printf("Enter two values: ");
  scanf("%d%d", &a, &b);
  p1 = &a;
  p2 = &b;
  printf("Addition is %d", (*p1 + *p2));
}

Output:
Enter two values: 10
20
Addition is 30

Write a program of pointer arithmetic in C.

                  #include <stdio.h>
void main() 
{
  int a, b;
  int *p1, *p2;
  printf("Enter two values: ");
  scanf("%d%d", &a, &b);
  p1 = &a;
  p2 = &b;
  printf("\nAddition is %d", (*p1 + *p2));
  printf("\nSubtraction is %d", (*p1 - *p2));
  printf("\nMultiplication is %d", (*p1 * *p2));
  printf("\nDivision is %d", (*p1 / *p2));
}

Output:
Enter two values: 20
10

Addition is 30
Subtraction is 10
Multiplication is 200
Division is 2

Top 100 C programs for geeks

Write a program of reading and writing one dimensional array using pointer.

                  #include <stdio.h>
void main() 
{
  int a[10], i, n = 10;
  int *temp;
  printf("Enter array elements: ");
  for(i = 0; i < n; i++)
  {
    scanf("%d", &a[i]);
  }
  temp = &a[0];
  printf("\nOutput of array elements");
  for(i = 0; i < n; i++)
  {
    printf("\n%d",*(temp + i));
  }
}

Output:
Enter array elements: 1
2
3
4
5
6
7
8
9
10

Output of array elements
1
2
3
4
5
6
7
8
9
10

Write a program of reading and writing two dimensional array using pointer.

                  #include <stdio.h>
void main() 
{
  int a[10][10];
  int i, j, row = 3, col = 3;
  printf("Enter array elements: ");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      scanf("%d", &a[i][j]);
    }
  }
  printf("Output of array elements\n");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      printf("%d  ", *(*(a + i) + j));
    }
    printf("\n");
  }
}

Output:
Enter array elements: 1
2
3
4
5
6
7
8
9
Output of array elements
1  2  3
4  5  6
7  8  9

Program of swap two values using call by reference.

                  #include <stdio.h>
void swap(int*, int*);
void main() 
{
  int a = 10, b = 20;
  printf("Before swapping a = %d, b = %d", a, b);
  swap(&a, &b);
  printf("\nAfter swapping a = %d, b = %d", a, b);
}
void swap(int *a, int *b) 
{
  *a = *a + *b;
  *b = *a - *b;
  *a = *a - *b;
}

Output:
Before swapping a = 10, b = 20
After swapping a = 20, b = 10

Write a program to access the members of a structure.

                  #include <stdio.h>
struct Computer
{
  float cost;
  int year;
  float cpu_speed;
  char cpu_type[16];
} model;
void main() 
{
  printf("Enter the type of the CPU inside your computer: ");
  gets(model.cpu_type);
  printf("Enter the speed(GHz) of the CPU: ");
  scanf("%f", &model.cpu_speed);
  printf("Enter the year your computer was made: ");
  scanf("%d", &model.year);
  printf("Enter the much you paid for the computer: ");
  scanf("%f", &model.cost);

  printf("\nCPU type: %s", model.cpu_type);
  printf("\nCPU speed: %f GHz", model.cpu_speed);
  printf("\nYear: %d", model.year);
  printf("\nCost: $%4.2f", model.cost);
}

Output:
Enter the type of the CPU inside your computer: Intel Core
Enter the speed(GHz) of the CPU: 1.80
Enter the year your computer was made: 2005
Enter the much you paid for the computer: 800

CPU type: Intel Core
CPU speed: 1.800000 GHz
Year: 2005
Cost: $800.00

Program to access the members of a structure in C

                  #include <stdio.h>
struct Computer
{
  float cost;
  int year;
  float cpu_speed;
  char cpu_type[16];
};
void main() 
{
  struct Computer model = { 800, 2005, 1.80, "Intel Core"}; // Initialization of structure
  printf("\nCPU type: %s", model.cpu_type);
  printf("\nCPU speed: %f GHz", model.cpu_speed);
  printf("\nYear: %d", model.year);
  printf("\nCost: $%4.2f", model.cost);
}

Output:
CPU type: Intel Core
CPU speed: 1.800000 GHz
Year: 2005
Cost: $800.00

You may also like Top HR Interview Questions and Answers for Job Seekers

Write a program of pointer to structure.

                  #include <stdio.h>
struct Employee
{
  int empNo;
  char empName[16];
};
void main() 
{
  struct Employee emp, *ptr;
  ptr = &emp;
  printf("Enter employee no: ");
  scanf("%d", &ptr->empNo);
  printf("Enter employee name: ");
  scanf("%s", &ptr->empName);
  printf("\nEmployee No: %d", ptr->empNo);
  printf("\nEmployee Name: %s", ptr->empName);
}

Output:
Enter employee no: 1
Enter employee name: Geek

Employee No: 1
Employee Name: Geek

C program of arrays of structures.

                  #include <stdio.h>
struct Student
{
  int roll;
  char name[16];
  char address[30];
};
typedef struct Student studentStruct;
void main() 
{
  studentStruct stud[10];
  int i;
  for(i = 0; i < 2; i++) // You can max value in loop
  {
    printf("Enter the roll number: ");
    fflush(stdin);
    scanf("%d", &stud[i].roll);
    printf("Enter the name: ");
    fflush(stdin);
    gets(stud[i].name);
    printf("Enter the address: ");
    fflush(stdin);
    gets(stud[i].address);
  }
  for(i = 0; i < 2; i++)  // You can max value in loop
  {
    printf("\n\nRoll number: ");
    printf("%d",stud[i].roll);
    printf("\nName: ");
    puts(stud[i].name);
    printf("Address: ");
    puts(stud[i].address);
  }
}

Output:
Enter the roll number: 1
Enter the name: Geek
Enter the address: India
Enter the roll number: 2
Enter the name: Scott
Enter the address: USA

Roll number: 1
Name: Geek
Address: India

Roll number: 2
Name: Scott
Address: USA

Write a program of passing a structure to a function.

                  #include <stdio.h>
struct Student
{
  int roll;
  char name[16];
  char address[30];
};
typedef struct Student studentStruct;
studentStruct receive(studentStruct stud);
void main() 
{
  studentStruct stud;;
  stud = receive(stud);
  printf("\n\nRoll number: ");
  printf("%d",stud.roll);
  printf("\nName: ");
  puts(stud.name);
  printf("Address: ");
  puts(stud.address);
}
studentStruct receive(studentStruct stud) 
{
  printf("Enter the roll number: ");
  fflush(stdin);
  scanf("%d", &stud.roll);
  printf("Enter the name: ");
  fflush(stdin);
  gets(stud.name);
  printf("Enter the address: ");
  fflush(stdin);
  gets(stud.address);
  return stud;
}

Output:
Enter the roll number: 1
Enter the name: Geek
Enter the address: India

Roll number: 1
Name: Geek
Address: India

C program to open a file.

                  #include <stdio.h>	
void main() 
{
  FILE *fp;
  fp = fopen("file.txt", "r");
  if(fp == NULL)
  {
    printf("File does not exist, please check file location!\n");
  }
  else
  {
    printf("File Opened!\n");
  }
  fclose(fp);
}

Output:
File does not exist, please check file location! // If file is not available at location.

File Opened! // If file is available at location.

Write program to display the contents of a file on screen.

file.txt

Welcome To GeekCer!
This is an example of file program.
                  #include <stdio.h>	
void main() 
{
  FILE *fp;
  int c;
  fp = fopen("file.txt", "r");
  if(fp == NULL)
  {
    printf("File does not exist, please check file location!\n");
  }
  else
  {
    c = getc(fp) ;
    while(c != EOF)
    {
      putchar(c);
      c = getc(fp);
    }
  }
  fclose(fp);
}

Output:
Welcome To GeekCer!
This is an example of file program.

C program to write contents into the file.

                  #include <stdio.h>	
void main() 
{
  FILE *fp;
  fp = fopen("file.txt", "w");
  fprintf(fp,"%s","This is just an example of file.");
  fclose(fp);
}

Output: (Please check file.txt)
This is just an example of file.

Write a program of copy the file content using getc and putc.

file.txt

Welcome To GeekCer!
This is an example of file program.
                  #include <stdio.h>	
void main() 
{
  char in_file[30], out_file[30], c;
  FILE *fpin, *fpout;;
  printf("Enter name of the source file: ");
  scanf("%s", in_file);
  printf("Enter name of the destination file: ");
  scanf("%s", out_file);
  if((fpin = fopen(in_file, "r")) == NULL)
  {
    printf("Error could not open source file for reading.\n");
  }
  else if((fpout = fopen(out_file, "w")) == NULL)
  {
    printf("Error could not open destination file for writing.\n");
  }
  else
  {
    while((c = getc(fpin)) != EOF)
    {
      putc(c, fpout);
    }
    printf("Destination file has been copied.\n");
  }
  fclose(fpin);
  fclose(fpout);
}

Output: (Please check output.txt)
Enter name of the source file: file.txt
Enter name of the destination file: output.txt
Destination file has been copied.

Hope you liked Top C Program, and it can be very useful for you and your knowledge. If you have any doubt and query feel free to comment or email us at geekcer.code@gmail.com.

And also you can go to contact section and write your query.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language, Programs Tags:C program examples, C programming examples with output, C Programs for practice, C programs with solutions

Post navigation

Previous Post: Top C++ programs for geeks | C++ code examples
Next Post: Top Java Programs for Coder

More Related Articles

Statements in C Language | Types of statements in C Language Statements in C Language | Types of statements in C Language C Language
Programming with C Language Tutorial Programming with C Language Tutorial C Language
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Constants and Operators in C Language Constants and Operators in C Language C Language
Top C++ programs for geeks Top C++ programs for geeks | C++ code examples Programs
File Handling in C with examples | File Operations in C File Handling in C with examples | File Operations in C C Language

Related Posts

  • Statements in C Language | Types of statements in C Language
    Statements in C Language | Types of statements in C Language C Language
  • Programming with C Language Tutorial
    Programming with C Language Tutorial C Language
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Constants and Operators in C Language
    Constants and Operators in C Language C Language
  • Top C++ programs for geeks
    Top C++ programs for geeks | C++ code examples Programs
  • File Handling in C with examples | File Operations in C
    File Handling in C with examples | File Operations in C C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • National Doctors Day in India and Other Countries, July 1, 2022
  • America Independence Day : 4th July USA | USA Birthday
  • Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा
  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
  • Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
  • Internal Working of HashMap in Java, Rehashing, Collision
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
  • Binary search in Data structures In C (Recursive and Iterative)
    Binary search algorithm in C, Recursive, Iterative, examples Data Structure
  • Java Keywords
    Java Keywords Java
  • Visual Paradigm Online and integration with Visual Studio
    Visual Paradigm Online and integration with Visual Studio Important
  • Anupama TV serial and Cast name | Anuj kapadia in Anupama
    Anupama Serial Cast & Crew name | Anuj kapadia in Anupama News
  • Difference between Jenkins and TeamCity
    Difference between Jenkins and TeamCity, Maven, Octopus Differences
  • Multithreading in java
    Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
  • Frequently asked Microservices Interview Questions
    Frequently asked Microservices Interview Questions Interview
  • Java Operators
    Java Operators Java
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved