Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 130

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

Output of above program:


"Hello World"

Example 2 - c program to take input from user using scanf

#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

Example 3 - using if else control instructions

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

Example 4 - loop example

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

Example 6 - command line arguments

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

Example 7 - Array program

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

Example 8 - function program

#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");
}

Example 9 - Using comments in a program

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

Example 10 - using structures in c programming

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

Example 11 - c program for fibonacci series

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

Example 12 - c graphics programming

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

For gcc compiler users


If you are using gcc compiler on linux operating system then you need to modify
programs. For example consider the following programs which prints first ten natural
numbers

#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 program print integer


This c program first inputs an integer and then prints it. Input is done using scanf
function and number is printed on screen using printf.

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 program to add two numbers


C program to add two numbers: This c language program perform the basic arithmetic
operation of addition on two numbers and then prints the sum on the screen. For
example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the
screen.

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

Add numbers program executable.

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

C program to add two numbers repeatedly


#include<stdio.h>
 
main()
{
int a, b, c;
char ch;
 
while(1)
{
printf("Enter values of a and b\n");
scanf("%d%d",&a,&b);
 
c = a + b;
 
printf("a + b = %d\n", c);
 
printf("Do you wish to add more numbers(y/n)\n");
scanf(" %c",&ch);
 
if ( ch == 'y' || ch == 'Y' )
continue;
else
break;
}
 
return 0;
}

Adding numbers in c using function


We have used long data type as it can handle large numbers.

#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 program to check odd or even using modulus


operator
#include<stdio.h>
 
main()
{
int n;
 
printf("Enter an integer\n");
scanf("%d",&n);
 
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
 
return 0;
}

C program to check odd or even using bitwise


operator
#include<stdio.h>
 
main()
{
int n;
 
printf("Enter an integer\n");
scanf("%d",&n);
 
if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");
 
return 0;
}

C program to check odd or even without using bitwise


or modulus operator
#include<stdio.h>
 
main()
{
int n;
 
printf("Enter an integer\n");
scanf("%d",&n);
 
if ( (n/2)*2 == n )
printf("Even\n");
else
printf("Odd\n");
 
return 0;
}

Find odd or even using conditional operator


#include<stdio.h>
 
main()
{
int n;
 
printf("Enter an integer\n");
scanf("%d",&n);
 
n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
 
return 0;
}

C program to perform addition, subtraction,


multiplication and division
C program to perform basic arithmetic operations i.e. add , subtract, multiply and divide
two numbers. Numbers are assumed to be integers and will be entered by the user.

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 program to check whether input alphabet is a vowel


or not
This code checks whether an input alphabet is a vowel or not. Both lower-case and
upper-case are checked.

C programming code
#include<stdio.h>

main()

char ch;

printf("Enter a character\n");

scanf("%c",&ch);

if( ch =='a'|| ch =='A'|| ch =='e'|| ch =='E'|| ch =='i'|| ch =='I'||


ch =='o'|| ch=='O'|| ch =='u'|| ch =='U')

printf("%c is a vowel.\n", ch);

else

printf("%c is not a vowel.\n", ch);

return0;

}
Output:

Check vowel using switch statement


#include<stdio.h>

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:

printf("%c is not a vowel.\n", ch);

return0;

Function to check vowel


int check_vowel(char a)

if( a >='A'&& a <='Z')

a = a +'a'-'A';/* Converting to lower case */ 

if( a =='a'|| a =='e'|| a =='i'|| a =='o'|| a =='u')

return1; 

return0;

c program to check leap year


c program to check leap year: c code to check leap year, year will be entered by the
user.

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

add digits of number in c


C program to add digits of a number: Here we are using modulus operator to extract
individual digits of number and adding them.

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:

Factorial program in c using function


#include<stdio.h>
 
long factorial(int);
 
main()
{
int number;
long fact = 1;
 
printf("Enter a number to calculate it's factorial\n");
scanf("%d",&number);
 
printf("%d! = %ld\n", number, factorial(number));
 
return 0;
}
 
long factorial(int n)
{
int c;
long result = 1;
 
for( c = 1 ; c <= n ; c++ )
result = result*c;
 
return ( result );
}

Factorial program in c using recursion


#include<stdio.h>
 
long factorial(int);
 
main()
{
int num;
long f;
 
printf("ENTER A NUMBER TO FIND FACTORIAL :");
scanf("%d",&num);
 
if(num<0)
printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
else
{
f = factorial(num);
printf("%d!=%ld",num,f);
}
return(0);
}
 
long factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}

Decimal to binary conversion


C program to convert decimal to binary: c language code to convert an integer from
decimal number system(base-10) to binary number system(base-2). Size of integer is
assumed to be 32 bits. We use bitwise operators to perform the desired task. We right
shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the
number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).

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 code to store decimal to binary conversion in a


string
#include <stdio.h>
#include <stdlib.h>
 
char *decimal_to_binary(int);
 
main()
{
int n, c, k;
char *pointer;
 
printf("Enter an integer in decimal number system\n");
scanf("%d",&n);
 
pointer = decimal_to_binary(n);
printf("Binary string of %d is: %s\n", n, t);
 
free(pointer);
 
return 0;
}
 
char *decimal_to_binary(int n)
{
int c, d, count;
char *pointer;
 
count = 0;
pointer = (char*)malloc(32+1);
 
if ( pointer == NULL )
exit(EXIT_FAILURE);
 
for ( c = 31 ; c >= 0 ; c-- )
{
d = n >> c;
 
if ( d & 1 )
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';
 
count++;
}
*(pointer+count) = '\0';
 
return pointer;
}

