Professional Documents
Culture Documents
C Codes
C Codes
Hello world
Print Integer
Addition
Odd or Even
Add, subtract, multiply and divide
Check vowel
Leap year
Add digits
Factorial
Decimal to binary conversion
ncR and nPr
Add n numbers
Swapping
Reverse number
Palindrome number
Print Pattern
Diamond
Prime numbers
Find armstrong number
Generate armstrong number
Fibonacci series
Print floyd's triangle
Print pascal triangle
Addition using pointers
Maximum element in array
Minimum element in array
Linear search
Binary search
Reverse array
Insert element in array
Delete element from array
Bubble sort
Insertion sort
Selection sort
Add matrices
Subtract matrices
Transpose matrix
Multiply two matrices
Print string
String length
Compare strings
Copy string
Concatenate strings
Reverse string
Find palindrome
Delete vowels
C substring
Sort a string
Remove spaces
Change case
Swap strings
Character's frequency
Anagrams
Read file
Copy files
Merge two files
List files in a directory
Delete file
Random numbers
Add complex numbers
Print date
Get ip address
Shutdown computer
c programming examples
Example 1 - C hello world program
/* A very simple c program printing a string on screen*/
#include<stdio.h>
main()
{
printf("Hello World\n");
return 0;
}
#include<stdio.h>
main()
{
int number;
printf("Enter a number\n");
scanf("%d",&number);
printf("Number entered by you is %d\n", number);
return 0;
}
Output:
Enter a number
5
Number entered by you is 5
#include<stdio.h>
main()
{
int x = 1;
if ( x == 1 )
printf("x is equal to one.\n");
else
printf("For comparison use == as = is the assignment
operator.\n");
return 0;
}
Output:
x is equal to one.
#include<stdio.h>
main()
{
int value = 1;
while(value<=3)
{
printf("Value is %d\n", value);
value++;
}
return 0;
}
Output:
Value is 1
Value is 2
Value is 3
Example 5 - c program for prime number
#include<stdio.h>
main()
{
int n, c;
printf("Enter a number\n");
scanf("%d", &n);
if ( n == 2 )
printf("Prime number.\n");
else
{
for ( c = 3 ; c <= n - 1 ; c++ )
{
if ( n % c == 0 )
break;
}
if ( c != n )
printf("Not prime.\n");
else
printf("Prime number.\n");
}
return 0;
}
#include<stdio.h>
main(int argc, char *argv[])
{
int c;
printf("Number of command line arguments passed: %d\n", argc);
for ( c = 0 ; c < argc ; c++)
printf("%d. Command line argument passed is %s\n", c+1,
argv[c]);
return 0;
}
Above c program prints the number and all arguments which are passed to it.
#include<stdio.h>
main()
{
int array[100], n, c;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Array elements entered bu you are:\n");
for ( c = 0 ; c < n ; c++ )
printf("array[%d] = %d\n", c, array[c]);
return 0;
}
#include<stdio.h>
void my_function();
main()
{
printf("Main function.\n");
my_function();
printf("Back in function main.\n");
return 0;
}
void my_function()
{
printf("Welcome to my function. Feel at home.\n");
}
#include<stdio.h>
main()
{
// Single line comment in c source code
printf("Writing comments is very useful.\n");
/*
* Multiline comment syntax
* Comments help us to understand code later easily.
* Will you write comments while developing programs ?
*/
printf("Good luck\n");
return 0;
}
#include<stdio.h>
struct programming
{
float constant;
char *pointer;
};
main()
{
struct programming variable;
char string[] = "Programming in Software Development.";
variable.constant = 1.23;
variable.pointer = string;
printf("%f\n", variable.constant);
printf("%s\n", variable.pointer);
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms ");
scanf("%d",&n);
printf("First %d terms of fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
getch();
return 0;
}
#include <graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,"C:\\TC\\BGI");
outtextxy(10,20, "Graphics source code example.");
circle(200, 200, 50);
setcolor(BLUE);
line(350, 250, 450, 50);
getch();
closegraph( );
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int c;
for ( c = 1 ; c <= 10 ; c++ )
printf("%d\n", c);
getch();
return 0;
}
Above source code includes a header file <conio.h> and uses function getch, but this
file is Borland specific so it works in turbo c compiler but not in gcc. So the code for gcc
should be like
#include<stdio.h>
main()
{
int c;
/* for loop */
for ( c = 1 ; c <= 10 ; c++ )
printf("%d\n", c);
return 0;
}
If using gcc compiler save the code in a file say numbers.c, to compile the program
open the terminal and enter command gcc numbers.c, this will compile the program and
to execute the program enter command ./a.out .
C programming code
#include<stdio.h>
main()
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
printf("Integer that you have entered is %d\n", a);
return 0;
}
Output:
C programming code
#include<stdio.h>
main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
Output of program
Addition without using third variable
#include<stdio.h>
main()
{
int a = 1, b = 2;
/* Storing result of addition in variable a */
a = a + b;
/* Not recommended because original value of a is lost
* and you may be using it some where in code considering it
* as it was entered by the user.
*/
printf("Sum of a and b = %d\n", a);
return 0;
}
#include<stdio.h>
long addition(long, long);
main()
{
long first, second, sum;
scanf("%ld%ld", &first, &second);
sum = addition(first, second);
printf("%ld\n", sum);
return 0;
}
long addition(long a, long b)
{
long result;
result = a + b;
return result;
C programming code
#include<stdio.h>
main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d",&first,&second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
Arithmetic operations.
Output:
C programming code
#include<stdio.h>
main()
char ch;
printf("Enter a character\n");
scanf("%c",&ch);
else
return0;
}
Output:
main()
char ch;
printf("Enter a character\n");
scanf("%c",&ch);
switch(ch)
case'a':
case'A':
case'e':
case'E':
case'i':
case'I':
case'o':
case'O':
case'u':
case'U':
printf("%c is a vowel.\n", ch);
break;
default:
return0;
return1;
return0;
C programming code
#include<stdio.h>
main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
C programming code
#include<stdio.h>
main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while( n != 0 )
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
Output of program:
Factorial
Factorial program in c using for loop
: Here we find factorial using for loop.
#include<stdio.h>
#include<conio.h>
main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d",&n);
for( c = 1 ; c <= n ; c++ )
fact = fact*c;
printf("Factorial of %d = %d\n",n,fact);
getch();
return 0;
}
Factorial
Output of code:
C programming code
#include <stdio.h>
main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d",&n);
printf("%d in binary number system is:\n", n);
for ( c = 31 ; c >= 0 ; c-- )
{
k = n >> c;
if ( k & 1 )
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
Above code only prints binary of integer, but we may wish to perform operations on
binary so in the code below we are storing the binary in a string. We create a function
which returns a pointer to string which is the binary of the number passed as argument
to the function.
C programming code
#include<stdio.h>
#include<conio.h>
main()
{
int n, sum = 0, c, var;
printf("Enter the number of integers you want to add\n");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for ( c = 1 ; c <= n ; c++ )
{
scanf("%d",&var);
sum = sum + var;
}
printf("Sum of entered numbers = %d\n",sum);
getch();
return 0;
}
Add n numbers.
Output:
C programming code
#include<stdio.h>
main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
Output of program:
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it
will not change. For example some palindrome numbers examples are 121, 212, 12321,
-454. To check whether a number is palindrome or not first we reverse it and then
compare the number obtained with the original, if both are same then number is
palindrome otherwise not. C program for palindrome number is given below.
*
***
*****
*******
*********
We have shown five rows above, in the program you will be asked to enter the numbers
of rows you want to print in the pyramid of stars.
C programming code
#include<stdio.h>
main()
scanf("%d",&n);
temp = n;
printf(" ");
temp--;
printf("*");
printf("\n");
return0;
Output:
#include<stdio.h>
main()
int n, c, k;
scanf("%d",&n);
printf("*");
printf("\n");
return0;
For more patterns or shapes on numbers and characters see comments below and also
see codes on following pages:
Floyd triangle
Pascal triangle
»
Comments
#1Guest : i need a code for this pattern.
12345
1234
123
12
1
26/08/2011 - 12:35
main()
int n, c, k, space;
scanf("%d",&n);
space =0;
printf(" ");
space++;
printf("%d", c);
printf("\n");
return0;
26/08/2011 - 17:06
23/08/2011 - 22:04
main()
char ch ='A';
scanf("%d",&n);
printf(" ");
space++;
ch++;
printf("\n");
ch ='A';
return0;
20/08/2011 - 21:29
#6Guest : c language
20/08/2011 - 14:51
Diamond pattern
26/08/2011 - 09:48
#8Guest : please help give me source code
1
01
010
1010
10101
19/08/2011 - 17:38
main()
scanf("%d",&n);
printf("%d", num);
num =1;
else
num =0;
printf("\n");
}
return0;
20/08/2011 - 08:43
p
pr
pro
prog
progr
progra
program
18/08/2011 - 21:20
Just input the string and press enter, corresponding pattern will be printed.
#include<stdio.h>
#include<string.h>
main()
char string[100];
int c, k, length;
printf("Enter a string\n");
gets(string);
length =strlen(string);
for( c =0; c < length ; c++)
printf("%c", string[k]);
printf("\n");
return0;
18/08/2011 - 21:59
please get me the code of the c program to get the output as follows:
1
121
12321
1234321
123454321
18/08/2011 - 20:40
main()
int n, c, k, x =1;
scanf("%d",&n);
printf("%d", x);
x++;
x--;
x--;
printf("%d", x);
printf("\n");
x =1;
return0;
18/08/2011 - 22:10
usingnamespace std;
main()
cin>> n;
x = n;
cout<< num;
num++;
num--;
cout<<" ";
x--;
if( k != n )
cout<< num;
num--;
else
num--;
cout<< num;
num--;
printf("\n");
num =1;
return0;
18/08/2011 - 17:14
#16Guest : may i get its source code
1
232
34543
4567654
567898765
16/08/2011 - 21:34
main()
scanf("%d",&n);
space = n -1;
num = d;
printf(" ");
space--;
printf("%d", num);
num++;
num--;
num--;
printf("%d", num);
num--;
printf("\n");
return0;
18/08/2011 - 17:17
*******
***S***
**SSS**
*SSSSS*
*******
*** ***
** **
* *
13/08/2011 - 17:53
main()
int n, c, k, space, r;
scanf("%d",&n);
space =1;
r = n-1;
printf("*");
printf("\n");
printf("*");
printf(" ");
space =2*k-1;
r--;
printf("\n");
return0;
13/08/2011 - 19:17
#20Guest : Doubt
In the program, instead of the statement space=2*k-1; , can we go for the statement,
space+=2; ?
22/08/2011 - 09:47
#21adminPs : sure
You can use but don't forget to initialize variable space to one.
22/08/2011 - 13:01
#22Guest : code
*
**
***
****
*****
13/08/2011 - 17:15
main()
{
int n, c, k, space;
scanf("%d",&n);
space = n;
printf(" ");
space--;
printf("*");
printf("\n");
return0;
13/08/2011 - 17:29
1
22
333
4444
55555
12/08/2011 - 18:48
main()
int n, c, k;
scanf("%d",&n);
printf("%d", c);
printf("\n");
return0;
12/08/2011 - 20:13
11/08/2011 - 20:14
12/08/2011 - 07:31
*
**
***
****
***
**
*
09/08/2011 - 21:46
main()
int n, c, k;
scanf("%d",&n);
{
for( k =1; k <= c ; k++)
printf("*");
printf("\n");
printf("*");
printf("\n");
return0;
09/08/2011 - 22:38
*
*A*
*A*A*
*A*A*A*
09/08/2011 - 17:21
main()
{
int n, c, k, space, count =1;
scanf("%d",&n);
space = n;
printf(" ");
printf("*");
printf("A");
count++;
printf("\n");
space--;
count =1;
}
return0;
09/08/2011 - 21:46
08/08/2011 - 13:18
main()
int n, c, k, temp;
scanf("%d",&n);
temp = n;
printf("* ");
temp--;
printf("\n");
return0;
08/08/2011 - 15:32
*
**
***
****
Please post the code to print the following shape as soon as possible.
08/08/2011 - 13:01
#35adminPs : code
#include<stdio.h>
main()
int n, c, k;
scanf("%d",&n);
printf("* ");
printf("\n");
return0;
08/08/2011 - 15:29
06/08/2011 - 13:59
main()
scanf("%d",&n);
{
printf("%d ", number);
number++;
number =1;
printf("\n");
return0;
07/08/2011 - 11:32
Thanks for the pyramid program!! Please help me in the same problem.
Can't we directly print k directly instead of number(in 2nd inner for loop)?
I have tried it but output gives garbage values!!why does this happen? Please help me!
08/08/2011 - 22:02
You are right there is no need to use variable number you can use k directly as in code
below:
#include<stdio.h>
main()
int n, c, k;
printf("\n");
return0;
09/08/2011 - 11:19
*
* *
* * *
* * * *
please help?
05/08/2011 - 11:53
main()
int n, c, k =2, j;
printf("Enter number of rows\n");
scanf("%d",&n);
printf(" ");
k = k +2;
printf("* ");
printf("\n");
getch();
return0;
08/08/2011 - 15:45
ABCDEFEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
c program to print diamond pattern
Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as
follows:
*
***
*****
***
*
C programming code
#include<stdio.h>
main()
{
int n, c, k, space = 1;
printf("Enter number of rows\n");
scanf("%d",&n);
space = n - 1;
for ( k = 1 ; k <= n ; k++ )
{
for ( c = 1 ; c <= space ; c++ )
printf(" ");
space--;
for ( c = 1 ; c <= 2*k-1 ; c++)
printf("*");
printf("\n");
}
space = 1;
for ( k = 1 ; k <= n - 1 ; k++ )
{
for ( c = 1 ; c <= space; c++)
printf(" ");
space++;
for ( c = 1 ; c <= 2*(n-k)-1 ; c++ )
printf("*");
printf("\n");
}
return 0;
}
There are many logic to check prime numbers, one given below is more efficient then
above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
//only checking from 2 to square root of number is sufficient.
C code
#include<stdio.h>
#include<conio.h>
main()
{
int number, sum = 0, temp, remainder;
printf("Enter a number\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number.");
else
printf("Entered number is not an armstrong number.");
getch();
return 0;
}
Output:
#include<stdio.h>
main()
{
int a, b;
printf("Enter two numbers to swap ");
scanf("%d%d",&a,&b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
To understand above logic simply choose a as 7 and b as 9 and then do what is written
in program. You can choose any other combination of numbers as well. Sometimes it's
a good way to understand a program.
#include<stdio.h>
void swap(int*, int*);
main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
Output of code:
C code
#include<stdio.h>
#include<conio.h>
main()
int r;
printf("Enter the maximum range upto which you want to find armstrong
numbers ");
scanf("%ld",&number);
temp = c;
r = temp%10;
temp = temp/10;
if( c == sum )
printf("%ld\n", c);
sum =0;
getch();
return0;
fibonacci series in c
Fibonacci series in c programming: c program for fibonacci series without and with
recursion. Using the code below you can print as many number of terms of series as
desired. Numbers of fibonacci sequence are known as fibonacci numbers. First few
numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every
other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5).
This sequence has many applications in mathematics and Computer Science.
Output of program:
C programming code
#include<stdio.h>
#include<conio.h>
main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d",&n);
for ( i = 1 ; i <= n ; i++ )
{
for ( c = 1 ; c <= i ; c++ )
{
printf("%d ",a);
a++;
}
printf("\n");
}
getch();
return 0;
}
Output of program:
1
1 1
1 2 1
1 3 3 1
Pascal triangle in c
#include<stdio.h>
long factorial(int);
main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal
triangle\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
For more patterns or shapes on numbers and characters see codes on following pages:
Patterns programs
Floyd triangle
Output of program:
C programming code
#include<stdio.h>
main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
Output of code:
C code
#include<stdio.h>
main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
maximum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] > maximum )
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location number %d and it's
value is %d.\n", location, maximum);
return 0;
}
C code
#include<stdio.h>
main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
maximum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] > maximum )
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location number %d and it's
value is %d.\n", location, maximum);
return 0;
}
C code
#include<stdio.h>
main()
{
int array[100], minimum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is present at location number %d and it's
value is %d.\n", location, minimum);
return 0;
}
C code using pointers
#include<stdio.h>
main()
{
int array[100], *minimum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array;
*minimum = *array;
for ( c = 1 ; c < size ; c++ )
{
if ( *(array+c) < *minimum )
{
*minimum = *(array+c);
location = c+1;
}
}
printf("Minimum element is present at location number %d and it's
value is %d.\n", location, *minimum);
return 0;
}
linear search in c
Linear search in c programming: The following code implements linear search
( Searching algorithm ) which is used to find whether a given number is present in an
array and if it is present then at what location it occurs.It is also known as sequential
search. It is very simple and works as follows: We keep on comparing each element
with the element to search until the desired element is found or list ends. Linear search
in c language for multiple occurrences and using function.
#include<stdio.h>
main()
{
int array[100], search, c, n, count = 0;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d numbers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < n ; c++ )
{
if ( array[c] == search )
{
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if ( count == 0 )
printf("%d is not present in array.\n", search);
else
printf("%d is present %d times in array.\n", search, count);
return 0;
}
Output of code:
The code below assumes that the input numbers are in ascending order.
C programming code for binary search
#include<stdio.h>
main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}
C programming code
#include<stdio.h>
main()
{
int n, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for ( c = 0 ; c < n ; c++ )
scanf("%d",&a[c]);
for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )
b[d] = a[c];
for ( c = 0 ; c < n ; c++ )
a[c] = b[c];
printf("Reverse array is\n");
for( c = 0 ; c < n ; c++ )
printf("%d\n", a[c]);
return 0;
}
Output of program:
C code
#include<stdio.h>
main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an
element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for ( c = n - 1 ; c >= position - 1 ; c-- )
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
for( c = 0 ; c <= n ; c++ )
printf("%d\n", array[c]);
return 0;
insertion sort in c
Insertion sort in c: c program for insertion sort to sort numbers. This code implements
insertion sort algorithm to arrange numbers of an array in ascending order. With a little
modification it will arrange numbers in descending order.
selection sort in c
Selection sort in c: c program for selection sort to sort numbers. This code implements
selection sort algorithm to arrange numbers of an array in ascending order. With a little
modification it will arrange numbers in descending order.
C code
#include<stdio.h>
#include<conio.h>
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m,&n);
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&first[c][d]);
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&second[c][d]);
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d]+ second[c][d];
printf("Sum of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t",sum[c][d]);
printf("\n");
}
getch();
return 0;
}
Subtract matrices
C code to subtract matrices of any order. This program finds difference between
corresponding elements of two matrices and then print the resultant matrix.
C code
#include<stdio.h>
#include<conio.h>
main()
{
int m, n, c, d, first[10][10], second[10][10], difference[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&first[c][d]);
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&second[c][d]);
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
difference[c][d] = first[c][d] - second[c][d];
printf("difference of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t",difference[c][d]);
printf("\n");
}
getch();
return 0;
}
C programming code
#include<stdio.h>
main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}
printf("Transpose of entered matrix :-\n");
for( c = 0 ; c < n ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}
return 0;
}
Output of program:
Matrix multiplication in c
Matrix multiplication in c language: c program to multiply matrices (two dimensional
array), this program multiplies two matrices which will be entered by the user. Firstly
user will enter the order of a matrix. If the entered orders of two matrix is such that they
can't be multiplied then an error message is displayed on the screen. You have already
studied the logic to multiply them in Mathematics. Matrices are frequently used while
doing programming and are used to represent graph data structure, in solving system of
linear equations and many more.
C programming code
#include<stdio.h>
main()
{
char array[20] = "Hello World";
printf("%s\n",array);
return 0;
}
C programming code
#include<stdio.h>
main()
{
char array[100];
printf("Enter a string\n");
scanf("%s",&array);
printf("You entered the string %s\n",array);
return 0;
}
String length
This program prints length of string, for example consider the string "c programming" it's
length is 13. Null character is not counted when calculating string length. To find string
length we use strlen function of string.h.
Output of program:
You can also find string length using pointer or without strlen function. Following
program shows how to achieve this.
#include<stdio.h>
main()
{
char array[100], *pointer;
int length = 0;
printf("Enter a string\n");
gets(array);
pointer = array;
while(*(pointer+length))
length++;
printf("Length of entered string = %d\n",length);
return 0;
}
#include<stdio.h>
int compare_string(char*, char*);
main()
{
char first[100], second[100], result;
printf("Enter first string\n");
gets(first);
printf("Enter second string\n");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}
#include<stdio.h>
void copy_string(char*, char*);
main()
{
char source[100], target[100];
printf("Enter source string\n");
gets(source);
copy_string(target, source);
printf("Target string is \"%s\"\n", target);
return 0;
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}
C code
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
return 0;
}
string concatenation.
Output:
Reverse string
This program reverses a string entered by the user. For example if a user enters a
string "reverse me" then on reversing the string will be "em esrever". We show you three
different methods to reverse string the first one uses strrev library function of string.h
header file and in second we make our own function to reverse string using pointers and
reverse string using recursion. If you are using first method then you must include
string.h in your program.
C programming code
/* String reverse in c*/
#include<stdio.h>
#include<string.h>
main()
{
char arr[100];
printf("Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of entered string is \n%s\n",arr);
return 0;
}
/* Second method */
#include<stdio.h>
int string_length(char*);
void reverse(char*);
main()
{
char string[100];
printf("Enter a string\n");
gets(string);
reverse(string);
printf("Reverse of entered string is \"%s\".\n", string);
return 0;
}
void reverse(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != '\0' )
c++;
return c;
}
Palindrome number in c
#include <stdio.h>
main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
C programming code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
int check_vowel(char);
main()
{
char string[100], *temp, *pointer, ch, *start;
printf("Enter a string\n");
gets(string);
temp = string;
pointer = (char*)malloc(100);
if( pointer == NULL )
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
start = pointer;
while(*temp)
{
ch = *temp;
if ( !check_vowel(ch) )
{
*pointer = ch;
pointer++;
}
temp++;
}
*pointer = '\0';
pointer = start;
strcpy(string, pointer); /* If you wish to convert original string
*/
free(pointer);
printf("String after removing vowel is \"%s\"\n", string);
return 0;
}
int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A';
if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return TRUE;
return FALSE;
}
Substring in c programming, c substring
Substring in c programming: c programming code to find a substring from a given string
and for all substrings of a string, For example substrings of string "the" are "t", "th",
"the", "h", "he" and "e" to find substring we create our own c substring function which
returns a pointer to string. String address, length of substring required and position from
where to extract substring are the three arguments passed to function. String.h does not
contain any library function to directly find substring.
C substring code
#include<stdio.h>
#include<malloc.h>
char* substring(char*, int, int);
main()
{
char string[100], *pointer;
int position, length;
printf("Enter a string\n");
gets(string);
printf("Enter the position and length of substring\n");
scanf("%d%d",&position, &length);
pointer = substring( string, position, length);
printf("Required substring is \"%s\"\n", pointer);
free(pointer);
return 0;
}
/*C substring function: It returns a pointer to the substring */
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if( pointer == NULL )
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
for( c = 0 ; c < position -1 ; c++ )
string++;
for( c = 0 ; c < length ; c++ )
{
*(pointer+c) = *string;
string++;
}
*(pointer+c) = '\0';
return pointer;
}
"c programming"
there are two spaces in this string, so our program will print a string "c programming". It
will remove spaces when they occur more than one time consecutively in string
anywhere.
C programming code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SPACE ' '
main()
{
char string[100], *blank, *start;
int length, c = 0, d = 0;
printf("Enter a string\n");
gets(string);
length = strlen(string);
blank = string;
start = (char*)malloc(length+1);
if ( start == NULL )
exit(EXIT_FAILURE);
while(*(blank+c))
{
if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
{}
else
{
*(start+d) = *(blank+c);
d++;
}
c++;
}
*(start+d)='\0';
printf("%s\n", start);
free(start);
return 0;
}
strlwr, strupr in c
Here we will change string case with and without strlwr, strupr functions.
stwlwr in c
#include<stdio.h>
#include<string.h>
int main()
{
char string[] = "Strlwr in C";
printf("%s\n",strlwr(string));
return 0;
}
strupr in c
#include<stdio.h>
#include<string.h>
main()
{
char string[] = "strupr in c";
printf("%s\n",strupr(string));
return 0;
}
void upperString(char*);
main()
{
char string[100];
printf("Enter a string to convert it into upper case\n");
gets(string);
upperString(string);
return 0;
}
#include<stdio.h>
void lowerString(char*);
main()
{
char string[100];
lowerString(string);
return 0;
}
C programming code
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
main()
gets(first);
gets(second);
printf("\nBefore Swapping\n");
temp =(char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("After Swapping\n");
getch();
return0;
Output of program
C programming code
#include<stdio.h>
#include<string.h>
main()
{
char string[100], ch;
int c = 0, count[26] = {0};
printf("Enter a string\n");
gets(string);
while ( string[c] != '\0' )
{
/*
* Considering characters from 'a' to 'z' only
*/
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;
c++;
}
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf("%c occurs %d times in the entered
string.\n",c+'a',count[c]);
}
return 0;
}
Output of program:
anagram in c
Anagram in c: c program to check whether two strings are anagrams or not, string is
assumed to consist of alphabets only. Two words are said to be anagrams of each other
if the letters from one word can be rearranged to form the other word. From the above
definition it is clear that two strings are anagrams if all characters in both strings occur
same number of times. For example "abc" and "cab" are anagram strings, here every
character 'a', 'b' and 'c' occur only one time in both strings. Our algorithm tries to find
how many times characters appears in the strings and then comparing their
corresponding counts.
#include<stdio.h>
#include<stdlib.h>
main()
FILE *fp;
gets(file_name);
if( fp == NULL )
exit(EXIT_FAILURE);
}
printf("%c",ch);
fclose(fp);
return0;
Output of program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch, file1[20], file2[20];
FILE *fs,*ft;
fs = fopen(file1,"r");
if( fs == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
ft = fopen(file2,"w");
if( ft == NULL )
{
perror("Error ");
fclose(fs);
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
getch();
fclose(fs);
fclose(ft);
return 0;
}
C programming code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fs1, *fs2, *ft;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first file ");
gets(file1);
printf("Enter name of second file ");
gets(file2);
printf("Enter name of file which will store contents of two files
");
gets(file3);
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
if( fs1 == NULL || fs2 == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
ft = fopen(file3,"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(fs1) ) != EOF )
fputc(ch,ft);
while( ( ch = fgetc(fs2) ) != EOF )
fputc(ch,ft);
printf("Two files were merged into %s file successfully.\n",file3);
fclose(fs1);
fclose(fs2);
fclose(ft);
getch();
return 0;
}
C programming code
#include<stdio.h>
#include<conio.h>
#include<dir.h>
main()
{
int done;
struct ffblk a;
printf("Press any key to view the files in the current
directory\n");
getch();
done = findfirst("*.*",&a,0);
while(!done)
{
printf("%s\n",a.ff_name);
done = findnext(&a);
}
getch();
return 0;
}
C code
#include<stdio.h>
#include<conio.h>
int main()
{
int status;
char fname[25];
status = remove(fname);
if( status == 0 )
printf("%s file deleted successfully\n",fname);
else
{
printf("Unable to delete the file\n");
perror("Error ");
}
getch();
return 0;
}
C programming code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct complex
{
int real;
int img;
};
main()
{
struct complex a, b, c;
printf("Enter a and b where a + ib is the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf("Sum of two complex numbers = %d + %di",c.real,c.img);
else
printf("Sum of two complex numbers = %d %di",c.real,c.img);
getch();
return 0;
}
C programming code
#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
struct date d;
getdate(&d);
return0;
Output of program
C programming code
#include<stdlib.h>
main()
{
system("C:\\Windows\\System32\\ipconfig");
system("pause");
return 0;
}
IP address program executable.
If you are using Turbo C Compiler then execute your file from folder. Press F9 to build
your executable file from source program. When you run from within the compiler by
pressing Ctrl+F9 it may not work.