
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!”.
#include <stdio.h>
main()
{
printf(" ");
}
Output: Hello, World!
C program to perform arithmetic operations.
#include <stdio.h>
main()
{
int a, b;
printf( );
scanf( );
printf( );
scanf( );
printf( );
printf( );
printf( );
printf( );
}
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( );
scanf( );
c=a;
a=b;
b=c;
printf( );
printf( );
}
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( );
scanf( );
a=a+b;
b=a-b;
a=a-b;
printf( );
printf( );
}
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( );
scanf( );
(a>b)?printf( ):printf( );
}
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( );
scanf( );
printf( );
printf( );
printf( );
printf( );
printf( );
printf( );
printf( );
printf( );
}
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( ); // decimal output
printf( ); // octal output
printf( ); // hexadecimal output
printf( ); // decimal long output
printf( ); // decimal short output
printf( ); // unsigned output
printf( ); // character output
printf( ); // floating output
printf( ); // double float output
printf( );
printf( ); // simple int output
printf( ); // use a field width of 7
printf( ); // left justify in field of 7
printf( );
printf( ); // simple float output
printf( ); // use field width of 12
printf( ); // use 3 decimal places
printf( ); // use 6 decimal places
printf( ); // 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( );
scanf( );
if (a%2 == 0)
{
printf( );
}
}
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( );
scanf( );
if (a > b)
{
printf( );
}
if (b > a)
{
printf( );
}
if (a == b)
{
printf( );
}
}
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( );
scanf( );
if (a > b && a > c)
{
printf( );
}
else if (b > c)
{
printf( );
}
else
{
printf( );
}
}
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( );
scanf( );
if (a > b && a > c && a > d)
{
printf( );
}
else if (b > c && b > d)
{
printf( );
}
else if (c > d)
{
printf( );
}
else
{
printf( );
}
}
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( );
{
printf( );
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( );
scanf( );
m = n;
while( );
{
d = n % 10;
sum = sum + d;
n = n / 10;
}
printf( );
}
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( );
scanf( );
m = n;
while( );
{
d = n % 10;
sum = sum + (d * d * d);
n = n / 10;
}
if(m == sum)
{
printf( );
}
else
{
printf( );
}
}
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( );
for( );
{
printf( );
}
}
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( );
for( )
{
scanf( );
}
printf( );
for( )
{
printf( );
}
}
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( );
for( )
{
scanf( );
sum = sum + a[i];
}
printf( );
}
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( );
for( )
{
scanf( );
}
g = a[0];
s = a[0];
for( )
{
if(a[i] > g)
{
g = a[i];
}
if(a[i] < s)
{
s = a[i];
}
}
printf( );
printf( );
}
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( );
scanf( );
printf( );
scanf( );
printf( );
for( )
{
for( )
{
scanf( );
}
}
printf( );
for( )
{
for( )
{
scanf( );
}
}
printf( );
for( )
{
for( )
{
sum[i][j] = a[i][j] + b[i][j];
printf( );
}
printf( );
}
}
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( );
scanf( );
printf( );
scanf( );
printf( );
scanf( );
printf( );
scanf( );
if (colA != rowB)
{
printf( );
}
else
{
printf( );
for( )
{
for( )
{
scanf( );
}
}
printf( );
for( )
{
for( )
{
scanf( );
}
}
printf( );
for( )
{
for( )
{
mul[i][j] = 0;
for( )
{
mul[i][j] = mul[i][j] + (a[i][k]*b[k][j]);
}
printf( );
}
printf( );
}
}
}
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( );
scanf( );
printf( );
scanf( );
printf( );
for( )
{
for( )
{
scanf( );
}
}
for( )
{
rowSum = 0;
for( )
{
rowSum = rowSum + a[i][j];
}
printf( );
}
for( )
{
colSum = 0;
for( )
{
colSum = colSum + a[i][j];
}
printf( );
}
}
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( );
scanf( );
printf( );
scanf( );
printf( );
for( )
{
for( )
{
scanf( );
}
}
for( )
{
for( )
{
printf( );
}
printf( );
}
for( )
{
rowSum = 0;
for( )
{
rowSum = rowSum + a[i][j];
}
if (rowSum > l)
{
l = rowSum;
}
}
printf( );
}
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( );
c = getchar();
while( )
{
s[i++] = c;
c = getchar();
}
s[i]='\0';
printf( );
i = 0;
while( )
{
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( );
gets(s);
for( )
{
if( )
{
if( )
{
v++;
}
else
{
c++;
}
}
else
{
sc++;
}
}
printf( );
printf( );
printf( );
}
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( );
gets(s);
for( )
{
len++;
}
printf( );
}
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( );
printf( );
}
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( );
gets(s);
for( )
{
len++;
}
for( )
{
rev[j++] = s[i];
}
rev[j] = '\0';
printf( );
}
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( );
gets(s);
for( )
{
c[i] = s[i];
}
c[i]='\0';
printf( );
}
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( );
printf( );
}
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( );
gets(s);
printf( );
gets(s1);
for( )
{
c[j++] = s[i];
}
for( )
{
c[j++] = s1[i];
}
c[j]='\0';
nbsp;printf( );
}
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( );
printf( );
}
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( );
}
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( );
gets(s);
for( )
{
len++;
}
for( )
{
rev[j++] = s[i];
}
rev[j] = '\0';
for( )
{
if(s[i] == rev[i])
{
count++;
}
}
if(len == count)
{
printf( );
}
else
{
printf( );
}
}
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( );
scanf( );
result = calculatePower(base, power);
printf( );
}
long int calculatePower(int base, int power)
{
long int i, result=1;
for( )
{
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( );
scanf( );
armstrong(n);
}
void armstrong(int a)
{
int c, s = 0, d;
c = a;
while( )
{
d = a % 10;
s = s + (d * d * d);
a = a / 10;
}
if (s == c)
{
printf( );
}
else
{
printf( );
}
}
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( );
scanf( );
f = factorial(n);
printf( );
}
int factorial(int n)
{
int i, f = 1;
for( )
{
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( );
scanf( );
printf( );
}
int factorial(int n)
{
if( )
{
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( );
scanf( );
p1 = &a;
p2 = &b;
printf( );
}
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( );
scanf( );
p1 = &a;
p2 = &b;
printf( );
printf( );
printf( );
printf( );
}
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( );
for( )
{
scanf( );
}
temp = &a[0];
printf( );
for( )
{
printf( );
}
}
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( );
for( )
{
for( )
{
scanf( );
}
}
printf( );
for( )
{
for( )
{
printf( );
}
printf( );
}
}
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( );
swap( );
printf( );
}
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( );
gets(model.cpu_type);
printf( );
scanf( );
printf( );
scanf( );
printf( );
scanf( );
printf( );
printf( );
printf( );
printf( );
}
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( );
printf( );
printf( );
printf( );
}
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( );
scanf( );
printf( );
scanf( );
printf( );
printf( );
}
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( );
fflush(stdin);
scanf( );
printf( );
fflush(stdin);
gets(stud[i].name);
printf( );
fflush(stdin);
gets(stud[i].address);
}
for(i = 0; i < 2; i++) // You can max value in loop
{
printf( );
printf( );
printf( );
puts(stud[i].name);
printf( );
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( );
printf( );
printf( );
puts(stud.name);
printf( );
puts(stud.address);
}
studentStruct receive(studentStruct stud)
{
printf( );
fflush(stdin);
scanf( );
printf( );
fflush(stdin);
gets(stud.name);
printf( );
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( );
}
else
{
printf( );
}
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( );
}
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( );
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( );
scanf( );
printf( );
scanf( );
if((fpin = fopen(in_file, "r")) == NULL)
{
printf( );
}
else if((fpout = fopen(out_file, "w")) == NULL)
{
printf( );
}
else
{
while((c = getc(fpin)) != EOF)
{
putc(c, fpout);
}
printf( );
}
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.
Pointer to an array of integers in C language
#include<stdio.h>
#include<conio.h>
main() {
int i;
int * ptr;
int arr[5] = {
10,
20,
30,
40,
50
};
// Initialize array with the base address
ptr = arr;
for (i = 0; i < 5; i++) {
printf("'arr[%d]' is stored in %u\n", i, ptr);
printf("The value of arr[%d] is %d\n", i, * ptr);
ptr++;
}
getch();
}
Output: 'arr[0]' is stored in 2424352 The value of arr[0] is 10 'arr[1]' is stored in 2424356 The value of arr[1] is 20 'arr[2]' is stored in 2424360 The value of arr[2] is 30 'arr[3]' is stored in 2424364 The value of arr[3] is 40 'arr[4]' is stored in 2424368 The value of arr[4] is 50
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.