c program to find ncr and npr


c program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr =
n!/(n-r)!

C program to find nCr using function


#include<stdio.h>
 
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
 
main()
{
int n, r;
long ncr, npr;
 
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
 
ncr = find_ncr(n, r);
npr = find_npr(n, r);
 
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
 
return 0;
}
 
long find_ncr(int n, int r)
{
long result;
 
result = factorial(n)/(factorial(r)*factorial(n-r));
 
return result;
}
 
long find_npr(int n, int r)
{
long result;
 
result = factorial(n)/factorial(n-r);
 
return result;
}
 
long factorial(int n)
{
int c;
long result = 1;
 
for( c = 1 ; c <= n ; c++ )
result = result*c;
 
return ( result );
}
Output of program:

c program to add n numbers


This c program add n numbers which will be entered by the user. Firstly user will enter a
number indicating how many numbers user wishes to add and then user will enter n
numbers.In our c program to add numbers we are not using an array, if you wish you
can use an array.

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 program to reverse a number


C Program to reverse a number :- This program reverse the number entered by the user
and then prints the reversed number on the screen. For example if user enter 123 as
input then 321 is printed as output. In our program we use modulus(%) operator to
obtain the digits of a number. To invert number look at it and write it from opposite
direction or the output of code is a number obtained by writing original number from
right to left.

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.

Palindrome number algorithm


1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.

Palindrome number program 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 program to print patterns of numbers and stars


These program prints various different patterns of numbers and stars. These codes
illustrate how to create various patterns using c programming. Most of these c programs
involve usage of nested loops and space. A pattern of numbers, star or characters is a
way of arranging these in some logical manner or they may form a sequence. Some of
these patterns are triangles which have special importance in mathematics. Some
patterns are symmetrical while other are not. Please see the complete page and look at
comments for many different patterns.

*
***
*****
*******
*********

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

int row, c, n, temp;

