Sie sind auf Seite 1von 103

1.

Hello world:
C hello world program: c programming language code to print hello world. This program
prints hello world, printf library function is used to display text on screen, '\n' places cursor
on the beginning of next line, stdio.h header file contains declaration of printf function. The
code will work on all operating systems may be its Linux, Mac or any other and compilers.
To learn a programming language you must start writing programs in it and may be your
first c code while learning programming.
Hello world in C language
//C hello world example
#i ncl ude <st di o. h>
i nt mai n( )
{
pr i nt f ( " Hel l o wor l d\n" ) ;
r et ur n 0;
}
Purpose of Hello world program may be to say hello to people or the users of your software
or application.
Output of program:
2. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt a;
pr i nt f ( " Ent er an i nt eger \n" ) ;
scanf ( " %d" , &a) ;
pr i nt f ( " I nt eger t hat you have ent er ed i s %d\n" , a) ;
r et ur n 0;
}
Download integer program.
Output of program:
In c language we have data type for different types of data, for integer data it is int, for
character date char, for floating point data it's float and so on.
3. 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
#i ncl ude<st di o. h>
i nt mai n( )
{
i nt a, b, c;
pr i nt f ( " Ent er t wo number s t o add\n" ) ;
scanf ( " %d%d" , &a, &b) ;
c = a + b;
pr i nt f ( " Sumof ent er ed number s = %d\n" , c) ;
r et ur n 0;
}
Output of program:
Addition without using third variable
#i ncl ude<st di o. h>
mai n( )
{
i nt 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 somewhere in code considering it
* as it was entered by the user.
*/
pr i nt f ( " Sumof a and b = %d\n" , a) ;
r et ur n 0;
}
C program to add two numbers repeatedly
#i ncl ude<st di o. h>
mai n( )
{
i nt a, b, c;
char ch;
whi l e( 1)
{
pr i nt f ( " Ent er val ues of a and b\n" ) ;
scanf ( " %d%d" , &a, &b) ;
c = a + b;
pr i nt f ( " a + b = %d\n" , c) ;
pr i nt f ( " Do you wi sh t o add mor e number s( y/ n) \n" ) ;
scanf ( " %c" , &ch) ;
i f ( ch == ' y' | | ch == ' Y' )
cont i nue;
el se
break;
}
r et ur n 0;
}
Adding numbers in c using function
#i ncl ude<st di o. h>
l ong addi t i on( l ong, l ong) ;
mai n( )
{
l ong f i r st , second, sum;
scanf ( " %l d%l d" , &f i r st , &second) ;
sum= addi t i on( f i r st , second) ;
pr i nt f ( " %l d\n" , sum) ;
r et ur n 0;
}
l ong addi t i on( l ong a, l ong b)
{
l ong r esul t ;
r esul t = a + b;
r et ur n r esul t ;
}
We have used long data type as it can handle large numbers, if you want to add still larger
numbers which doesn't fit in long range then use array, string or other data structure.
4. C program to check odd or even
c program to check odd or even: We will determine whether a number is odd or even by
using different methods all are provided with a code in c language. As you have study in
mathematics that in decimal number system even numbers are divisible by 2 while odd are
not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1
( remainder when four is divided by three). Even numbers are of the form 2*p and odd are
of the form (2*p+1) where p is is an integer.
C program to check odd or even using modulus
operator
#i ncl ude<st di o. h>
mai n( )
{
i nt n;
pr i nt f ( " Ent er an i nt eger \n" ) ;
scanf ( " %d" , &n) ;
i f ( n%2 == 0 )
pr i nt f ( " Even\n" ) ;
el se
pr i nt f ( " Odd\n" ) ;
r et ur n 0;
}
We can use bitwise AND (&) operator to check odd or even, as an example consider binary
of 7 (0111) when we perform 7 & 1 the result will be one and you may observe that the least
significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also (
even_number & 1 ) is zero.
C program to check odd or even using bitwise
operator
#i ncl ude<st di o. h>
mai n( )
{
i nt n;
pr i nt f ( " Ent er an i nt eger \n" ) ;
scanf ( " %d" , &n) ;
i f ( n & 1 == 1 )
pr i nt f ( " Odd\n" ) ;
el se
pr i nt f ( " Even\n" ) ;
r et ur n 0;
}
Find odd or even using conditional operator
#i ncl ude<st di o. h>
mai n( )
{
i nt n;
pr i nt f ( " I nput an i nt eger \n" ) ;
scanf ( " %d" , &n) ;
n%2 == 0 ? pr i nt f ( " Even\n" ) : pr i nt f ( " Odd\n" ) ;
r et ur n 0;
}
C program to check odd or even without using
bitwise or modulus operator
#i ncl ude<st di o. h>
mai n( )
{
i nt n;
pr i nt f ( " Ent er an i nt eger \n" ) ;
scanf ( " %d" , &n) ;
i f ( ( n/ 2) *2 == n )
pr i nt f ( " Even\n" ) ;
el se
pr i nt f ( " Odd\n" ) ;
r et ur n 0;
}
In c programming language when we divide two integers we get an integer result, For
example the result of 7/3 will be 2.So we can take advantage of this and may use it to find
whether the number is odd or even. Consider an integer n we can first divide by 2 and then
multiply it by 2 if the result is the original number then the number is even otherwise the
number is odd. For example 11/2 = 5, 5*2 = 10 ( which is not equal to eleven), now consider
12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help you
in finding if a number is odd or not.
5. C program to perform addition, subtraction, multiplication
and division
C program to perform basic arithmetic operations which are addition, subtraction,
multiplication and division of two numbers. Numbers are assumed to be integers and will be
entered by the user.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt f i r st , second, add, subt r act , mul t i pl y;
f l oat di vi de;
pr i nt f ( " Ent er t wo i nt eger s\n" ) ;
scanf ( " %d%d" , &f i r st , &second) ;
add = f i r st + second;
subt r act = f i r st - second;
mul t i pl y = f i r st * second;
di vi de = f i r st / ( f l oat ) second; //typecasting
pr i nt f ( " Sum= %d\n" , add) ;
pr i nt f ( " Di f f er ence = %d\n" , subt r act ) ;
pr i nt f ( " Mul t i pl i cat i on = %d\n" , mul t i pl y) ;
pr i nt f ( " Di vi si on = %. 2f \n" , di vi de) ;
r et ur n 0;
}
Download Arithmetic operations program.
Output of program:
In c language when we divide two integers we get integer result for example 5/2 evaluates to
2. As a general rule integer/integer = integer and float/integer = float or integer/float =
float. So we convert denominator to float in our program, you may also write float in
numerator. This explicit conversion is known as typecasting.
6. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
char ch;
pr i nt f ( " Ent er a char act er \n" ) ;
scanf ( " %c" , &ch) ;
i f ( ch == ' a' | | ch == ' A' | | ch == ' e' | | ch == ' E' | | ch == ' i ' | | ch ==
' I ' | | ch ==' o' | | ch==' O' | | ch == ' u' | | ch == ' U' )
pr i nt f ( " %c i s a vowel . \n" , ch) ;
el se
pr i nt f ( " %c i s not a vowel . \n" , ch) ;
r et ur n 0;
}
Output of program:
Check vowel using switch statement
#i ncl ude <st di o. h>
i nt mai n( )
{
char ch;
pr i nt f ( " I nput a char act er \n" ) ;
scanf ( " %c" , &ch) ;
swi t ch( ch)
{
case ' a' :
case ' A' :
case ' e' :
case ' E' :
case ' i ' :
case ' I ' :
case ' o' :
case ' O' :
case ' u' :
case ' U' :
pr i nt f ( " %c i s a vowel . \n" , ch) ;
break;
def aul t :
pr i nt f ( " %c i s not a vowel . \n" , ch) ;
}
r et ur n 0;
}
Function to check vowel
i nt check_vowel ( char a)
{
i f ( a >= ' A' && a <= ' Z' )
a = a + ' a' - ' A' ; /* Converting to lower case or use a = a + 32 */
i f ( a == ' a' | | a == ' e' | | a == ' i ' | | a == ' o' | | a == ' u' )
r et ur n 1;
r et ur n 0;
}
This function can also be used to check if a character is a consonant or not, if it's not a vowel
then it will be a consonant, but make sure that the character is an alphabet not a special
character.
7. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt year ;
pr i nt f ( " Ent er a year t o check i f i t i s a l eap year \n" ) ;
scanf ( " %d" , &year ) ;
i f ( year %400 == 0)
pr i nt f ( " %d i s a l eap year . \n" , year ) ;
el se i f ( year %100 == 0)
pr i nt f ( " %d i s not a l eap year . \n" , year ) ;
el se i f ( year %4 == 0 )
pr i nt f ( " %d i s a l eap year . \n" , year ) ;
el se
pr i nt f ( " %d i s not a l eap year . \n" , year ) ;
r et ur n 0;
}
Download Leap year program.
Output of program:
Please read the leap year article at Wikipedia, it will help you to understand the program.
This code is based on Gregorian Calendar.
8. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, sum= 0, r emai nder ;
pr i nt f ( " Ent er an i nt eger \n" ) ;
scanf ( " %d" , &n) ;
whi l e( n ! = 0)
{
r emai nder = n %10;
sum= sum+ r emai nder ;
n = n / 10;
}
pr i nt f ( " Sumof di gi t s of ent er ed number = %d\n" , sum) ;
r et ur n 0;
}
For example if the input is 98, sum(variable) is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in c whenever we divide integer by another integer we get an integer.
9%10 = 9
sum = 8(previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, loop ends we get the required sum.
Output of program:
Add digits using recursion
#i ncl ude <st di o. h>
i nt add_di gi t s( i nt ) ;
i nt mai n( )
{
i nt n, r esul t ;
scanf ( " %d" , &n) ;
r esul t = add_di gi t s( n) ;
pr i nt f ( " %d\n" , r esul t ) ;
r et ur n 0;
}
i nt add_di gi t s( i nt n) {
st at i c i nt sum= 0;
i f ( n == 0) {
r et ur n 0;
}
sum= n%10 + add_di gi t s( n/ 10) ;
r et ur n sum;
}
Static variable sum is used and is initialized to 0, it' value will persists after function calls i.e.
it is initialized only once when a first call to function is made.
9. Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are
given, first one uses for loop, second uses a function to find factorial and third
using recursion. Factorial is represented using '!', so five factorial will be written as (5!), n
factorial as (n!). Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.
Factorial program in c using for loop
Here we find factorial using for loop.
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt c, n, f act = 1;
pr i nt f ( " Ent er a number t o cal cul at e i t ' s f act or i al \n" ) ;
scanf ( " %d" , &n) ;
f or ( c = 1; c <= n; c++)
f act = f act * c;
pr i nt f ( " Fact or i al of %d = %d\n" , n, f act ) ;
r et ur n 0;
}
Output of code:
Factorial program in c using function
#i ncl ude <st di o. h>
l ong f act or i al ( i nt ) ;
i nt mai n( )
{
i nt number ;
l ong f act = 1;
pr i nt f ( " Ent er a number t o cal cul at e i t ' s f act or i al \n" ) ;
scanf ( " %d" , &number ) ;
pr i nt f ( " %d! = %l d\n" , number , f act or i al ( number ) ) ;
r et ur n 0;
}
l ong f act or i al ( i nt n)
{
i nt c;
l ong r esul t = 1;
f or ( c = 1; c <= n; c++)
r esul t = r esul t * c;
r et ur n r esul t ;
}
Factorial program in c using recursion
#i ncl ude<st di o. h>
l ong f act or i al ( i nt ) ;
i nt mai n( )
{
i nt n;
l ong f ;
pr i nt f ( " Ent er an i nt eger t o f i nd f act or i al \n" ) ;
scanf ( " %d" , &n) ;
i f ( n < 0)
pr i nt f ( " Negat i ve i nt eger s ar e not al l owed. \n" ) ;
el se
{
f = f act or i al ( n) ;
pr i nt f ( " %d! = %l d\n" , n, f ) ;
}
r et ur n 0;
}
l ong f act or i al ( i nt n)
{
i f ( n == 0)
r et ur n 1;
el se
r et ur n( n * f act or i al ( n- 1) ) ;
}
Recursion is a technique in which a function calls itself, for example in above code factorial
function is calling itself. To solve a problem using recursion you must first express its
solution in recursive form.
10. C program to find hcf and lcm
C program to find hcf and lcm: The code below finds highest common factor and least
common multiple of two integers. HCF is also known as greatest common divisor(GCD) or
greatest common factor(gcf).
C programming code
#i ncl ude <st di o. h>
i nt mai n( ) {
i nt a, b, x, y, t , gcd, l cm;
pr i nt f ( " Ent er t wo i nt eger s\n" ) ;
scanf ( " %d%d" , &x, &y) ;
a = x;
b = y;
whi l e ( b ! = 0) {
t = b;
b = a %b;
a = t ;
}
gcd = a;
l cm= ( x*y) / gcd;
pr i nt f ( " Gr eat est common di vi sor of %d and %d = %d\n" , x, y, gcd) ;
pr i nt f ( " Least common mul t i pl e of %d and %d = %d\n" , x, y, l cm) ;
r et ur n 0;
}
Output of program:
C program to find hcf and lcm using recursion
#i ncl ude <st di o. h>
l ong gcd( l ong, l ong) ;
i nt mai n( ) {
l ong x, y, hcf , l cm;
pr i nt f ( " Ent er t wo i nt eger s\n" ) ;
scanf ( " %l d%l d" , &x, &y) ;
hcf = gcd( x, y) ;
l cm= ( x*y) / hcf ;
pr i nt f ( " Gr eat est common di vi sor of %l d and %l d = %l d\n" , x, y, hcf ) ;
pr i nt f ( " Least common mul t i pl e of %l d and %l d = %l d\n" , x, y, l cm) ;
r et ur n 0;
}
l ong gcd( l ong a, l ong b) {
i f ( b == 0) {
r et ur n a;
}
el se {
r et ur n gcd( b, a %b) ;
}
}
C program to find hcf and lcm using function
#i ncl ude <st di o. h>
l ong gcd( l ong, l ong) ;
i nt mai n( ) {
l ong x, y, hcf , l cm;
pr i nt f ( " Ent er t wo i nt eger s\n" ) ;
scanf ( " %l d%l d" , &x, &y) ;
hcf = gcd( x, y) ;
l cm= ( x*y) / hcf ;
pr i nt f ( " Gr eat est common di vi sor of %l d and %l d = %l d\n" , x, y, hcf ) ;
pr i nt f ( " Least common mul t i pl e of %l d and %l d = %l d\n" , x, y, l cm) ;
r et ur n 0;
}
l ong gcd( l ong x, l ong y) {
i f ( x == 0) {
r et ur n y;
}
whi l e ( y ! = 0) {
i f ( x > y) {
x = x - y;
}
el se {
y = y - x;
}
}
r et ur n x;
}
11. Decimal to binary conversion
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, c, k;
pr i nt f ( " Ent er an i nt eger i n deci mal number syst em\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " %d i n bi nar y number syst emi s: \n" , n) ;
f or ( c = 31; c >= 0; c- - )
{
k = n >> c;
i f ( k & 1)
pr i nt f ( " 1" ) ;
el se
pr i nt f ( " 0" ) ;
}
pr i nt f ( " \n" ) ;
r et ur n 0;
}
Output of program:
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
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
char *deci mal _t o_bi nar y( i nt ) ;
mai n( )
{
i nt n, c, k;
char *poi nt er ;
pr i nt f ( " Ent er an i nt eger i n deci mal number syst em\n" ) ;
scanf ( " %d" , &n) ;
poi nt er = deci mal _t o_bi nar y( n) ;
pr i nt f ( " Bi nar y st r i ng of %d i s: %s\n" , n, t ) ;
f r ee( poi nt er ) ;
r et ur n 0;
}
char *deci mal _t o_bi nar y( i nt n)
{
i nt c, d, count ;
char *poi nt er ;
count = 0;
poi nt er = ( char *) mal l oc( 32+1) ;
i f ( poi nt er == NULL )
exi t ( EXI T_FAI LURE) ;
f or ( c = 31 ; c >= 0 ; c- - )
{
d = n >> c;
i f ( d & 1 )
*( poi nt er +count ) = 1 + ' 0' ;
el se
*( poi nt er +count ) = 0 + ' 0' ;
count ++;
}
*( poi nt er +count ) = ' \0' ;
r et ur n poi nt er ;
}
Memory is allocated dynamically because we can't return a pointer to a local variable (character
array in this case). If we return a pointer to local variable then program may crash or we get
incorrect result.
12. 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
#i ncl ude<st di o. h>
l ong f act or i al ( i nt ) ;
l ong f i nd_ncr ( i nt , i nt ) ;
l ong f i nd_npr ( i nt , i nt ) ;
mai n( )
{
i nt n, r ;
l ong ncr , npr ;
pr i nt f ( " Ent er t he val ue of n and r \n" ) ;
scanf ( " %d%d" , &n, &r ) ;
ncr = f i nd_ncr ( n, r ) ;
npr = f i nd_npr ( n, r ) ;
pr i nt f ( " %dC%d = %l d\n" , n, r , ncr ) ;
pr i nt f ( " %dP%d = %l d\n" , n, r , npr ) ;
r et ur n 0;
}
l ong f i nd_ncr ( i nt n, i nt r )
{
l ong r esul t ;
r esul t = f act or i al ( n) / ( f act or i al ( r ) *f act or i al ( n- r ) ) ;
r et ur n r esul t ;
}
l ong f i nd_npr ( i nt n, i nt r )
{
l ong r esul t ;
r esul t = f act or i al ( n) / f act or i al ( n- r ) ;
r et ur n r esul t ;
}
l ong f act or i al ( i nt n)
{
i nt c;
l ong r esul t = 1;
f or ( c = 1 ; c <= n ; c++ )
r esul t = r esul t *c;
r et ur n ( r esul t ) ;
}
Output of program:
13. 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 the first c program to add numbers we are not using an array, and using
array in the second code.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, sum= 0, c, val ue;
pr i nt f ( " Ent er t he number of i nt eger s you want t o add\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d i nt eger s\n" , n) ;
f or ( c = 1; c <= n; c++)
{
scanf ( " %d" , &val ue) ;
sum= sum+ val ue;
}
pr i nt f ( " Sumof ent er ed i nt eger s = %d\n" , sum) ;
r et ur n 0;
}
Output of program:
C programming code using array
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, sum= 0, c, ar r ay[ 100] ;
scanf ( " %d" , &n) ;
f or ( c = 0; c < n; c++)
{
scanf ( " %d" , &ar r ay[ c] ) ;
sum= sum+ ar r ay[ c] ;
}
pr i nt f ( " Sum= %d\n" , sum) ;
r et ur n 0;
}
14. C program to swap two numbers
C program to swap two numbers with and without using third variable, swapping in c
using pointers, functions (Call by reference) and using bitwise XOR operator, 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 of two numbers in c
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt x, y, t emp;
pr i nt f ( " Ent er t he val ue of x and y\n" ) ;
scanf ( " %d%d" , &x, &y) ;
pr i nt f ( " Bef or e Swappi ng\nx = %d\ny = %d\n" , x, y) ;
t emp = x;
x = y;
y = t emp;
pr i nt f ( " Af t er Swappi ng\nx = %d\ny = %d\n" , x, y) ;
r et ur n 0;
}
Output of program:
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 :-
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt a, b;
pr i nt f ( " Ent er t wo i nt eger s t o swap\n" ) ;
scanf ( " %d%d" , &a, &b) ;
a = a + b;
b = a - b;
a = a - b;
pr i nt f ( " a = %d\nb = %d\n" , a, b) ;
r et ur n 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt x, y, *a, *b, t emp;
pr i nt f ( " Ent er t he val ue of x and y\n" ) ;
scanf ( " %d%d" , &x, &y) ;
pr i nt f ( " Bef or e Swappi ng\nx = %d\ny = %d\n" , x, y) ;
a = &x;
b = &y;
t emp = *b;
*b = *a;
*a = t emp;
pr i nt f ( " Af t er Swappi ng\nx = %d\ny = %d\n" , x, y) ;
r et ur n 0;
}
Swapping numbers using call by reference
In this method we will make a function to swap numbers.
#i ncl ude <st di o. h>
voi d swap( i nt *, i nt *) ;
i nt mai n( )
{
i nt x, y;
pr i nt f ( " Ent er t he val ue of x and y\n" ) ;
scanf ( " %d%d" , &x, &y) ;
pr i nt f ( " Bef or e Swappi ng\nx = %d\ny = %d\n" , x, y) ;
swap( &x, &y) ;
pr i nt f ( " Af t er Swappi ng\nx = %d\ny = %d\n" , x, y) ;
r et ur n 0;
}
voi d swap( i nt *a, i nt *b)
{
i nt t emp;
t emp = *b;
*b = *a;
*a = t emp;
}
C programming code to swap using bitwise XOR
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt x, y;
scanf ( " %d%d" , &x, &y) ;
pr i nt f ( " x = %d\ny = %d\n" , x, y) ;
x = x ^ y;
y = x ^ y;
x = x ^ y;
pr i nt f ( " x = %d\ny = %d\n" , x, y) ;
r et ur n 0;
}
Swapping is used in sorting algorithms that is when we wish to arrange numbers in a
particular order either in ascending order or in descending.
15. 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. To reverse or invert
large numbers use long data type or long long data type if your compiler supports it, if you
still have large numbers then use strings or other data structure.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, r ever se = 0;
pr i nt f ( " Ent er a number t o r ever se\n" ) ;
scanf ( " %d" , &n) ;
whi l e ( n ! = 0)
{
r ever se = r ever se * 10;
r ever se = r ever se + n%10;
n = n/ 10;
}
pr i nt f ( " Rever se of ent er ed number i s = %d\n" , r ever se) ;
r et ur n 0;
}
Output of program:
16. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, r ever se = 0, t emp;
pr i nt f ( " Ent er a number t o check i f i t i s a pal i ndr ome or not \n" ) ;
scanf ( " %d" , &n) ;
t emp = n;
whi l e( t emp ! = 0 )
{
r ever se = r ever se * 10;
r ever se = r ever se + t emp%10;
t emp = t emp/ 10;
}
i f ( n == r ever se )
pr i nt f ( " %d i s a pal i ndr ome number . \n" , n) ;
el se
pr i nt f ( " %d i s not a pal i ndr ome number . \n" , n) ;
r et ur n 0;
}
Output of program:
17. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt r ow, c, n, t emp;
pr i nt f ( " Ent er t he number of r ows i n pyr ami d of st ar s you wi sh t o see " ) ;
scanf ( " %d" , &n) ;
t emp = n;
f or ( r ow = 1 ; r ow <= n ; r ow++ )
{
f or ( c = 1 ; c < t emp ; c++ )
pr i nt f ( " " ) ;
t emp- - ;
f or ( c = 1 ; c <= 2*r ow - 1 ; c++ )
pr i nt f ( " *" ) ;
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Output of program:
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, c, k;
pr i nt f ( " Ent er number of r ows\n" ) ;
scanf ( " %d" , &n) ;
f or ( c = 1 ; c <= n ; c++ )
{
f or ( k = 1 ; k <= c ; k++ )
pr i nt f ( " *" ) ;
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Using these examples you are in a better position to create your desired pattern for yourself.
Creating a pattern involves how to use nested loops properly, some pattern may involve
alphabets or other special characters. Key aspect is knowing how the characters in pattern
changes.
C pattern programs
Pattern:

*
*A*
*A*A*
*A*A*A*
C pattern program of stars and alphabets:
#i ncl ude<st di o. h>
mai n( )
{
i nt n, c, k, space, count = 1;
pr i nt f ( " Ent er number of r ows\n" ) ;
scanf ( " %d" , &n) ;
space = n;
f or ( c = 1 ; c <= n ; c++)
{
f or ( k = 1 ; k < space ; k++)
pr i nt f ( " " ) ;
f or ( k = 1 ; k <= c ; k++)
{
pr i nt f ( " *" ) ;
i f ( c > 1 && count < c)
{
pr i nt f ( " A" ) ;
count ++;
}
}
pr i nt f ( " \n" ) ;
space- - ;
count = 1;
}
r et ur n 0;
}
Pattern:
1
232
34543
4567654
567898765
C program:
#i ncl ude<st di o. h>
mai n( )
{
i nt n, c, d, num= 1, space;
scanf ( " %d" , &n) ;
space = n - 1;
f or ( d = 1 ; d <= n ; d++ )
{
num= d;
f or ( c = 1 ; c <= space ; c++ )
pr i nt f ( " " ) ;
space- - ;
f or ( c = 1 ; c <= d ; c++ )
{
pr i nt f ( " %d" , num) ;
num++;
}
num- - ;
num- - ;
f or ( c = 1 ; c < d ; c++)
{
pr i nt f ( " %d" , num) ;
num- - ;
}
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
18. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, c, k, space = 1;
pr i nt f ( " Ent er number of r ows\n" ) ;
scanf ( " %d" , &n) ;
space = n - 1;
f or ( k = 1; k <= n; k++)
{
f or ( c = 1; c <= space; c++)
pr i nt f ( " " ) ;
space- - ;
f or ( c = 1; c <= 2*k- 1; c++)
pr i nt f ( " *" ) ;
pr i nt f ( " \n" ) ;
}
space = 1;
f or ( k = 1; k <= n - 1; k++)
{
f or ( c = 1; c <= space; c++)
pr i nt f ( " " ) ;
space++;
f or ( c = 1 ; c <= 2*( n- k) - 1; c++)
pr i nt f ( " *" ) ;
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Output of program:
19. 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
*3
3
*5
1
Prime number program in c language
#i ncl ude<st di o. h>
i nt mai n( )
{
i nt n, i = 3, count , c;
pr i nt f ( " Ent er t he number of pr i me number s r equi r ed\n" ) ;
scanf ( " %d" , &n) ;
i f ( n >= 1 )
{
pr i nt f ( " Fi r st %d pr i me number s ar e : \n" , n) ;
pr i nt f ( " 2\n" ) ;
}
f or ( count = 2 ; count <= n ; )
{
f or ( c = 2 ; c <= i - 1 ; c++ )
{
i f ( i %c == 0 )
break;
}
i f ( c == i )
{
pr i nt f ( " %d\n" , i ) ;
count ++;
}
i ++;
}
r et ur n 0;
}
Output of program:
C program for prime number or not
#i ncl ude<st di o. h>
mai n( )
{
i nt n, c = 2;
pr i nt f ( " Ent er a number t o check i f i t i s pr i me\n" ) ;
scanf ( " %d" , &n) ;
f or ( c = 2 ; c <= n - 1 ; c++ )
{
i f ( n%c == 0 )
{
pr i nt f ( " %d i s not pr i me. \n" , n) ;
break;
}
}
i f ( c == n )
pr i nt f ( " %d i s pr i me. \n" , n) ;
r et ur n 0;
}
C program for prime number using function
#i ncl ude<st di o. h>
i nt check_pr i me( i nt ) ;
mai n( )
{
i nt n, r esul t ;
pr i nt f ( " Ent er an i nt eger t o check whet her i t i s pr i me or not . \n" ) ;
scanf ( " %d" , &n) ;
r esul t = check_pr i me( n) ;
i f ( r esul t == 1 )
pr i nt f ( " %d i s pr i me. \n" , n) ;
el se
pr i nt f ( " %d i s not pr i me. \n" , n) ;
r et ur n 0;
}
i nt check_pr i me( i nt a)
{
i nt c;
f or ( c = 2 ; c <= a - 1 ; c++ )
{
i f ( a%c == 0 )
r et ur n 0;
}
i f ( c == a )
r et ur n 1;
}
There are many logic to check prime numbers, one given below is more efficient then above
method.
f or ( c = 2 ; c <= ( i nt ) sqr t ( n) ; c++ )
Only checking from 2 to square root of number is sufficient.
There are many more efficient logic available.
20. 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
+ 7
3
+
1
3
= 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt number , sum= 0, t emp, r emai nder ;
pr i nt f ( " Ent er an i nt eger \n" ) ;
scanf ( " %d" , &number ) ;
t emp = number ;
whi l e( t emp ! = 0 )
{
r emai nder = t emp%10;
sum= sum+ r emai nder *r emai nder *r emai nder ;
t emp = t emp/ 10;
}
i f ( number == sum)
pr i nt f ( " Ent er ed number i s an ar mst r ong number . \n" ) ;
el se
pr i nt f ( " Ent er ed number i s not an ar mst r ong number . \n" ) ;
r et ur n 0;
}
Output of program:
21. 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
+ 7
3
+ 1
3
= 371. Some other
armstrong numbers are 0, 1, 153, 370, 407.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt r ;
l ong number = 0, c, sum= 0, t emp;
pr i nt f ( " Ent er an i nt eger upt o whi ch you want t o f i nd ar mst r ong
number s\n" ) ;
scanf ( " %l d" , &number ) ;
pr i nt f ( " Fol l owi ng ar mst r ong number s ar e f ound f r om1 t o %l d\n" , number ) ;
f or ( c = 1 ; c <= number ; c++ )
{
t emp = c;
whi l e( t emp ! = 0 )
{
r = t emp%10;
sum= sum+ r *r *r ;
t emp = t emp/ 10;
}
i f ( c == sum)
pr i nt f ( " %l d\n" , c) ;
sum= 0;
}
r et ur n 0;
}
Output of program:
22. 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 numbers 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 */
#i ncl ude<st di o. h>
i nt mai n( )
{
i nt n, f i r st = 0, second = 1, next , c;
pr i nt f ( " Ent er t he number of t er ms\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Fi r st %d t er ms of Fi bonacci ser i es ar e : - \n" , n) ;
f or ( c = 0 ; c < n ; c++ )
{
i f ( c <= 1 )
next = c;
el se
{
next = f i r st + second;
f i r st = second;
second = next ;
}
pr i nt f ( " %d\n" , next ) ;
}
r et ur n 0;
}
Output of program:
Fibonacci series program in c using recursion
#i ncl ude<st di o. h>
i nt Fi bonacci ( i nt ) ;
mai n( )
{
i nt n, i = 0, c;
scanf ( " %d" , &n) ;
pr i nt f ( " Fi bonacci ser i es\n" ) ;
f or ( c = 1 ; c <= n ; c++ )
{
pr i nt f ( " %d\n" , Fi bonacci ( i ) ) ;
i ++;
}
r et ur n 0;
}
i nt Fi bonacci ( i nt n)
{
i f ( n == 0 )
r et ur n 0;
el se i f ( n == 1 )
r et ur n 1;
el se
r et ur n ( Fi bonacci ( n- 1) + Fi bonacci ( n- 2) ) ;
}
Recursion method is less efficient as it involves function calls which uses stack, also there
are chances of stack overflow if function is called frequently for calculating larger Fibonacci
numbers.
23. 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
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, i , c, a = 1;
pr i nt f ( " Ent er t he number of r ows of Fl oyd' s t r i angl e t o pr i nt \n" ) ;
scanf ( " %d" , &n) ;
f or ( i = 1; i <= n; i ++)
{
f or ( c = 1; c <= i ; c++)
{
pr i nt f ( " %d " , a) ;
a++;
}
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Output of program:
24. 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
#i ncl ude <st di o. h>
l ong f act or i al ( i nt ) ;
i nt mai n( )
{
i nt i , n, c;
pr i nt f ( " Ent er t he number of r ows you wi sh t o see i n pascal t r i angl e\n" ) ;
scanf ( " %d" , &n) ;
f or ( i = 0 ; i < n ; i ++ )
{
f or ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
pr i nt f ( " " ) ;
f or ( c = 0 ; c <= i ; c++ )
pr i nt f ( " %l d " , f act or i al ( i ) / ( f act or i al ( c) *f act or i al ( i - c) ) ) ;
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
l ong f act or i al ( i nt n)
{
i nt c;
l ong r esul t = 1;
f or ( c = 1 ; c <= n ; c++ )
r esul t = r esul t *c;
r et ur n ( r esul t ) ;
}
Output of program:
25. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt f i r st , second, *p, *q, sum;
pr i nt f ( " Ent er t wo i nt eger s t o add\n" ) ;
scanf ( " %d%d" , &f i r st , &second) ;
p = &f i r st ;
q = &second;
sum= *p + *q;
pr i nt f ( " Sumof ent er ed number s = %d\n" , sum) ;
r et ur n 0;
}
Output of program:
26. 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 programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , maxi mum, si ze, c, l ocat i on = 1;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &si ze) ;
pr i nt f ( " Ent er %d i nt eger s\n" , si ze) ;
f or ( c = 0; c < si ze; c++)
scanf ( " %d" , &ar r ay[ c] ) ;
maxi mum= ar r ay[ 0] ;
f or ( c = 1; c < si ze; c++)
{
i f ( ar r ay[ c] > maxi mum)
{
maxi mum = ar r ay[ c] ;
l ocat i on = c+1;
}
}
pr i nt f ( " Maxi mumel ement i s pr esent at l ocat i on %d and i t ' s val ue i s %d. \n" ,
l ocat i on, maxi mum) ;
r et ur n 0;
}
Output of program:
C programming code using pointers
#i ncl ude <st di o. h>
i nt mai n( )
{
l ong ar r ay[ 100] , *maxi mum, si ze, c, l ocat i on = 1;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %l d" , &si ze) ;
pr i nt f ( " Ent er %l d i nt eger s\n" , si ze) ;
f or ( c = 0 ; c < si ze ; c++ )
scanf ( " %l d" , &ar r ay[ c] ) ;
maxi mum = ar r ay;
*maxi mum= *ar r ay;
f or ( c = 1; c < si ze; c++)
{
i f ( *( ar r ay+c) > *maxi mum)
{
*maxi mum= *( ar r ay+c) ;
l ocat i on = c+1;
}
}
pr i nt f ( " Maxi mumel ement f ound at l ocat i on %l d and i t ' s val ue i s %l d. \n" ,
l ocat i on, *maxi mum) ;
r et ur n 0;
}
The complexity of above code is O(n) as the time used depends on the size of input array or
in other words time to find maximum increases linearly as array size grows.
27. 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 programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , mi ni mum, si ze, c, l ocat i on = 1;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &si ze) ;
pr i nt f ( " Ent er %d i nt eger s\n" , si ze) ;
f or ( c = 0 ; c < si ze ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
mi ni mum= ar r ay[ 0] ;
f or ( c = 1 ; c < si ze ; c++ )
{
i f ( ar r ay[ c] < mi ni mum)
{
mi ni mum= ar r ay[ c] ;
l ocat i on = c+1;
}
}
pr i nt f ( " Mi ni mumel ement i s pr esent at l ocat i on %d and i t ' s val ue i s
%d. \n" , l ocat i on, mi ni mum) ;
r et ur n 0;
}
Output of program:
C programming code using pointers
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , *mi ni mum, si ze, c, l ocat i on = 1;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &si ze) ;
pr i nt f ( " Ent er %d i nt eger s\n" , si ze) ;
f or ( c = 0 ; c < si ze ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
mi ni mum= ar r ay;
*mi ni mum= *ar r ay;
f or ( c = 1 ; c < si ze ; c++ )
{
i f ( *( ar r ay+c) < *mi ni mum)
{
*mi ni mum= *( ar r ay+c) ;
l ocat i on = c+1;
}
}
pr i nt f ( " Mi ni mumel ement f ound at l ocat i on %d and i t ' s val ue i s %d. \n" ,
l ocat i on, *mi ni mum) ;
r et ur n 0;
}
Our algorithm first assumes first element as minimum and then compare it with other
elements if an element is smaller than it then it becomes the new minimum and this process
is repeated till complete array is scanned.
28. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , sear ch, c, n;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d i nt eger ( s) \n" , n) ;
f or ( c = 0; c < n; c++)
scanf ( " %d" , &ar r ay[ c] ) ;
pr i nt f ( " Ent er t he number t o sear ch\n" ) ;
scanf ( " %d" , &sear ch) ;
f or ( c = 0; c < n; c++)
{
i f ( ar r ay[ c] == sear ch) /* if required element found */
{
pr i nt f ( " %d i s pr esent at l ocat i on %d. \n" , sear ch, c+1) ;
break;
}
}
i f ( c == n)
pr i nt f ( " %d i s not pr esent i n ar r ay. \n" , sear ch) ;
r et ur n 0;
}
Output of program:
C program for binary search
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.
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , sear ch, c, n, count = 0;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d number s\n" , n) ;
f or ( c = 0 ; c < n ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
pr i nt f ( " Ent er t he number t o sear ch\n" ) ;
scanf ( " %d" , &sear ch) ;
f or ( c = 0 ; c < n ; c++ )
{
i f ( ar r ay[ c] == sear ch )
{
pr i nt f ( " %d i s pr esent at l ocat i on %d. \n" , sear ch, c+1) ;
count ++;
}
}
i f ( count == 0 )
pr i nt f ( " %d i s not pr esent i n ar r ay. \n" , sear ch) ;
el se
pr i nt f ( " %d i s pr esent %d t i mes i n ar r ay. \n" , sear ch, count ) ;
r et ur n 0;
}
Download Linear search multiple occurrence program.
Output of code:
C program for linear search using function
#i ncl ude <st di o. h>
i nt l i near _sear ch( i nt *, i nt , i nt ) ;
i nt mai n( )
{
i nt ar r ay[ 100] , sear ch, c, n, posi t i on;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d number s\n" , n) ;
f or ( c = 0 ; c < n ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
pr i nt f ( " Ent er t he number t o sear ch\n" ) ;
scanf ( " %d" , &sear ch) ;
posi t i on = l i near _sear ch( ar r ay, n, sear ch) ;
i f ( posi t i on == - 1 )
pr i nt f ( " %d i s not pr esent i n ar r ay. \n" , sear ch) ;
el se
pr i nt f ( " %d i s pr esent at l ocat i on %d. \n" , sear ch, posi t i on+1) ;
r et ur n 0;
}
i nt l i near _sear ch( i nt *poi nt er , i nt n, i nt f i nd)
{
i nt c;
f or ( c = 0 ; c < n ; c++ )
{
i f ( *( poi nt er +c) == f i nd )
r et ur n c;
}
r et ur n - 1;
}
Time required to search an element using linear search algorithm depends on size of list. In
best case element is present at beginning of list and in worst case element is present at the
end. Time complexity of linear search is O(n).
29. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt c, f i r st , l ast , mi ddl e, n, sear ch, ar r ay[ 100] ;
pr i nt f ( " Ent er number of el ement s\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d i nt eger s\n" , n) ;
f or ( c = 0 ; c < n ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
pr i nt f ( " Ent er val ue t o f i nd\n" ) ;
scanf ( " %d" , &sear ch) ;
f i r st = 0;
l ast = n - 1;
mi ddl e = ( f i r st +l ast ) / 2;
whi l e( f i r st <= l ast )
{
i f ( ar r ay[ mi ddl e] < sear ch )
f i r st = mi ddl e + 1;
el se i f ( ar r ay[ mi ddl e] == sear ch )
{
pr i nt f ( " %d f ound at l ocat i on %d. \n" , sear ch, mi ddl e+1) ;
break;
}
el se
l ast = mi ddl e - 1;
mi ddl e = ( f i r st + l ast ) / 2;
}
i f ( f i r st > l ast )
pr i nt f ( " Not f ound! %d i s not pr esent i n t he l i st . \n" , sear ch) ;
r et ur n 0;
}
C program for linear search
Output of program:
Binary search is faster than linear search but list should be sorted, hashing is faster than
binary search and perform searches in constant time.
30. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, c, d, a[ 100] , b[ 100] ;
pr i nt f ( " Ent er t he number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er t he ar r ay el ement s\n" ) ;
f or ( c = 0; c < n ; c++)
scanf ( " %d" , &a[ c] ) ;
/*
* Copying elements into array b starting from end of array a
*/
f or ( c = n - 1, d = 0; c >= 0; c- - , d++)
b[ d] = a[ c] ;
/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/
f or ( c = 0; c < n; c++)
a[ c] = b[ c] ;
pr i nt f ( " Rever se ar r ay i s\n" ) ;
f or ( c = 0; c < n; c++)
pr i nt f ( " %d\n" , a[ c] ) ;
r et ur n 0;
}
Output of program:
Reverse array by swapping (without using additional
memory)
#i ncl ude <st di o. h>
i nt mai n( ) {
i nt ar r ay[ 100] , n, c, t , end;
scanf ( " %d" , &n) ;
end = n - 1;
f or ( c = 0; c < n; c++) {
scanf ( " %d" , &ar r ay[ c] ) ;
}
f or ( c = 0; c < n/ 2; c++) {
t = ar r ay[ c] ;
ar r ay[ c] = ar r ay[ end] ;
ar r ay[ end] = t ;
end- - ;
}
pr i nt f ( " Rever sed ar r ay el ement s ar e: \n" ) ;
f or ( c = 0; c < n; c++) {
pr i nt f ( " %d\n" , ar r ay[ c] ) ;
}
r et ur n 0;
}
C program to reverse an array using pointers
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
voi d r ever se_ar r ay( i nt *, i nt ) ;
i nt mai n( )
{
i nt n, c, *poi nt er ;
scanf ( " %d" , &n) ;
poi nt er = ( i nt *) mal l oc( si zeof ( i nt ) *n) ;
i f ( poi nt er == NULL )
exi t ( EXI T_FAI LURE) ;
f or ( c = 0 ; c < n ; c++ )
scanf ( " %d" , ( poi nt er +c) ) ;
r ever se_ar r ay( poi nt er , n) ;
pr i nt f ( " Or i gi nal ar r ay on r ever sal i s\n" ) ;
f or ( c = 0 ; c < n ; c++ )
pr i nt f ( " %d\n" , *( poi nt er +c) ) ;
f r ee( poi nt er ) ;
r et ur n 0;
}
voi d r ever se_ar r ay( i nt *poi nt er , i nt n)
{
i nt *s, c, d;
s = ( i nt *) mal l oc( si zeof ( i nt ) *n) ;
i f ( s == NULL )
exi t ( EXI T_FAI LURE) ;
f or ( c = n - 1, d = 0 ; c >= 0 ; c- - , d++ )
*( s+d) = *( poi nt er +c) ;
f or ( c = 0 ; c < n ; c++ )
*( poi nt er +c) = *( s+c) ;
f r ee( s) ;
}
Array is passed to function and a new array is created and contents of passed array (in
reverse order) are copied into it and finally contents of new array are copied into array
passed to function.
31. 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 programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , posi t i on, c, n, val ue;
pr i nt f ( " Ent er number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d el ement s\n" , n) ;
f or ( c = 0; c < n; c++)
scanf ( " %d" , &ar r ay[ c] ) ;
pr i nt f ( " Ent er t he l ocat i on wher e you wi sh t o i nser t an el ement \n" ) ;
scanf ( " %d" , &posi t i on) ;
pr i nt f ( " Ent er t he val ue t o i nser t \n" ) ;
scanf ( " %d" , &val ue) ;
f or ( c = n - 1; c >= posi t i on - 1; c- - )
ar r ay[ c+1] = ar r ay[ c] ;
ar r ay[ posi t i on- 1] = val ue;
pr i nt f ( " Resul t ant ar r ay i s\n" ) ;
f or ( c = 0; c <= n; c++)
pr i nt f ( " %d\n" , ar r ay[ c] ) ;
r et ur n 0;
}
Output of program:
32. C program to delete an element from an array
This program delete an element from an array. Deleting an element does not affect the size
of array. It is also checked whether deletion is possible or not, For example if array is
containing five elements and you want to delete element at position six which is not
possible.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , posi t i on, c, n;
pr i nt f ( " Ent er number of el ement s i n ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d el ement s\n" , n) ;
f or ( c = 0 ; c < n ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
pr i nt f ( " Ent er t he l ocat i on wher e you wi sh t o del et e el ement \n" ) ;
scanf ( " %d" , &posi t i on) ;
i f ( posi t i on >= n+1 )
pr i nt f ( " Del et i on not possi bl e. \n" ) ;
el se
{
f or ( c = posi t i on - 1 ; c < n - 1 ; c++ )
ar r ay[ c] = ar r ay[ c+1] ;
pr i nt f ( " Resul t ant ar r ay i s\n" ) ;
f or ( c = 0 ; c < n - 1 ; c++ )
pr i nt f ( " %d\n" , ar r ay[ c] ) ;
}
r et ur n 0;
}
Output of program:
33. C program to merge two arrays
C program to merge two arrays into third array: Arrays are assumed to be sorted
in ascending order. You enter two short sorted arrays and combine them to get a large
array.
C programming code to merge two sorted arrays
#i ncl ude <st di o. h>
voi d mer ge( i nt [ ] , i nt , i nt [ ] , i nt , i nt [ ] ) ;
i nt mai n( ) {
i nt a[ 100] , b[ 100] , m, n, c, sor t ed[ 200] ;
pr i nt f ( " I nput number of el ement s i n f i r st ar r ay\n" ) ;
scanf ( " %d" , &m) ;
pr i nt f ( " I nput %d i nt eger s\n" , m) ;
f or ( c = 0; c < m; c++) {
scanf ( " %d" , &a[ c] ) ;
}
pr i nt f ( " I nput number of el ement s i n second ar r ay\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " I nput %d i nt eger s\n" , n) ;
f or ( c = 0; c < n; c++) {
scanf ( " %d" , &b[ c] ) ;
}
mer ge( a, m, b, n, sor t ed) ;
pr i nt f ( " Sor t ed ar r ay: \n" ) ;
f or ( c = 0; c < m+ n; c++) {
pr i nt f ( " %d\n" , sor t ed[ c] ) ;
}
r et ur n 0;
}
voi d mer ge( i nt a[ ] , i nt m, i nt b[ ] , i nt n, i nt sor t ed[ ] ) {
i nt i , j , k;
j = k = 0;
f or ( i = 0; i < m+ n; ) {
i f ( j < m&& k < n) {
i f ( a[ j ] < b[ k] ) {
sor t ed[ i ] = a[ j ] ;
j ++;
}
el se {
sor t ed[ i ] = b[ k] ;
k++;
}
i ++;
}
el se i f ( j == m) {
f or ( ; i < m+ n; ) {
sor t ed[ i ] = b[ k] ;
k++;
i ++;
}
}
el se {
f or ( ; i < m+ n; ) {
sor t ed[ i ] = a[ j ] ;
j ++;
i ++;
}
}
}
}
Output of program:
If the arrays are not sorted then you can sort them first and then use the above merge
function, another method is to merge them and then sort the array. Sorting two smaller
arrays will take less time as compared to sorting a big array. Merging two sorted array is
used in merge sort algorithm.
34. 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 */
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , n, c, d, swap;
pr i nt f ( " Ent er number of el ement s\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d i nt eger s\n" , n) ;
f or ( c = 0; c < n; c++)
scanf ( " %d" , &ar r ay[ c] ) ;
f or ( c = 0 ; c < ( n - 1 ) ; c++)
{
f or ( d = 0 ; d < n - c - 1; d++)
{
i f ( ar r ay[ d] > ar r ay[ d+1] ) /* For decreasing order use < */
{
swap = ar r ay[ d] ;
ar r ay[ d] = ar r ay[ d+1] ;
ar r ay[ d+1] = swap;
}
}
}
pr i nt f ( " Sor t ed l i st i n ascendi ng or der : \n" ) ;
f or ( c = 0 ; c < n ; c++ )
pr i nt f ( " %d\n" , ar r ay[ c] ) ;
r et ur n 0;
}
Output of program:
Bubble sort in c language using function
#i ncl ude <st di o. h>
voi d bubbl e_sor t ( l ong [ ] , l ong) ;
i nt mai n( )
{
l ong ar r ay[ 100] , n, c, d, swap;
pr i nt f ( " Ent er number of el ement s\n" ) ;
scanf ( " %l d" , &n) ;
pr i nt f ( " Ent er %l d i nt eger s\n" , n) ;
f or ( c = 0; c < n; c++)
scanf ( " %l d" , &ar r ay[ c] ) ;
bubbl e_sor t ( ar r ay, n) ;
pr i nt f ( " Sor t ed l i st i n ascendi ng or der : \n" ) ;
f or ( c = 0 ; c < n ; c++ )
pr i nt f ( " %l d\n" , ar r ay[ c] ) ;
r et ur n 0;
}
voi d bubbl e_sor t ( l ong l i st [ ] , l ong n)
{
l ong c, d, t ;
f or ( c = 0 ; c < ( n - 1 ) ; c++)
{
f or ( d = 0 ; d < n - c - 1; d++)
{
i f ( l i st [ d] > l i st [ d+1] )
{
/* Swapping */
t = l i st [ d] ;
l i st [ d] = l i st [ d+1] ;
l i st [ d+1] = t ;
}
}
}
}
You can also sort strings using Bubble sort, it is less efficient as its average and worst case
complexity is high, there are many other fast sorting algorithms.
35. 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 */
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt n, ar r ay[ 1000] , c, d, t ;
pr i nt f ( " Ent er number of el ement s\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d i nt eger s\n" , n) ;
f or ( c = 0; c < n; c++) {
scanf ( " %d" , &ar r ay[ c] ) ;
}
f or ( c = 1 ; c <= n - 1; c++) {
d = c;
whi l e ( d > 0 && ar r ay[ d] < ar r ay[ d- 1] ) {
t = ar r ay[ d] ;
ar r ay[ d] = ar r ay[ d- 1] ;
ar r ay[ d- 1] = t ;
d- - ;
}
}
pr i nt f ( " Sor t ed l i st i n ascendi ng or der : \n" ) ;
f or ( c = 0; c <= n - 1; c++) {
pr i nt f ( " %d\n" , ar r ay[ c] ) ;
}
r et ur n 0;
}
Output of program:
36. 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
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt ar r ay[ 100] , n, c, d, posi t i on, swap;
pr i nt f ( " Ent er number of el ement s\n" ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er %d i nt eger s\n" , n) ;
f or ( c = 0 ; c < n ; c++ )
scanf ( " %d" , &ar r ay[ c] ) ;
f or ( c = 0 ; c < ( n - 1 ) ; c++ )
{
posi t i on = c;
f or ( d = c + 1 ; d < n ; d++ )
{
i f ( ar r ay[ posi t i on] > ar r ay[ d] )
posi t i on = d;
}
i f ( posi t i on ! = c )
{
swap = ar r ay[ c] ;
ar r ay[ c] = ar r ay[ posi t i on] ;
ar r ay[ posi t i on] = swap;
}
}
pr i nt f ( " Sor t ed l i st i n ascendi ng or der : \n" ) ;
f or ( c = 0 ; c < n ; c++ )
pr i nt f ( " %d\n" , ar r ay[ c] ) ;
r et ur n 0;
}
Output of program:
37. 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 :-
1 2
3 4
Second matrix :-
4 5
-1 5
then output of the program ( sum of First and Second matrix ) will be
5 7
2 9
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt m, n, c, d, f i r st [ 10] [ 10] , second[ 10] [ 10] , sum[ 10] [ 10] ;
pr i nt f ( " Ent er t he number of r ows and col umns of mat r i x\n" ) ;
scanf ( " %d%d" , &m, &n) ;
pr i nt f ( " Ent er t he el ement s of f i r st mat r i x\n" ) ;
f or ( c = 0 ; c < m; c++ )
f or ( d = 0 ; d < n ; d++ )
scanf ( " %d" , &f i r st [ c] [ d] ) ;
pr i nt f ( " Ent er t he el ement s of second mat r i x\n" ) ;
f or ( c = 0 ; c < m; c++ )
f or ( d = 0 ; d < n ; d++ )
scanf ( " %d" , &second[ c] [ d] ) ;
f or ( c = 0 ; c < m; c++ )
f or ( d = 0 ; d < n ; d++ )
sum[ c] [ d] = f i r st [ c] [ d] + second[ c] [ d] ;
pr i nt f ( " Sumof ent er ed mat r i ces: - \n" ) ;
f or ( c = 0 ; c < m; c++ )
{
f or ( d = 0 ; d < n ; d++ )
pr i nt f ( " %d\t" , sum[ c] [ d] ) ;
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Output of program:
38. 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 programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt m, n, c, d, f i r st [ 10] [ 10] , second[ 10] [ 10] , di f f er ence[ 10] [ 10] ;
pr i nt f ( " Ent er t he number of r ows and col umns of mat r i x\n" ) ;
scanf ( " %d%d" , &m, &n) ;
pr i nt f ( " Ent er t he el ement s of f i r st mat r i x\n" ) ;
f or ( c = 0; c < m; c++)
f or ( d = 0 ; d < n; d++)
scanf ( " %d" , &f i r st [ c] [ d] ) ;
pr i nt f ( " Ent er t he el ement s of second mat r i x\n" ) ;
f or ( c = 0; c < m; c++)
f or ( d = 0; d < n; d++)
scanf ( " %d" , &second[ c] [ d] ) ;
f or ( c = 0; c < m; c++)
f or ( d = 0; d < n; d++)
di f f er ence[ c] [ d] = f i r st [ c] [ d] - second[ c] [ d] ;
pr i nt f ( " di f f er ence of ent er ed mat r i ces: - \n" ) ;
f or ( c = 0; c < m; c++)
{
f or ( d = 0; d < n; d++)
pr i nt f ( " %d\t" , di f f er ence[ c] [ d] ) ;
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Output of program:
39. 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
1 2
3 4
5 6
then transpose of above matrix will be
1 3 5
2 4 6
When we transpose a matrix then the order of matrix changes, but for a square matrix order
remains same.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt m, n, c, d, mat r i x[ 10] [ 10] , t r anspose[ 10] [ 10] ;
pr i nt f ( " Ent er t he number of r ows and col umns of mat r i x " ) ;
scanf ( " %d%d" , &m, &n) ;
pr i nt f ( " Ent er t he el ement s of mat r i x \n" ) ;
f or ( c = 0 ; c < m; c++ )
{
f or ( d = 0 ; d < n ; d++ )
{
scanf ( " %d" , &mat r i x[ c] [ d] ) ;
}
}
f or ( c = 0 ; c < m; c++ )
{
f or ( d = 0 ; d < n ; d++ )
{
t r anspose[ d] [ c] = mat r i x[ c] [ d] ;
}
}
pr i nt f ( " Tr anspose of ent er ed mat r i x : - \n" ) ;
f or ( c = 0 ; c < n ; c++ )
{
f or ( d = 0 ; d < m; d++ )
{
pr i nt f ( " %d\t" , t r anspose[ c] [ d] ) ;
}
pr i nt f ( " \n" ) ;
}
r et ur n 0;
}
Output of program:
40. 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.
Matrix multiplication in c language
#i ncl ude <st di o. h>
i nt mai n( )
{
i nt m, n, p, q, c, d, k, sum= 0;
i nt f i r st [ 10] [ 10] , second[ 10] [ 10] , mul t i pl y[ 10] [ 10] ;
pr i nt f ( " Ent er t he number of r ows and col umns of f i r st mat r i x\n" ) ;
scanf ( " %d%d" , &m, &n) ;
pr i nt f ( " Ent er t he el ement s of f i r st mat r i x\n" ) ;
f or ( c = 0 ; c < m; c++ )
f or ( d = 0 ; d < n ; d++ )
scanf ( " %d" , &f i r st [ c] [ d] ) ;
pr i nt f ( " Ent er t he number of r ows and col umns of second mat r i x\n" ) ;
scanf ( " %d%d" , &p, &q) ;
i f ( n ! = p )
pr i nt f ( " Mat r i ces wi t h ent er ed or der s can' t be mul t i pl i ed wi t h each
ot her . \n" ) ;
el se
{
pr i nt f ( " Ent er t he el ement s of second mat r i x\n" ) ;
f or ( c = 0 ; c < p ; c++ )
f or ( d = 0 ; d < q ; d++ )
scanf ( " %d" , &second[ c] [ d] ) ;
f or ( c = 0 ; c < m; c++ )
{
f or ( d = 0 ; d < q ; d++ )
{
f or ( k = 0 ; k < p ; k++ )
{
sum= sum+ f i r st [ c] [ k] *second[ k] [ d] ;
}
mul t i pl y[ c] [ d] = sum;
sum= 0;
}
}
pr i nt f ( " Pr oduct of ent er ed mat r i ces: - \n" ) ;
f or ( c = 0 ; c < m; c++ )
{
f or ( d = 0 ; d < q ; d++ )
pr i nt f ( " %d\t" , mul t i pl y[ c] [ d] ) ;
pr i nt f ( " \n" ) ;
}
}
r et ur n 0;
}
A 3 X 3 matrix multiply in c is shown as example below.
Matrices are frequently used while doing programming and are used to represent graph
data structure, in solving system of linear equations and many more. Lot of research is being
done on how to multiply matrices using minimum number of operations. You can also
implement it using pointers.
41. C program print string
This program print a string. String can be printed by using various functions such as printf,
puts.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
char ar r ay[ 20] = " Hel l o Wor l d" ;
pr i nt f ( " %s\n" , ar r ay) ;
r et ur n 0;
}
To input a string we use scanf function.
C programming code
#i ncl ude <st di o. h>
i nt mai n( )
{
char ar r ay[ 100] ;
pr i nt f ( " Ent er a st r i ng\n" ) ;
scanf ( " %s" , ar r ay) ;
pr i nt f ( " You ent er ed t he st r i ng %s\n" , ar r ay) ;
r et ur n 0;
}
Input string containing spaces
#i ncl ude <st di o. h>
i nt mai n( )
{
char a[ 80] ;
get s( a) ;
pr i nt f ( " %s\n" , a) ;
r et ur n 0;
}
Note that scanf can only input single word strings, to receive strings containing spaces use
gets function.
42. 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
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char a[ 100] ;
i nt l engt h;
pr i nt f ( " Ent er a st r i ng t o cal cul at e i t ' s l engt h\n" ) ;
get s( a) ;
l engt h = st r l en( a) ;
pr i nt f ( " Lengt h of ent er ed st r i ng i s = %d\n" , l engt h) ;
r et ur n 0;
}
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.
#i ncl ude <st di o. h>
i nt mai n( )
{
char ar r ay[ 100] , *poi nt er ;
i nt l engt h = 0;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( ar r ay) ;
poi nt er = ar r ay;
whi l e( *( poi nt er +l engt h) )
l engt h++;
pr i nt f ( " Lengt h of ent er ed st r i ng = %d\n" , l engt h) ;
r et ur n 0;
}
Function to find string length
i nt st r i ng_l engt h( char *s)
{
i nt c = 0;
whi l e( *( s+c) )
c++;
r et ur n c;
}
43. 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
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char a[ 100] , b[ 100] ;
pr i nt f ( " Ent er t he f i r st st r i ng\n" ) ;
get s( a) ;
pr i nt f ( " Ent er t he second st r i ng\n" ) ;
get s( b) ;
i f ( st r cmp( a, b) == 0 )
pr i nt f ( " Ent er ed st r i ngs ar e equal . \n" ) ;
el se
pr i nt f ( " Ent er ed st r i ngs ar e not equal . \n" ) ;
r et ur n 0;
}
Output of program:
C program to compare two strings without using strcmp
Here we create our own function to compare strings.
i nt compar e( char a[ ] , char b[ ] )
{
i nt c = 0;
whi l e( a[ c] == b[ c] )
{
i f ( a[ c] == ' \0' | | b[ c] == ' \0' )
break;
c++;
}
i f ( a[ c] == ' \0' && b[ c] == ' \0' )
r et ur n 0;
el se
r et ur n - 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.
#i ncl ude<st di o. h>
i nt compar e_st r i ng( char *, char *) ;
i nt mai n( )
{
char f i r st [ 100] , second[ 100] , r esul t ;
pr i nt f ( " Ent er f i r st st r i ng\n" ) ;
get s( f i r st ) ;
pr i nt f ( " Ent er second st r i ng\n" ) ;
get s( second) ;
r esul t = compar e_st r i ng( f i r st , second) ;
i f ( r esul t == 0 )
pr i nt f ( " Bot h st r i ngs ar e same. \n" ) ;
el se
pr i nt f ( " Ent er ed st r i ngs ar e not equal . \n" ) ;
r et ur n 0;
}
i nt compar e_st r i ng( char *f i r st , char *second)
{
whi l e( *f i r st ==*second)
{
i f ( *f i r st == ' \0' | | *second == ' \0' )
break;
f i r st ++;
second++;
}
i f ( *f i r st == ' \0' && *second == ' \0' )
r et ur n 0;
el se
r et ur n - 1;
}
44. 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
#i ncl ude<st di o. h>
#i ncl ude<st r i ng. h>
mai n( )
{
char sour ce[ ] = " C pr ogr am" ;
char dest i nat i on[ 50] ;
st r cpy( dest i nat i on, sour ce) ;
pr i nt f ( " Sour ce st r i ng: %s\n" , sour ce) ;
pr i nt f ( " Dest i nat i on st r i ng: %s\n" , dest i nat i on) ;
r et ur n 0;
}
C program to copy a string using pointers
: here we copy string without using strcmp by creating our own function which uses
pointers.
#i ncl ude<st di o. h>
voi d copy_st r i ng( char *, char *) ;
mai n( )
{
char sour ce[ 100] , t ar get [ 100] ;
pr i nt f ( " Ent er sour ce st r i ng\n" ) ;
get s( sour ce) ;
copy_st r i ng( t ar get , sour ce) ;
pr i nt f ( " Tar get st r i ng i s \"%s\"\n" , t ar get ) ;
r et ur n 0;
}
voi d copy_st r i ng( char *t ar get , char *sour ce)
{
whi l e( *sour ce)
{
*t ar get = *sour ce;
sour ce++;
t ar get ++;
}
*t ar get = ' \0' ;
}
45. 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 programming code
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char a[ 100] , b[ 100] ;
pr i nt f ( " Ent er t he f i r st st r i ng\n" ) ;
get s( a) ;
pr i nt f ( " Ent er t he second st r i ng\n" ) ;
get s( b) ;
st r cat ( a, b) ;
pr i nt f ( " St r i ng obt ai ned on concat enat i on i s %s\n" , a) ;
r et ur n 0;
}
Output of program:
String concatenation without strcat
#i ncl ude <st di o. h>
voi d concat enat e_st r i ng( char *, char *) ;
i nt mai n( )
{
char or i gi nal [ 100] , add[ 100] ;
pr i nt f ( " Ent er sour ce st r i ng\n" ) ;
get s( or i gi nal ) ;
pr i nt f ( " Ent er st r i ng t o concat enat e\n" ) ;
get s( add) ;
concat enat e_st r i ng( or i gi nal , add) ;
pr i nt f ( " St r i ng af t er concat enat i on i s \"%s\"\n" , or i gi nal ) ;
r et ur n 0;
}
voi d concat enat e_st r i ng( char *or i gi nal , char *add)
{
whi l e( *or i gi nal )
or i gi nal ++;
whi l e( *add)
{
*or i gi nal = *add;
add++;
or i gi nal ++;
}
*or i gi nal = ' \0' ;
}
46. 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, reverse string using
recursion and Reverse words in string. If you are using first method then you must include
string.h in your program.
C programming code
/* String reverse in c*/
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char ar r [ 100] ;
pr i nt f ( " Ent er a st r i ng t o r ever se\n" ) ;
get s( ar r ) ;
st r r ev( ar r ) ;
pr i nt f ( " Rever se of ent er ed st r i ng i s \n%s\n" , ar r ) ;
r et ur n 0;
}
Output of program:
/* Second method */
C program to reverse a string using pointers
: Now we will invert string using pointers or without using library function strrev.
#i ncl ude<st di o. h>
i nt st r i ng_l engt h( char *) ;
voi d r ever se( char *) ;
mai n( )
{
char st r i ng[ 100] ;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
r ever se( st r i ng) ;
pr i nt f ( " Rever se of ent er ed st r i ng i s \"%s\". \n" , st r i ng) ;
r et ur n 0;
}
voi d r ever se( char *st r i ng)
{
i nt l engt h, c;
char *begi n, *end, t emp;
l engt h = st r i ng_l engt h( st r i ng) ;
begi n = st r i ng;
end = st r i ng;
f or ( c = 0 ; c < ( l engt h - 1 ) ; c++ )
end++;
f or ( c = 0 ; c < l engt h/ 2 ; c++ )
{
t emp = *end;
*end = *begi n;
*begi n = t emp;
begi n++;
end- - ;
}
}
i nt st r i ng_l engt h( char *poi nt er )
{
i nt c = 0;
whi l e( *( poi nt er +c) ! = ' \0' )
c++;
r et ur n c;
}
C program to reverse a string using recursion
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
voi d r ever se( char *, i nt , i nt ) ;
i nt mai n( )
{
char a[ 100] ;
get s( a) ;
r ever se( a, 0, st r l en( a) - 1) ;
pr i nt f ( " %s\n" , a) ;
r et ur n 0;
}
voi d r ever se( char *x, i nt begi n, i nt end)
{
char c;
i f ( begi n >= end)
r et ur n;
c = *( x+begi n) ;
*( x+begi n) = *( x+end) ;
*( x+end) = c;
r ever se( x, ++begi n, - - end) ;
}
In recursion method we swap characters at the begin and at the end and then move towards
the middle of the string. This method is inefficient due to repeated function calls but useful
in practicing recursion.
47. 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
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char a[ 100] , b[ 100] ;
pr i nt f ( " Ent er t he st r i ng t o check i f i t i s a pal i ndr ome\n" ) ;
get s( a) ;
st r cpy( b, a) ;
st r r ev( b) ;
i f ( st r cmp( a, b) == 0 )
pr i nt f ( " Ent er ed st r i ng i s a pal i ndr ome. \n" ) ;
el se
pr i nt f ( " Ent er ed st r i ng i s not a pal i ndr ome. \n" ) ;
r et ur n 0;
}
Output of program:
Palindrome number in c
#i ncl ude <st di o. h>
mai n( )
{
i nt n, r ever se = 0, t emp;
pr i nt f ( " Ent er a number t o check i f i t i s a pal i ndr ome or not \n" ) ;
scanf ( " %d" , &n) ;
t emp = n;
whi l e( t emp ! = 0 )
{
r ever se = r ever se * 10;
r ever se = r ever se + t emp%10;
t emp = t emp/ 10;
}
i f ( n == r ever se )
pr i nt f ( " %d i s a pal i ndr ome number . \n" , n) ;
el se
pr i nt f ( " %d i s not a pal i ndr ome number . \n" , n) ;
r et ur n 0;
}
C program for palindrome without using string
functions
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char t ext [ 100] ;
i nt begi n, mi ddl e, end, l engt h = 0;
get s( t ext ) ;
whi l e ( t ext [ l engt h] ! = ' \0' )
l engt h++;
end = l engt h - 1;
mi ddl e = l engt h/ 2;
f or ( begi n = 0 ; begi n < mi ddl e ; begi n++ )
{
i f ( t ext [ begi n] ! = t ext [ end] )
{
pr i nt f ( " Not a pal i ndr ome. \n" ) ;
break;
}
end- - ;
}
i f ( begi n == mi ddl e )
pr i nt f ( " Pal i ndr ome. \n" ) ;
r et ur n 0;
}
C program check palindrome
#i ncl ude <st di o. h>
i nt i s_pal i ndr ome( char *) ;
voi d copy_st r i ng( char *, char *) ;
voi d r ever se_st r i ng( char *) ;
i nt st r i ng_l engt h( char *) ;
i nt compar e_st r i ng( char *, char *) ;
i nt mai n( )
{
char st r i ng[ 100] ;
i nt r esul t ;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
r esul t = i s_pal i ndr ome( st r i ng) ;
i f ( r esul t == 1 )
pr i nt f ( " \"%s\" i s a pal i ndr ome st r i ng. \n" , st r i ng) ;
el se
pr i nt f ( " \"%s\" i s not a pal i ndr ome st r i ng. \n" , st r i ng) ;
r et ur n 0;
}
i nt i s_pal i ndr ome( char *st r i ng)
{
i nt check, l engt h;
char *r ever se;
l engt h = st r i ng_l engt h( st r i ng) ;
r ever se = ( char *) mal l oc( l engt h+1) ;
copy_st r i ng( r ever se, st r i ng) ;
r ever se_st r i ng( r ever se) ;
check = compar e_st r i ng( st r i ng, r ever se) ;
f r ee( r ever se) ;
i f ( check == 0 )
r et ur n 1;
el se
r et ur n 0;
}
i nt st r i ng_l engt h( char *st r i ng)
{
i nt l engt h = 0;
whi l e( *st r i ng)
{
l engt h++;
st r i ng++;
}
r et ur n l engt h;
}
voi d copy_st r i ng( char *t ar get , char *sour ce)
{
whi l e( *sour ce)
{
*t ar get = *sour ce;
sour ce++;
t ar get ++;
}
*t ar get = ' \0' ;
}
voi d r ever se_st r i ng( char *st r i ng)
{
i nt l engt h, c;
char *begi n, *end, t emp;
l engt h = st r i ng_l engt h( st r i ng) ;
begi n = st r i ng;
end = st r i ng;
f or ( c = 0 ; c < ( l engt h - 1 ) ; c++ )
end++;
f or ( c = 0 ; c < l engt h/ 2 ; c++ )
{
t emp = *end;
*end = *begi n;
*begi n = t emp;
begi n++;
end- - ;
}
}
i nt compar e_st r i ng( char *f i r st , char *second)
{
whi l e( *f i r st ==*second)
{
i f ( *f i r st == ' \0' | | *second == ' \0' )
break;
f i r st ++;
second++;
}
i f ( *f i r st == ' \0' && *second == ' \0' )
r et ur n 0;
el se
r et ur n - 1;
}
Pointers are used in functions, you can develop your code without using pointers.
48. 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
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt check_vowel ( char ) ;
i nt mai n( )
{
char s[ 100] , t [ 100] ;
i nt i , j = 0;
pr i nt f ( " Ent er a st r i ng t o del et e vowel s\n" ) ;
get s( s) ;
f or ( i = 0; s[ i ] ! = ' \0' ; i ++) {
i f ( check_vowel ( s[ i ] ) == 0) { //not a vowel
t [ j ] = s[ i ] ;
j ++;
}
}
t [ j ] = ' \0' ;
st r cpy( s, t ) ; //We are changing initial string
pr i nt f ( " St r i ng af t er del et i ng vowel s: %s\n" , s) ;
r et ur n 0;
}
i nt check_vowel ( char c)
{
swi t ch( c) {
case ' a' :
case ' A' :
case ' e' :
case ' E' :
case ' i ' :
case ' I ' :
case ' o' :
case ' O' :
case ' u' :
case ' U' :
r et ur n 1;
def aul t :
r et ur n 0;
}
}
C programming code using pointers
#i ncl ude<st di o. h>
#i ncl ude<st dl i b. h>
#i ncl ude<st r i ng. h>
#def i ne TRUE 1
#def i ne FALSE 0
i nt check_vowel ( char ) ;
mai n( )
{
char st r i ng[ 100] , *t emp, *poi nt er , ch, *st ar t ;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
t emp = st r i ng;
poi nt er = ( char *) mal l oc( 100) ;
i f ( poi nt er == NULL )
{
pr i nt f ( " Unabl e t o al l ocat e memor y. \n" ) ;
exi t ( EXI T_FAI LURE) ;
}
st ar t = poi nt er ;
whi l e( *t emp)
{
ch = *t emp;
i f ( ! check_vowel ( ch) )
{
*poi nt er = ch;
poi nt er ++;
}
t emp++;
}
*poi nt er = ' \0' ;
poi nt er = st ar t ;
st r cpy( st r i ng, poi nt er ) ; /* If you wish to convert original string */
f r ee( poi nt er ) ;
pr i nt f ( " St r i ng af t er r emovi ng vowel i s \"%s\"\n" , st r i ng) ;
r et ur n 0;
}
i nt check_vowel ( char a)
{
i f ( a >= ' A' && a <= ' Z' )
a = a + ' a' - ' A' ;
i f ( a == ' a' | | a == ' e' | | a == ' i ' | | a == ' o' | | a == ' u' )
r et ur n TRUE;
r et ur n FALSE;
}
49. 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
#i ncl ude <st di o. h>
#i ncl ude <mal l oc. h>
char * subst r i ng( char *, i nt , i nt ) ;
i nt mai n( )
{
char st r i ng[ 100] , *poi nt er ;
i nt posi t i on, l engt h;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
pr i nt f ( " Ent er t he posi t i on and l engt h of subst r i ng\n" ) ;
scanf ( " %d%d" , &posi t i on, &l engt h) ;
poi nt er = subst r i ng( st r i ng, posi t i on, l engt h) ;
pr i nt f ( " Requi r ed subst r i ng i s \"%s\"\n" , poi nt er ) ;
f r ee( poi nt er ) ;
r et ur n 0;
}
/*C substring function: It returns a pointer to the substring */
char *subst r i ng( char *st r i ng, i nt posi t i on, i nt l engt h)
{
char *poi nt er ;
i nt c;
poi nt er = mal l oc( l engt h+1) ;
i f ( poi nt er == NULL)
{
pr i nt f ( " Unabl e t o al l ocat e memor y. \n" ) ;
exi t ( EXI T_FAI LURE) ;
}
f or ( c = 0 ; c < posi t i on - 1 ; c++)
st r i ng++;
f or ( c = 0 ; c < l engt h ; c++)
{
*( poi nt er +c) = *st r i ng;
st r i ng++;
}
*( poi nt er +c) = ' \0' ;
r et ur n poi nt er ;
}
C substring program output:
C code for all substrings of a string
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
#i ncl ude <mal l oc. h>
char * subst r i ng( char *, i nt , i nt ) ;
i nt mai n( )
{
char st r i ng[ 100] , *poi nt er ;
i nt posi t i on = 1, l engt h = 1, t emp, st r i ng_l engt h;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
t emp = st r i ng_l engt h = st r l en( st r i ng) ;
pr i nt f ( " Subst r i ng of \"%s\" ar e\n" , st r i ng) ;
whi l e ( posi t i on <= st r i ng_l engt h)
{
whi l e ( l engt h <= t emp)
{
poi nt er = subst r i ng( st r i ng, posi t i on, l engt h) ;
pr i nt f ( " %s\n" , poi nt er ) ;
f r ee( poi nt er ) ;
l engt h++;
}
t emp- - ;
posi t i on++;
l engt h = 1;
}
r et ur n 0;
}
/* Use substring function given in above c program*/
Substring code output:
50. 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
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
#i ncl ude <st r i ng. h>
voi d sor t _st r i ng( char *) ;
i nt mai n( )
{
char st r i ng[ 100] ;
pr i nt f ( " Ent er some t ext \n" ) ;
get s( st r i ng) ;
sor t _st r i ng( st r i ng) ;
pr i nt f ( " %s\n" , st r i ng) ;
r et ur n 0;
}
voi d sor t _st r i ng( char *s)
{
i nt c, d = 0, l engt h;
char *poi nt er , *r esul t , ch;
l engt h = st r l en( s) ;
r esul t = ( char *) mal l oc( l engt h+1) ;
poi nt er = s;
f or ( ch = ' a' ; ch <= ' z' ; ch++ )
{
f or ( c = 0 ; c < l engt h ; c++ )
{
i f ( *poi nt er == ch )
{
*( r esul t +d) = *poi nt er ;
d++;
}
poi nt er ++;
}
poi nt er = s;
}
*( r esul t +d) = ' \0' ;
st r cpy( s, r esul t ) ;
f r ee( r esul t ) ;
}
Output of program:
51. 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 pr ogr ammi ng"
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
#i ncl ude <st di o. h>
i nt mai n( )
{
char t ext [ 100] , bl ank[ 100] ;
i nt c = 0, d = 0;
pr i nt f ( " Ent er some t ext \n" ) ;
get s( t ext ) ;
whi l e ( t ext [ c] ! = ' \0' )
{
i f ( ! ( t ext [ c] == ' ' && t ext [ c+1] == ' ' ) ) {
bl ank[ d] = t ext [ c] ;
d++;
}
c++;
}
bl ank[ d] = ' \0' ;
pr i nt f ( " Text af t er r emovi ng bl anks\n%s\n" , bl ank) ;
r et ur n 0;
}
If you want you can copy blank into text string so that original string is modified.
Output of program:
C programming code using pointers
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
#i ncl ude <st dl i b. h>
#def i ne SPACE ' '
i nt mai n( )
{
char st r i ng[ 100] , *bl ank, *st ar t ;
i nt l engt h, c = 0, d = 0;
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
l engt h = st r l en( st r i ng) ;
bl ank = st r i ng;
st ar t = ( char *) mal l oc( l engt h+1) ;
i f ( st ar t == NULL )
exi t ( EXI T_FAI LURE) ;
whi l e( *( bl ank+c) )
{
i f ( *( bl ank+c) == SPACE && *( bl ank+c+1) == SPACE )
{}
el se
{
*( st ar t +d) = *( bl ank+c) ;
d++;
}
c++;
}
*( st ar t +d) = ' \0' ;
pr i nt f ( " %s\n" , st ar t ) ;
f r ee( st ar t ) ;
r et ur n 0;
}
52. strlwr, strupr in c
Here we will change string case with and without strlwr, strupr functions.
strlwr in c
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char st r i ng[ ] = " St r l wr i n C" ;
pr i nt f ( " %s\n" , st r l wr ( st r i ng) ) ;
r et ur n 0;
}
strupr in c
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char st r i ng[ ] = " st r upr i n c" ;
pr i nt f ( " %s\n" , st r upr ( st r i ng) ) ;
r et ur n 0;
}
Change string to upper case without strupr
#i ncl ude <st di o. h>
voi d upper _st r i ng( char *) ;
i nt mai n( )
{
char st r i ng[ 100] ;
pr i nt f ( " Ent er a st r i ng t o conver t i t i nt o upper case\n" ) ;
get s( st r i ng) ;
upper _st r i ng( st r i ng) ;
pr i nt f ( " Ent er ed st r i ng i n upper case i s \"%s\"\n" , st r i ng) ;
r et ur n 0;
}
voi d upper _st r i ng( char *st r i ng)
{
whi l e( *st r i ng)
{
i f ( *st r i ng >= ' a' && *st r i ng <= ' z' )
{
*st r i ng = *st r i ng - 32;
}
st r i ng++;
}
}
Change string to lower case without strlwr
#i ncl ude <st di o. h>
voi d l ower _st r i ng( char *) ;
i nt mai n( )
{
char st r i ng[ 100] ;
pr i nt f ( " Ent er a st r i ng t o conver t i t i nt o l ower case\n" ) ;
get s( st r i ng) ;
l ower _st r i ng( st r i ng) ;
pr i nt f ( " Ent er ed st r i ng i n l ower case i s \"%s\"\n" , st r i ng) ;
r et ur n 0;
}
voi d l ower _st r i ng( char *st r i ng)
{
whi l e( *st r i ng)
{
i f ( *st r i ng >= ' A' && *st r i ng <= ' Z' )
{
*st r i ng = *st r i ng + 32;
}
st r i ng++;
}
}
53. C program to swap two strings
C program to swap strings i.e. contents of two strings are interchanged.
C programming code
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
#i ncl ude <mal l oc. h>
i nt mai n( )
{
char f i r st [ 100] , second[ 100] , *t emp;
pr i nt f ( " Ent er t he f i r st st r i ng\n" ) ;
get s( f i r st ) ;
pr i nt f ( " Ent er t he second st r i ng\n" ) ;
get s( second) ;
pr i nt f ( " \nBef or e Swappi ng\n" ) ;
pr i nt f ( " Fi r st st r i ng: %s\n" , f i r st ) ;
pr i nt f ( " Second st r i ng: %s\n\n" , second) ;
t emp = ( char *) mal l oc( 100) ;
st r cpy( t emp, f i r st ) ;
st r cpy( f i r st , second) ;
st r cpy( second, t emp) ;
pr i nt f ( " Af t er Swappi ng\n" ) ;
pr i nt f ( " Fi r st st r i ng: %s\n" , f i r st ) ;
pr i nt f ( " Second st r i ng: %s\n" , second) ;
r et ur n 0;
}
Output of program:
54. 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
#i ncl ude <st di o. h>
#i ncl ude <st r i ng. h>
i nt mai n( )
{
char st r i ng[ 100] ;
i nt c = 0, count [ 26] = {0};
pr i nt f ( " Ent er a st r i ng\n" ) ;
get s( st r i ng) ;
whi l e ( st r i ng[ c] ! = ' \0' )
{
/* Considering characters from 'a' to 'z' only */
i f ( st r i ng[ c] >= ' a' && st r i ng[ c] <= ' z' )
count [ st r i ng[ c] - ' a' ] ++;
c++;
}
f or ( c = 0 ; c < 26 ; c++ )
{
i f ( count [ c] ! = 0 )
pr i nt f ( " %c occur s %d t i mes i n t he ent er ed
st r i ng. \n" , c+' a' , count [ c] ) ;
}
r et ur n 0;
}
Explanation of "count[string[c]-'a']++", suppose input string begins with 'a' so c is 0 initially
and string[0] = 'a' and string[0]-'a' = 0 and we increment count[0] i.e. a has occurred one
time and repeat this till complete string is scanned.
Output of program:
Did you notice that string in the output of program contains every alphabet at least once.
55. 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 appear in the strings and then comparing their corresponding counts.
C anagram programming code
#i ncl ude <st di o. h>
i nt check_anagr am( char [ ] , char [ ] ) ;
i nt mai n( )
{
char a[ 100] , b[ 100] ;
i nt f l ag;
pr i nt f ( " Ent er f i r st st r i ng\n" ) ;
get s( a) ;
pr i nt f ( " Ent er second st r i ng\n" ) ;
get s( b) ;
f l ag = check_anagr am( a, b) ;
i f ( f l ag == 1)
pr i nt f ( " \"%s\" and \"%s\" ar e anagr ams. \n" , a, b) ;
el se
pr i nt f ( " \"%s\" and \"%s\" ar e not anagr ams. \n" , a, b) ;
r et ur n 0;
}
i nt check_anagr am( char a[ ] , char b[ ] )
{
i nt f i r st [ 26] = {0}, second[ 26] = {0}, c = 0;
whi l e ( a[ c] ! = ' \0' )
{
f i r st [ a[ c] - ' a' ] ++;
c++;
}
c = 0;
whi l e ( b[ c] ! = ' \0' )
{
second[ b[ c] - ' a' ] ++;
c++;
}
f or ( c = 0; c < 26; c++)
{
i f ( f i r st [ c] ! = second[ c] )
r et ur n 0;
}
r et ur n 1;
}
Output of program:
56. C program to read a file
C program to read a file: This program reads a file entered by the user and displays its
contents on the screen, fopen function is used to open a file it returns a pointer to structure
FILE. FILE is a predefined 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. The file must be
present in the directory in which the executable file of this code sis present.
C program to open a file
C programming code to open a file and to print it contents on screen.
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
i nt mai n( )
{
char ch, f i l e_name[ 25] ;
FI LE *f p;
pr i nt f ( " Ent er t he name of f i l e you wi sh t o see\n" ) ;
get s( f i l e_name) ;
f p = f open( f i l e_name, " r " ) ; // read mode
i f ( f p == NULL )
{
per r or ( " Er r or whi l e openi ng t he f i l e. \n" ) ;
exi t ( EXI T_FAI LURE) ;
}
pr i nt f ( " The cont ent s of %s f i l e ar e : \n" , f i l e_name) ;
whi l e( ( ch = f get c( f p) ) ! = EOF )
pr i nt f ( " %c" , ch) ;
f cl ose( f p) ;
r et ur n 0;
}
Output of program:
There are blank lines present at end of file. In our program we have opened only one file but
you can open multiple files in a single program and in different modes as desired. File
handling is very important when we wish to store data permanently on a storage device. All
variables and data of program is lost when program exits so if that data is required later we
need to use files.
57. C program to copy files
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.
C programming code
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
i nt mai n( )
{
char ch, sour ce_f i l e[ 20] , t ar get _f i l e[ 20] ;
FI LE *sour ce, *t ar get ;
pr i nt f ( " Ent er name of f i l e t o copy\n" ) ;
get s( sour ce_f i l e) ;
sour ce = f open( sour ce_f i l e, " r " ) ;
i f ( sour ce == NULL )
{
pr i nt f ( " Pr ess any key t o exi t . . . \n" ) ;
exi t ( EXI T_FAI LURE) ;
}
pr i nt f ( " Ent er name of t ar get f i l e\n" ) ;
get s( t ar get _f i l e) ;
t ar get = f open( t ar get _f i l e, " w" ) ;
i f ( t ar get == NULL )
{
f cl ose( sour ce) ;
pr i nt f ( " Pr ess any key t o exi t . . . \n" ) ;
exi t ( EXI T_FAI LURE) ;
}
whi l e( ( ch = f get c( sour ce) ) ! = EOF )
f put c( ch, t ar get ) ;
pr i nt f ( " Fi l e copi ed successf ul l y. \n" ) ;
f cl ose( sour ce) ;
f cl ose( t ar get ) ;
r et ur n 0;
}
Output of program:
58. C program to merge two files
This c program merges two files and stores their contents in another file. The files which are
to be merged are opened in read mode and the file which contains content of both the files is
opened in write mode. To merge two files first we open a file and read it character by
character and store the read contents in 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
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
i nt mai n( )
{
FI LE *f s1, *f s2, *f t ;
char ch, f i l e1[ 20] , f i l e2[ 20] , f i l e3[ 20] ;
pr i nt f ( " Ent er name of f i r st f i l e\n" ) ;
get s( f i l e1) ;
pr i nt f ( " Ent er name of second f i l e\n" ) ;
get s( f i l e2) ;
pr i nt f ( " Ent er name of f i l e whi ch wi l l st or e cont ent s of t wo f i l es\n" ) ;
get s( f i l e3) ;
f s1 = f open( f i l e1, " r " ) ;
f s2 = f open( f i l e2, " r " ) ;
i f ( f s1 == NULL | | f s2 == NULL )
{
per r or ( " Er r or " ) ;
pr i nt f ( " Pr ess any key t o exi t . . . \n" ) ;
get ch( ) ;
exi t ( EXI T_FAI LURE) ;
}
f t = f open( f i l e3, " w" ) ;
i f ( f t == NULL )
{
per r or ( " Er r or " ) ;
pr i nt f ( " Pr ess any key t o exi t . . . \n" ) ;
exi t ( EXI T_FAI LURE) ;
}
whi l e( ( ch = f get c( f s1) ) ! = EOF )
f put c( ch, f t ) ;
whi l e( ( ch = f get c( f s2) ) ! = EOF )
f put c( ch, f t ) ;
pr i nt f ( " Two f i l es wer e mer ged i nt o %s f i l e successf ul l y. \n" , f i l e3) ;
f cl ose( f s1) ;
f cl ose( f s2) ;
f cl ose( f t ) ;
r et ur n 0;
}
Output of program:
59. 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(Turbo C compiler only)
#i ncl ude <st di o. h>
#i ncl ude <coni o. h>
#i ncl ude <di r . h>
i nt mai n( )
{
i nt done;
st r uct f f bl k a;
pr i nt f ( " Pr ess any key t o vi ew t he f i l es i n t he cur r ent di r ect or y\n" ) ;
get ch( ) ;
done = f i ndf i r st ( " *. *" , &a, 0) ;
whi l e( ! done)
{
pr i nt f ( " %s\n" , a. f f _name) ;
done = f i ndnext ( &a) ;
}
get ch( ) ;
r et ur n 0;
}
Obviously you will get a different output when you will execute this file on your computer.
60. 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, 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 programming code
#i ncl ude<st di o. h>
mai n( )
{
i nt st at us;
char f i l e_name[ 25] ;
pr i nt f ( " Ent er t he name of f i l e you wi sh t o del et e\n" ) ;
get s( f i l e_name) ;
st at us = r emove( f i l e_name) ;
i f ( st at us == 0 )
pr i nt f ( " %s f i l e del et ed successf ul l y. \n" , f i l e_name) ;
el se
{
pr i nt f ( " Unabl e t o del et e t he f i l e\n" ) ;
per r or ( " Er r or " ) ;
}
r et ur n 0;
}
Output of program:
Deleted file doesn't go to trash or recycle bin so you may not be able to recover it. Deleted
files can be recovered using special recovery software if the files are not overwritten on the
storage medium.
61. 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 using rand
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
i nt mai n( ) {
i nt c, n;
pr i nt f ( " Ten r andomnumber s i n [ 1, 100] \n" ) ;
f or ( c = 1; c <= 10; c++) {
n = r and( ) %100 + 1;
pr i nt f ( " %d\n" , n) ;
}
r et ur n 0;
}
C programming code using random function(Turbo C
compiler only)
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
#i ncl ude<st dl i b. h>
mai n( )
{
i nt n, max, num, c;
pr i nt f ( " Ent er t he number of r andomnumber s you want " ) ;
scanf ( " %d" , &n) ;
pr i nt f ( " Ent er t he maxi mumval ue of r andomnumber " ) ;
scanf ( " %d" , &max) ;
pr i nt f ( " %d r andomnumber s f r om0 t o %d ar e : - \n" , n, max) ;
r andomi ze( ) ;
f or ( c = 1 ; c <= n ; c++ )
{
num= r andom( max) ;
pr i nt f ( " %d\n" , num) ;
}
get ch( ) ;
r et ur n 0;
}
62. 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 prints it. User will have to enter the real
and imaginary parts of two complex numbers. In our program we will add real parts and
imaginary parts of complex numbers 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). A structure is used to store complex number.
C programming code
#i ncl ude <st di o. h>
st r uct compl ex
{
i nt r eal , i mg;
};
i nt mai n( )
{
st r uct compl ex a, b, c;
pr i nt f ( " Ent er a and b wher e a + i b i s t he f i r st compl ex number . \n" ) ;
pr i nt f ( " a = " ) ;
scanf ( " %d" , &a. r eal ) ;
pr i nt f ( " b = " ) ;
scanf ( " %d" , &a. i mg) ;
pr i nt f ( " Ent er c and d wher e c + i d i s t he second compl ex number . \n" ) ;
pr i nt f ( " c = " ) ;
scanf ( " %d" , &b. r eal ) ;
pr i nt f ( " d = " ) ;
scanf ( " %d" , &b. i mg) ;
c. r eal = a. r eal + b. r eal ;
c. i mg = a. i mg + b. i mg;
i f ( c. i mg >= 0 )
pr i nt f ( " Sumof t wo compl ex number s = %d + %di \n" , c. r eal , c. i mg) ;
el se
pr i nt f ( " Sumof t wo compl ex number s = %d %di \n" , c. r eal , c. i mg) ;
r et ur n 0;
}
Output of c program to add two complex numbers is shown in image below:
63. C program to print date
This c program prints current system date. To print date we will use getdate function.
C programming code (Works in Turbo C only)
#i ncl ude <st di o. h>
#i ncl ude <coni o. h>
#i ncl ude <dos. h>
i nt mai n( )
{
st r uct dat e d;
get dat e( &d) ;
pr i nt f ( " Cur r ent syst emdat e i s %d/ %d/ %d" , d. da_day, d. da_mon, d. da_year ) ;
get ch( ) ;
r et ur n 0;
}
This code works in Turbo C only because it supports dos.h header file.
64. 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
#i ncl ude<st dl i b. h>
i nt mai n( )
{
syst em( " C: \\Wi ndows\\Syst em32\\i pconf i g" ) ;
r et ur n 0;
}
Output of program: ( In Windows 7)
65. 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
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
mai n( )
{
char ch;
pr i nt f ( " Do you want t o shut down your comput er now ( y/ n) \n" ) ;
scanf ( " %c" , &ch) ;
i f ( ch == ' y' | | ch == ' Y' )
syst em( " C: \\WI NDOWS\\Syst em32\\shut down - s" ) ;
r et ur n 0;
}
C programming code for Windows 7
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
mai n( )
{
char ch;
pr i nt f ( " Do you want t o shut down your comput er now ( y/ n) \n" ) ;
scanf ( " %c" , &ch) ;
i f ( ch == ' y' | | ch == ' Y' )
syst em( " C: \\WI NDOWS\\Syst em32\\shut down / s" ) ;
r et ur n 0;
}
To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart
use /r instead of /s.
C programming code for Ubuntu Linux
#i ncl ude <st di o. h>
i nt mai n( ) {
syst em( " shut down - P now" ) ;
r et ur n 0;
}
You need to be logged in as root user for above program to execute otherwise you will get
the message shutdown: Need to be root, nowspecifies that you want to shutdown
immediately. '-P' option specifies you want to power off your machine. You can specify
minutes as:
shutdown -P "number of minutes"
For more help or options type at terminal: man shutdown.

Das könnte Ihnen auch gefallen