printf("Enter the number of rows in pyramid of stars you wish to see


");

scanf("%d",&n);

 
temp = n;

for( row =1; row <= n ; row++)

for( c =1; c < temp ; c++)

printf(" ");

temp--;

for( c =1; c <=2*row -1; c++)

printf("*");

printf("\n");

return0;

Output:

Consider the pattern


*
**
***
****
*****

to print above pattern see the code below:

#include<stdio.h>

main()

int n, c, k;

printf("Enter number of rows\n");

scanf("%d",&n);

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

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

#2adminPs : number pattern c code


#include<stdio.h>

main()

int n, c, k, space;

scanf("%d",&n);

space =0;

for( k = n ; k >=1; k--)

for( c =1; c <= space ; c++)

printf(" ");

space++;

for( c =1; c <= k ; c++)

printf("%d", c);
 

printf("\n");

return0;

26/08/2011 - 17:06

#3Guest : thanks for the patterns it

thanks for the patterns it really helped me a lot.

23/08/2011 - 22:04

#4Guest : i need code of this pattern


A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
A
20/08/2011 - 18:07

#5adminPs : c program for character pattern


#include<stdio.h>

main()

char ch ='A';

int n, c, k, space =0;

scanf("%d",&n);

for( k = n ; k >=1; k--)


{

for( c =1; c <= space ; c++)

printf(" ");

space++;

for( c =1; c <= k ; c++)

printf("%c ", ch);

ch++;

printf("\n");

ch ='A';

return0;

20/08/2011 - 21:29

#6Guest : c language

diamond pattern of stars

20/08/2011 - 14:51

#7adminPs : diamond pattern

Diamond pattern

26/08/2011 - 09:48
#8Guest : please help give me source code

1
01
010
1010
10101

19/08/2011 - 17:38

#9adminPs : c program to print pattern


#include<stdio.h>

main()

int n, c, k, num =1;

scanf("%d",&n);

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

printf("%d", num);

if( num ==0)

num =1;

else

num =0;

printf("\n");
}

return0;

20/08/2011 - 08:43

#10Guest : can you write code for this

p
pr
pro
prog
progr
progra
program

18/08/2011 - 21:20

#11adminPs : pattern for string

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

for( k =0; k <= c ; k++)

printf("%c", string[k]);

printf("\n");

return0;

18/08/2011 - 21:59

#12Guest : c programming code

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

#13adminPs : c program to print number pattern


#include<stdio.h>

main()

int n, c, k, x =1;

scanf("%d",&n);
 

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

printf("%d", x);

x++;

x--;

for( k =1; k <= c -1; k++)

x--;

printf("%d", x);

printf("\n");

x =1;

return0;

18/08/2011 - 22:10

#14Guest : c++ code to print pattern


1 1
12 21
12321
17/08/2011 - 19:15
#15adminPs : c++ code to print pattern
#include<iostream>

usingnamespace std;

main()

int n, k, c, space, x, num =1;

cin>> n;

x = n;

for( k =1; k <= n ; k++)

for( c =1; c <= k ; c++)

cout<< num;

num++;

num--;

for( c =1; c <=2*x -3; c++)

cout<<" ";

x--;
 

if( k != n )

for( c =1; c <= k ; c++)

cout<< num;

num--;

else

num--;

for( c =1; c <= k -1; c++)

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

#17adminPs : c code for number pattern


#include<stdio.h>

main()

int n, c, d, num =1, space;

scanf("%d",&n);

space = n -1;

for( d =1; d <= n ; d++)

num = d;

for( c =1; c <= space ; c++)

printf(" ");

space--;

for( c =1; c <= d ; c++)

printf("%d", num);
num++;

num--;

num--;

for( c =1; c < d ; c++)

printf("%d", num);

num--;

printf("\n");

return0;

18/08/2011 - 17:17

#18Guest : how to print this pattern

*******
***S***
**SSS**
*SSSSS*

where S represents Space

*******
*** ***
** **
* *
13/08/2011 - 17:53

#19adminPs : c code for star pattern


#include<stdio.h>

 
main()

int n, c, k, space, r;

printf("Enter number of rows\n");

scanf("%d",&n);

space =1;

r = n-1;

for( c =1; c <=2*n -1; c++)

printf("*");

printf("\n");

for( k =2; k <= n ; k++)

for( c =1; c <= r ; c++)

printf("*");

for( c =1; c <= space ; c++)

printf(" ");

space =2*k-1;

for( c =1; c <= r ; c++)


printf("*");

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

guys i need the code for this

*
**
***
****
*****
13/08/2011 - 17:15

#23adminPs : star pattern source code


#include<stdio.h>

main()

{
int n, c, k, space;

printf("Enter number of rows\n");

scanf("%d",&n);

space = n;

for( k =1; k <= n ; k++)

for( c =1; c < space ; c++)

printf(" ");

space--;

for( c =1; c <= k ; c++)

printf("*");

printf("\n");

return0;

13/08/2011 - 17:29

#24Guest : i need a program to draw the below patern

1
22
333
4444
55555

12/08/2011 - 18:48

#25adminPs : number pattern source code


#include<stdio.h>

main()

int n, c, k;

printf("Enter number of rows\n");

scanf("%d",&n);

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

printf("%d", c);

printf("\n");

return0;

12/08/2011 - 20:13

#26Guest : need help

i need program to print the following pattern ::


1
23
456
7 8 9 10

11/08/2011 - 20:14

#27adminPs : Floyd's triangle

This pattern is Floyd's triangle see Floyd's Triangle code.

12/08/2011 - 07:31

#28Guest : need a code in c to print below pattern

i want a code to print stars like below

*
**
***
****
***
**
*

09/08/2011 - 21:46

#29adminPs : stars pattern using c programming


#include<stdio.h>

main()

int n, c, k;

printf("Enter number of rows\n");

scanf("%d",&n);

for( c =1; c <= n ; c++)

{
for( k =1; k <= c ; k++)

printf("*");

printf("\n");

for( c = n -2; c >=0; c--)

for( k = c ; k >=0; k--)

printf("*");

printf("\n");

return0;

09/08/2011 - 22:38

#30Guest : GET ME THE CODE OF A PROGRAM

GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:

*
*A*
*A*A*
*A*A*A*
09/08/2011 - 17:21

#31adminPs : code to print pattern of stars and numbers


#include<stdio.h>

main()

{
int n, c, k, space, count =1;

printf("Enter number of rows\n");

scanf("%d",&n);

space = n;

for( c =1; c <= n ; c++)

for( k =1; k < space ; k++)

printf(" ");

for( k =1; k <= c ; k++)

printf("*");

if( c >1&& count < c)

printf("A");

count++;

printf("\n");

space--;

count =1;

}
return0;

09/08/2011 - 21:46

#32Guest : codes for pyramid of stars.

i need this pattern


*****
***
**
*

08/08/2011 - 13:18

#33adminPs : code for pattern


#include<stdio.h>

main()

int n, c, k, temp;

printf("Enter number of rows\n");

scanf("%d",&n);

temp = n;

for( c =1; c <= n ; c++)

for( k =1; k <= temp ; k++)

printf("* ");

temp--;
 

printf("\n");

return0;

08/08/2011 - 15:32

#34Guest : Please post the code for this

*
**
***
****

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;

printf("Enter number of rows\n");

scanf("%d",&n);

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

printf("* ");
 

printf("\n");

return0;

08/08/2011 - 15:29

#36Guest : please help

i need c program to print no in this format


1
12
123
1234

06/08/2011 - 13:59

#37adminPs : source code


#include<stdio.h>

main()

int number =1, n, c, k;

printf("Enter number of rows\n");

scanf("%d",&n);

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

{
printf("%d ", number);

number++;

number =1;

printf("\n");

return0;

07/08/2011 - 11:32

#38Guest : Thanks giving and suggestion

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

#39adminPs : number pattern code

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("Enter number of rows\n");


scanf("%d",&n);

for( c =1; c <= n ; c++)

for( k =1; k <= c ; k++)

printf("%d ", k);

printf("\n");

return0;

09/08/2011 - 11:19

#40Guest : i need a code in C to print pyramid of stars

i need a code in C to print stars like this

*
* *
* * *
* * * *

please help?

05/08/2011 - 11:53

#41adminPs : c code to print pattern


#include<stdio.h>

main()

int n, c, k =2, j;

 
printf("Enter number of rows\n");

scanf("%d",&n);

for( j =1; j <= n ; j++)

for( c =1; c <=2*n-k ; c++)

printf(" ");

k = k +2;

for( c =1; c <= j ; c++)

printf("* ");

printf("\n");

getch();

return0;

08/08/2011 - 15:45

#42Guest : GET THE SOURCE CODE

GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:

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

c program for prime number


Prime number program in c: c program for prime number, this code prints prime
numbers using c programming language. To check whether a number is prime or not
see another code below. Prime number logic: a number is prime if it is divisible only by
one and itself. Remember two is the only even and also the smallest prime number.
First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many
applications in computer science and mathematics. A number greater than one can be
factorized into prime numbers, For example 540 = 2 2*33*51

Prime number program in c language


#include<stdio.h>
 
main()
{
int n, i = 3, count, c;
 
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
 
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
 
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
 
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.

There are many more efficient logic then written above.

C program for prime number or not


#include<stdio.h>
 
main()
{
int n, c = 2;
 
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
 
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
 
return 0;
}

C program for prime number using function


#include<stdio.h>
 
int check_prime(int);
 
main()
{
int n, result;
 
printf("Enter an integer to check whether it is prime or not.\n");
scanf("%d",&n);
 
result = check_prime(n);
 
if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);
 
return 0;
}
 
int check_prime(int a)
{
int c;
 
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}

armstrong number c program


armstrong number c program: c programming code to check whether a number is
armstrong or not. A number is armstrong if the sum of cubes of individual digits of a
number is equal to the number itself. For example 371 is an armstrong number as 3 3 +
73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.

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:

c program to swap two numbers


C program to swap two numbers with and without using third variable, swapping in c
using pointers and functions (Call by reference) , swapping means interchanging. For
example if in your c program you have taken two variable a and b where a = 4 and b =
5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers.
Swapping is used in sorting that is when we wish to arrange numbers in a particular
order either in ascending order or in descending order.

Swaping of two numbers in c


#include<stdio.h>
#include<conio.h>
 
main()
{
int x, y, temp;
 
printf("Enter the value of x and y ");
scanf("%d%d",&x, &y);
 
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
temp = x;
x = y;
y = temp;
 
printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
getch();
return 0;
}
Swapping of two numbers without third variable
You can also swap two numbers without using temp or temporary or third variable. In
that case c program will be as shown :-

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

Swap two numbers using pointers


#include<stdio.h>
 
main()
{
int x, y, *a, *b, temp;
 
printf("Enter the value of x and y ");
scanf("%d%d",&x,&y);
 
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
a = &x;
b = &y;
 
temp = *b;
*b = *a;
*a = temp;
 
printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
return 0;
}

Swapping numbers using call by reference


In this method we will make a function to swap numbers.

#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 program to generate and print armstrong numbers


armstrong number in c: This program prints armstrong number. In our program we ask
the user to enter a number and then we use a loop from one to the entered number and
check if it is an armstrong number and if it is then the number is printed on the screen.
Remember a number is armstrong if the sum of cubes of individual digits of a number is
equal to the number itself. For example 371 is an armstrong number as 3 3 + 73 + 13 =
371. Some other armstrong numbers are 0, 1, 153, 370, 407.

C code
#include<stdio.h>

#include<conio.h>

main()

int r;

long number =0, c, sum =0, temp;

 
printf("Enter the maximum range upto which you want to find armstrong
numbers ");

scanf("%ld",&number);

printf("Following armstrong numbers are found from 1 to


%ld\n",number);

for( c =1; c <= number ; c++)

temp = c;

while( temp !=0)

r = temp%10;

sum = sum + r*r*r;

temp = temp/10;

if( c == sum )

printf("%ld\n", c);

sum =0;

getch();

return0;

Armstrong number program executable.


Output of program

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.

Fibonacci series in c using for loop


/* Fibonacci Series c language */
#include<stdio.h>
 
main()
{
int n, first = 0, second = 1, next, c;
 
printf("Enter the number of terms\n");
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);
}
 
return 0;
}

Output of program:

Fibonacci series program in c using recursion


#include<stdio.h>
 
int fibonacci(int);
 
main()
{
int n, i = 0, c;
 
scanf("%d",&n);
 
printf("Fibonacci series\n");
 
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", fibonacci(i));
i++;
}
 
return 0;
}
 
int fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( fibonacci(n-1) + fibonacci(n-2) );
}

c program to print Floyd's triangle


C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of
rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle
are as follows :-
1
23
456
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

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:

c program to print Pascal triangle


Pascal Triangle in c: C program to print Pascal triangle which you might have studied in
Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered
by the user. First four rows of Pascal triangle are shown below :-

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

Pascal triangle program.

Output of program:

c program to add two numbers using pointers


This program performs addition of two numbers using pointers. In our program we have
two two integer variables x, y and two pointer variables p and q. Firstly we assign the
addresses of x and y to p and q respectively and then assign the sum of x and y to
variable sum. Note that & is address of operator and * is value at address operator.

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 program to find maximum element in array


This code find maximum or largest element present in an array. It also prints the
location or index at which maximum element occurs in array. This can also be done by
using pointers (see both codes).

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 using pointers


#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;
*maximum = *array;
 
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 program to find maximum element in array


This code find maximum or largest element present in an array. It also prints the
location or index at which maximum element occurs in array. This can also be done by
using pointers (see both codes).

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 using pointers


#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;
*maximum = *array;
 
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 program to find minimum element in array


C code to find minimum or smallest element present in an array. It also prints the
location or index at which minimum element occurs in array. This can also be done by
using pointers (see both the codes).

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.

Linear search c program


#include<stdio.h>
 
main()
{
int array[100], search, c, number;
 
printf("Enter the number of elements in array\n");
scanf("%d",&number);
 
printf("Enter %d numbers\n", number);
 
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
 
printf("Enter the number to search\n");
scanf("%d",&search);
 
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
 
return 0;
}

C program for binary search


Output of code:

Linear search for multiple occurrences


In the code below we will print all the locations at which required element is found and
also the number of times it occur in the list.

#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:

C program for linear search using function


#include<stdio.h>
 
int linear_search(int*, int, int);
 
main()
{
int array[100], search, c, n, position;
 
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);
 
position = linear_search(array, n, search);
 
if ( position == -1 )
printf("%d is not present in array.\n", search);
else
printf("%d is present at location %d.\n", search, position+1);
 
return 0;
}
 
int linear_search(int *pointer, int n, int find)
{
int c;
 
for ( c = 0 ; c < n ; c++ )
{
if ( *(pointer+c) == find )
return c;
}
 
return -1;
}

C program for binary search


C program for binary search: This code implements binary search in c language. It can
only be used for sorted arrays, but it's fast as compared to linear search. If you wish to
use binary search on an array which is not sorted then you must sort it using some
sorting technique say merge sort and then use binary search algorithm to find the
desired element in the list. If the element to be searched is found then its position is
printed.

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 program to reverse an array


c program to reverse an array :- This program reverses the array elements. For
example if a is an array of integers with three elements
such that
a[0] = 1
a[1] = 2
a[2] = 3
then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1

Given below is the c code to reverse an array .

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 program to reverse an array using pointers


#include<stdio.h>
#include<stdlib.h>
 
void reverse_array(int*, int);
 
main()
{
int n, c, *pointer;
 
scanf("%d",&n);
 
pointer = (int*)malloc(sizeof(int)*n);
 
if( pointer == NULL )
exit(EXIT_FAILURE);
 
for ( c = 0 ; c < n ; c++ )
scanf("%d",(pointer+c));
 
reverse_array(pointer, n);
 
printf("Original array on reversal is\n");
 
for ( c = 0 ; c < n ; c++ )
printf("%d\n",*(pointer+c));
 
free(pointer);
 
return 0;
}
 
void reverse_array(int *pointer, int n)
{
int *s, c, d;
 
s = (int*)malloc(sizeof(int)*n);
 
if( s == NULL )
exit(EXIT_FAILURE);
 
for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )
*(s+d) = *(pointer+c);
 
for ( c = 0 ; c < n ; c++ )
*(pointer+c) = *(s+c);
 
free(s);

c program to insert an element in an array


This code will insert an element into an array, For example consider an array a[10]
having three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to
insert a number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step
below so after insertion a[1] = 1 which was a[0] initially, and a[2] = 2 and a[3] = 3. Array
insertion does not mean increasing its size i.e array will not be containing 11 elements.

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;

c program for bubble sort


c program for bubble sort: c programming code for bubble sort to sort numbers or
arrange them in ascending order. You can easily modify it to print numbers in
descending order.

Bubble sort algorithm in c


/* Bubble sort code */
 
#include<stdio.h>
 
main()
{
int array[100], n, c, d, swap;
 
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]);
 
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
for ( d = 0 ; d < n - c - 1 ; d++ )
{
if ( array[d] > array[d+1] ) /* For decreasing order use <
*/
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
 
printf("Sorted list in ascending order:\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.

Insertion sort algorithm implementation in c


/* insertion sort ascending order */
 
#include<stdio.h>
 
main()
{
int array[100], n, c, d, swap, k;
 
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]);
 
for ( c = 1 ; c <= n - 1 ; c++ )
{
for ( d = 0 ; d <= c - 1 ; d++ )
{
if ( array[c] < array[d] )
{
swap = array[d];
array[d] = array[c];
 
for ( k = c ; k > d ; k-- )
array[k] = array[k-1];
 
array[k+1] = swap;
}
}
}
 
printf("Sorted list in ascending order:\n");
 
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
 
return 0;
}

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.

Selection sort algorithm implementation in c


#include<stdio.h>
 
main()
{
int array[100], n, c, d, position, swap;
 
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]);
 
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
position = c;
 
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
 
printf("Sorted list in ascending order:\n");
 
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
 
return 0;

c program to add two matrix


This c program add two matrices i.e. compute the sum of two matrices and then print it.
Firstly user will be asked to enter the order of matrix ( number of rows and columns )
and then two matrices. For example if the user entered order as 2, 2 i.e. two rows and
two columns and matrices as
First Matrix :-
12
34
Second matrix :-
45
-1 5
then output of the program ( sum of First and Second matrix ) will be
57
29

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

Add matrices program.

Sample output of the above program is shown in image below :-

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 program to transpose a matrix


This c program prints transpose of a matrix. It is obtained by interchanging rows and
columns of a matrix. For example if a matrix is
12
34
56
then transpose of above matrix will be
135
246
When we transpose a matrix then the order of matrix changes, but for a square matrix
order remains same.

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.

Matrix multiplication in c language


#include<stdio.h>
 
main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
 
printf("Enter the number of rows and columns of first 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 number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
 
if ( n != p )
printf("Matrices with entered orders can't be multiplied with
each other.\n");
else
{
printf("Enter the elements of second matrix\n");
 
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d",&second[c][d]);
 
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
 
multiply[c][d] = sum;
sum = 0;
}
}
 
printf("Product of entered matrices:-\n");
 
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t",multiply[c][d]);
 
printf("\n");
}
}
 
return 0;
}

Matrix multiplication program executable.


Output of program - 3x3 matrix multiplication in c:

c program print string


This program print a string. String can be printed by using various functions such as
printf, puts.

C programming code
#include<stdio.h>
 
main()
{
char array[20] = "Hello World";
 
printf("%s\n",array);
 
return 0;
}

To input a string we use scanf function.

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.

C program to find string length


#include<stdio.h>
#include<string.h>
 
main()
{
char a[100];
int length;
 
printf("Enter a string to calculate it's length\n");
gets(a);
 
length = strlen(a);
 
printf("Length of entered string is = %d\n",length);
 
return 0;
}

String length program executable.

Output of program:

You can also find string length using pointer or without strlen function. Following
program shows how to achieve this.

C program to find string length without strlen


C program to find length of a string using pointers.

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

Function to find string length


int string_length(char *s)
{
int c = 0;
 
while(*(s+c))
c++;
 
return c;
}

c program to compare two strings


This c program compares two strings using strcmp, without strcmp and using pointers.
For comparing strings without using library function see another code below.

C program to compare two strings using strcmp


#include<stdio.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);
 
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
 
return 0;
}

C program to compare two strings without using


strcmp
Here we create our own function to compare strings.

int compare(char a[], char b[])


{
int c = 0;
 
while( a[c] == b[c] )
{
if( a[c] == '\0' || b[c] == '\0' )
break;
c++;
}
if( a[c] == '\0' && b[c] == '\0' )
return 0;
else
return -1;
}

C program to compare two strings using pointers


In this method we will make our own function to perform string comparison, we will use
character pointers in our function to manipulate string.

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

string copying in c programming


This program copy string using library function strcpy, to copy string without using strcpy
see source code below in which we have made our own function to copy string.

C program to copy a string


#include<stdio.h>
#include<string.h>
 
main()
{
char source[] = "C program";
char destination[50];
 
strcpy(destination, source);
 
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
 
return 0;
}

c program to copy a string using pointers


: here we copy string without using strcmp by creating our own function which uses
pointers.

#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 program to concatenate strings


This program concatenates strings, for example if the first string is "c " and second
string is "program" then on concatenating these two strings we get the string "c
program". To concatenate two strings we use strcat function of string.h, to concatenate
without using library function see another code below which uses pointers.

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:

String concatenation without strcat


#include<stdio.h>
 
void concatenate_string(char*, char*);
 
main()
{
char original[100], add[100];
 
printf("Enter source string\n");
gets(original);
 
printf("Enter string to concatenate\n");
gets(add);
 
concatenate_string(original, add);
 
printf("String after concatenation is \"%s\"\n", original);
 
return 0;
}
 
void concatenate_string(char *original, char *add)
{
while(*original)
original++;
 
while(*add)
{
*original = *add;
add++;
original++;
}
*original = '\0';
}

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

C program to reverse a string using pointers


: Now we will invert string using pointers or without using library function strrev.

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

C program to reverse a string using recursion


#include<stdio.h>
#include<string.h>
 
void reverse(char*,int,int);
 
main()
{
char a[100];
 
gets(a);
 
reverse(a, 0, strlen(a)-1);
 
printf("%s\n",a);
 
return 0;
}
 
void reverse(char *x, int beg, int end)
{
char a, b, c;
 
if ( beg >= end )
return;
 
c = *(x+beg);
*(x+beg) = *(x+end);
*(x+end) = c;
 
reverse(x, ++beg, --end);
}

Reverse string program executable.


Output of program:

c palindrome program, c program for palindrome


C program for palindrome or palindrome in c programming: palindrome program in c
language, c code to check if a string is a palindrome or not and for palindrome number.
This program works as follows :- at first we copy the entered string into a new string,
and then we reverse the new string and then compares it with original string. If both of
them have same sequence of characters i.e. they are identical then the entered string is
a palindrome otherwise not. To perform copy, reverse and compare operations we use
strcpy, strrev and strcmp functions of string.h respectively, if you do not wish to use
these functions see c programming code for palindrome without using string functions.
Some palindrome strings examples are "dad", "radar", "madam" etc.

C program for palindrome


#include <stdio.h>
#include <string.h>
 
main()
{
char a[100], b[100];
 
printf("Enter the string to check if it is a palindrome\n");
gets(a);
 
strcpy(b,a);
strrev(b);
 
if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
 
return 0;
}

Download palindrome executable


Output of program:

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 program for palindrome without using string


functions
#include <stdio.h>
#include <string.h>
 
main()
{
char text[100];
int begin, middle, end, length = 0;
 
gets(text);
 
while ( text[length] != '\0' )
length++;
 
end = length - 1;
middle = length/2;
 
for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.\n");
 
return 0;
}

C program check palindrome


#include <stdio.h>
 
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
 
main()
{
char string[100];
int result;
 
printf("Enter a string\n");
gets(string);
 
result = is_palindrome(string);
 
if ( result == 1 )
printf("\"%s\" is a palindrome string.\n", string);
else
printf("\"%s\" is not a palindrome string.\n", string);
 
return 0;
}
 
int is_palindrome(char *string)
{
int check, length;
char *reverse;
 
length = string_length(string);
reverse = (char*)malloc(length+1);
 
copy_string(reverse, string);
reverse_string(reverse);
 
check = compare_string(string, reverse);
 
free(reverse);
 
if ( check == 0 )
return 1;
else
return 0;
}
 
int string_length(char *string)
{
int length = 0;
 
while(*string)
{
length++;
string++;
}
 
return length;
}
 
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}
 
void reverse_string(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 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;
}

remove vowels string c


Remove vowels string c: c program to remove or delete vowels from a string, if the input
string is "c programming" then output will be "c prgrmmng". In the program we create a
new string and process entered string character by character, and if a vowel is found it
is not added to new string otherwise the character is added to new string, after the
string ends we copy the new string into original string. Finally we obtain a string without
any vowels.

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 substring program output:

C code for all substrings of a string


#include<stdio.h>
#include<string.h>
#include<malloc.h>
 
char* substring(char*, int, int);
 
main()
{
char string[100], *pointer;
int position = 1, length = 1, temp, string_length;
 
printf("Enter a string\n");
gets(string);
 
temp = string_length = strlen(string);
 
printf("Substring of \"%s\" are\n", string);
 
while ( position <= string_length )
{
while ( length <= temp )
{
pointer = substring(string, position, length);
printf("%s\n", pointer);
free(pointer);
length++;
}
temp--;
position++;
length = 1;
}
 
return 0;
}
 
/* Use substring function given in above c program*/

Substring code output:

c program to sort a string in alphabetic order


C program to sort a string in alphabetic order: For example if user will enter a string
"programming" then output will be "aggimmnoprr" or output string will contain characters
in alphabetical order.
C programming code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void sort_string(char*);
 
main()
{
char string[100];
 
printf("Enter some text\n");
gets(string);
 
sort_string(string);
printf("%s\n", string);
 
return 0;
}
 
void sort_string(char *s)
{
int c, d = 0, length;
char *pointer, *result, ch;
 
length = strlen(s);
 
result = (char*)malloc(length+1);
 
pointer = s;
 
for ( ch = 'a' ; ch <= 'z' ; ch++ )
{
for ( c = 0 ; c < length ; c++ )
{
if ( *pointer == ch )
{
*(result+d) = *pointer;
d++;
}
pointer++;
}
pointer = s;
}
*(result+d) = '\0';
 
strcpy(s, result);
free(result);
}
c program remove spaces, blanks from a string
C code to remove spaces or excess blanks from a string, For example consider the
string

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

Change string to upper case without strupr


#include<stdio.h>

void upperString(char*);

main()
{
char string[100];
printf("Enter a string to convert it into upper case\n");
gets(string);

upperString(string);

printf("Entered string in upper case is \"%s\"\n", string);

return 0;
}

void upperString(char *string)


{
while(*string)
{
if ( *string >= 'a' && *string <= 'z' )
{
*string = *string - 32;
}
string++;
}
}

Change string to lower case without strlwr

#include<stdio.h>

void lowerString(char*);

main()
{
char string[100];

printf("Enter a string to convert it into lower case\n");


gets(string);

lowerString(string);

printf("Entered string in lower case is \"%s\"\n", string);

return 0;
}

void lowerString(char *string)


{
while(*string)
{
if ( *string >= 'A' && *string <= 'Z' )
{
*string = *string + 32;
}
string++;
}
}

c program to swap two strings


C program to swap strings.

C programming code
#include<stdio.h>

#include<string.h>

#include<malloc.h>

#include<conio.h>

main()

char first[100], second[100],*temp;

printf("Enter the first string ");

gets(first);

printf("Enter the second string ");

gets(second);

printf("\nBefore Swapping\n");

printf("First string: %s\n",first);

printf("Second string: %s\n\n",second);

temp =(char*)malloc(100);

 
strcpy(temp,first);

strcpy(first,second);

strcpy(second,temp);

printf("After Swapping\n");

printf("First string: %s\n",first);

printf("Second string: %s\n",second);

getch();

return0;

Output of program

c program to find frequency of characters in a string


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

C programming code
#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.

C anagram programming code


#include<stdio.h>
 
int check(char [], char []);
 
main()
{
char a[100], b[100];
int flag;
 
printf("Enter first string\n");
gets(a);
 
printf("Enter second string\n");
gets(b);
 
flag = check(a, b);
 
if ( flag == 1 )
printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
 
return 0;
}
 
int check(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
 
while ( a[c] != '\0' )
{
first[a[c]-'a']++;
c++;
}
 
c = 0;
 
while ( b[c] != '\0' )
{
second[b[c]-'a']++;
c++;
}
 
for ( c = 0 ; c < 26 ; c++ )
{
if( first[c] != second[c] )
return 0;
}
 
return 1;
}
c program to read a file
C program to read a file :- This program reads a file entered by the user and displays it's
contents on the screen, fopen function is used to open a file, it returns a pointer to
structure FILE. FILE is a pre defined structure in stdio.h . If the file is successfully
opened then fopen returns a pointer to file and if it is unable to open a file then it returns
NULL. fgetc function returns a character which is read from the file and fclose function
closes the file. Opening a file means we bring file from disk to ram to perform operations
on it.

C program to open a file


C programmming code to open a file and to print it contents on screen.

#include<stdio.h>

#include<stdlib.h>

main()

char ch, file_name[25];

FILE *fp;

printf("Enter the name of file you wish to see ");

gets(file_name);

fp =fopen(file_name,"r");// read mode

if( fp == NULL )

perror("Error while opening the file.\n");

exit(EXIT_FAILURE);

}
 

printf("The contents of %s file are :- \n\n", file_name);

while(( ch =fgetc(fp))!= EOF )

printf("%c",ch);

fclose(fp);

return0;

Read file program executable.

Output of program

c program to copy files


This program copies a file, firstly you will specify the file to copy and then you will enter
the name of target file, You will have to mention the extension of file also. We will open
the file that we wish to copy in read mode and target file in write mode.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
char ch, file1[20], file2[20];
FILE *fs,*ft;

printf("Enter name of file to copy ");


gets(file1);

fs = fopen(file1,"r");
if( fs == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}

printf("Enter name of target file ");


gets(file2);

ft = fopen(file2,"w");
if( ft == NULL )
{
perror("Error ");
fclose(fs);
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}

while( ( ch = fgetc(fs) ) != EOF )


fputc(ch,ft);

printf("File copied successfully.\n");

getch();
fclose(fs);
fclose(ft);
return 0;
}

File copy program executable.


Output of the above program is shown in image below :-

c program to merge two files


This c program merges two files and store their contents in an another file. The files
which are to be merged are opened in read mode and the file which contains content of
both the files is opened in write mode. To merge two files first we open a file and read it
character by character and store the read contents in another file then we read the
contents of another file and store it in file, we read two files until EOF ( end of file ) is
reached.

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

Click here to download executable file of the above program.


Output of above program is shown in image below :-

c program to list files in directory


This program list all files present in a directory/folder in which this executable file is
present. For example if this executable file is present in C:\\TC\\BIN then it will lists all
the files present in C:\\TC\\BIN.

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

Download list files program executable.

Output c program to list files is shown in image below :-

c program to delete a file


This c program deletes a file which is entered by the user, the file to be deleted should
be present in the directory in which the executable file of this program is present.
Extension of the file should also be entered, also note that deleted file doesn't go to
recycle bin, remove macro is used to delete the file. If there is an error in deleting the
file then an error will be displayed using perror function.

C code
#include<stdio.h>
#include<conio.h>

int main()
{
int status;
char fname[25];

printf("Enter the name of file you wish to delete ");


gets(fname);

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

Delete file program executable.

Sample output of the above program is shown in image below :-

c program to generate random numbers


This c program generates random numbers using random function, randomize function
is used to initialize random number generator. If you don't use randomize function then
you will get same random numbers each time you run the program.
C programming code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
int n, max, num, c;
 
printf("Enter the number of random numbers you want ");
scanf("%d",&n);
 
printf("Enter the maximum value of random number ");
scanf("%d",&max);
 
printf("%d random numbers from 0 to %d are :-\n",n,max);
randomize();
 
for ( c = 1 ; c <= n ; c++ )
{
num = random(max);
printf("%d\n",num);
 
}
 
getch();
return 0;

c program to add two complex numbers


c program to add two complex numbers :- This program calculate the sum of two
complex numbers which will be entered by the user and then printed their sum. User will
have to enter the real and imaginary parts of two complex numbers. Then on our
program we will add real parts and imaginary parts of complex numbers and then we
add them and prints the complex number, i is the symbol used for iota. For example if
user entered two complex numbers as
(1 + 2i) and (4 + 6 i) then output of program will be (5+8i).

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

Download add complex numbers program executable.


Output of c program to add two complex numbers is shown in image below :-

c program to print date


This c program prints current system date. To print date we will use getdate function.

C programming code
#include<stdio.h>

#include<conio.h>

#include<dos.h>

main()

struct date d;

getdate(&d);

printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);


getch();

return0;

Output of program

c program to get ip address


This c program prints ip (internet protocol) address of your computer, system function is
used to execute the command ipconfig which prints ip address, subnet mask and
default gateway. The code given below works for Windows xp and Windows 7. If you
are using turbo c compiler then execute program from folder, it may not work when you
are working in compiler and press Ctrl+F9 to run your program.

C programming code
#include<stdlib.h>
 
main()
{
system("C:\\Windows\\System32\\ipconfig");
system("pause");
 
return 0;
}
IP address program executable.

Output of program: ( In Windows XP )

C program to shutdown or turn off computer


C Program to shutdown your computer :- This program turn off i.e shutdown your
computer system. Firstly it will asks you to shutdown your computer if you press 'y' the
your computer will shutdown in 30 seconds, system function of "stdlib.h" is used to run
an executable file shutdown.exe which is present in C:\WINDOWS\system32 in
windows-xp. You can use various options while executing shutdown.exe for example -s
option shutdown the computer after 30 seconds, if you wish to shutdown immediately
then you can write "shutdown -s -t 0" as an argument to system function. If you wish to
restart your computer then you can write "shutdown -r".

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.

C programming code for Windows XP


#include<stdio.h>
#include<stdlib.h>
 
main()
{
char ch;
 
printf("Do you want to shutdown your computer now (y/n) ");
scanf("%c",&ch);
 
if( ch == 'y' || ch == 'Y' )
system("C:\\WINDOWS\\System32\\shutdown -s");
 
return 0;
}

C programming code for Windows 7


#include<stdio.h>
#include<stdlib.h>
 
main()
{
char ch;
 
printf("Do you want to shutdown your computer now (y/n)\n");
scanf("%c",&ch);
 
if( ch == 'y' || ch == 'Y' )
system("C:\\WINDOWS\\System32\\shutdown /s");
 
return 0;
}

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


use /r instead of /s.

You might also like