C Programming by Examples: int n;
Check odd or even printf("Enter an integer\n");
Example 1 scanf("%d",&n);
#include<stdio.h>
main() if ( (n/2)*2 == n )
{ printf("Even\n");
int n; else
printf("Odd\n");
printf("Enter an integer\n");
scanf("%d",&n); return 0;
}
if ( n%2 == 0 ) Example 4
printf("Even\n"); #include<stdio.h>
else
printf("Odd\n"); main()
{
return 0; int n;
}
printf("Enter an integer\n");
Example 2 scanf("%d",&n);
#include<stdio.h>
n%2 == 0 ? printf("Even number\n") :
main() printf("Odd number\n");
{
int n; return 0;
}
printf("Enter an integer\n");
scanf("%d",&n); c program to check whether input alphabet is a
vowel or not.
if ( n & 1 == 1 ) #include <stdio.h>
printf("Odd\n");
else main()
printf("Even\n"); {
char ch;
return 0;
} printf("Enter a character\n");
Example 3 scanf("%c", &ch);
#include<stdio.h>
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'
main() || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' ||
{ ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch); {
else remainder = n % 10;
printf("%c is not a vowel.\n", ch); sum = sum + remainder;
n = n / 10;
return 0; }
}
printf("Sum of digits of entered number = %d\
c program to check leap year n",sum);
#include <stdio.h>
return 0;
int main() }
{
int year; Add digits using recursion Example
printf("Enter a year to check if it is a leap year\ #include <stdio.h>
n");
scanf("%d", &year); int add_digits(int);
if ( year%400 == 0) int main() {
printf("%d is a leap year.\n", year); int n, result;
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year); scanf("%d", &n);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year); result = add_digits(n);
else
printf("%d is not a leap year.\n", year); printf("%d\n", result);
return 0; return 0;
} }
Add digits of number in c int add_digits(int n) {
Example 1 static int sum = 0;
#include <stdio.h>
if (n == 0) {
main() return 0;
{ }
int n, sum = 0, remainder;
sum = n%10 + add_digits(n/10);
printf("Enter an integer\n");
scanf("%d",&n); return sum;
}
while(n != 0)
Factorial program in c printf("Greatest common divisor of %d and %d
#include <stdio.h> = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d =
int main() %d\n", x, y, lcm);
{
int c, n, fact = 1; return 0;
}
printf("Enter a number to calculate it's
factorial\n"); Example 2
scanf("%d", &n); #include <stdio.h>
for (c = 1; c <= n; c++) long gcd(long, long);
fact = fact * c;
int main() {
printf("Factorial of %d = %d\n", n, fact); long x, y, hcf, lcm;
return 0; printf("Enter two integers\n");
} scanf("%ld%ld", &x, &y);
C program to find hcf and lcm: The code below hcf = gcd(x, y);
finds highest common factor and least common lcm = (x*y)/hcf;
multiple of two integers.
#include <stdio.h> printf("Greatest common divisor of %ld and
%ld = %ld\n", x, y, hcf);
int main() { printf("Least common multiple of %ld and %ld
int a, b, x, y, t, gcd, lcm; = %ld\n", x, y, lcm);
printf("Enter two integers\n"); return 0;
scanf("%d%d", &x, &y); }
a = x; long gcd(long x, long y) {
b = y; if (x == 0) {
return y;
while (b != 0) { }
t = b;
b = a % b; while (y != 0) {
a = t; if (x > y) {
} x = x - y;
}
gcd = a; else {
lcm = (x*y)/gcd; y = y - x;
}
} main()
{
return x; int n, r;
} long ncr, npr;
Decimal to binary conversion printf("Enter the value of n and r\n");
#include <stdio.h> scanf("%d%d",&n,&r);
int main() ncr = find_ncr(n, r);
{ npr = find_npr(n, r);
int n, c, k;
printf("%dC%d = %ld\n", n, r, ncr);
printf("Enter an integer in decimal number printf("%dP%d = %ld\n", n, r, npr);
system\n");
scanf("%d", &n); return 0;
}
printf("%d in binary number system is:\n", n);
long find_ncr(int n, int r)
for (c = 31; c >= 0; c--) {
{ long result;
k = n >> c;
result = factorial(n)/(factorial(r)*factorial(n-
if (k & 1) r));
printf("1");
else return result;
printf("0"); }
}
long find_npr(int n, int r)
printf("\n"); {
long result;
return 0;
} result = factorial(n)/factorial(n-r);
c program to find ncr and npr
c program to find nCr and nPr: This code return result;
calculate nCr which is n!/(r!*(n-r)!) and nPr = }
n!/(n-r)!
#include<stdio.h> long factorial(int n)
{
long factorial(int); int c;
long find_ncr(int, int); long result = 1;
long find_npr(int, int);
for( c = 1 ; c <= n ; c++ )
result = result*c; x = y;
y = temp;
return ( result );
} printf("After Swapping\nx = %d\ny = %d\
n",x,y);
c program to add n numbers
#include <stdio.h> return 0;
}
int main()
{ c program to reverse a number
int n, sum = 0, c, value; #include <stdio.h>
printf("Enter the number of integers you want main()
to add\n"); {
scanf("%d", &n); int n, reverse = 0;
printf("Enter %d integers\n",n); printf("Enter a number to reverse\n");
scanf("%d",&n);
for (c = 1; c <= n; c++)
{ while (n != 0)
scanf("%d",&value); {
sum = sum + value; reverse = reverse * 10;
} reverse = reverse + n%10;
n = n/10;
printf("Sum of entered integers = %d\n",sum); }
return 0; printf("Reverse of entered number is = %d\n",
} reverse);
c program to swap two numbers
#include <stdio.h> return 0;
}
int main()
{ Palindrome number algorithm
int x, y, temp; 1. Get the number from user.
2. Reverse it.
printf("Enter the value of x and y\n"); 3. Compare it with the number entered by the
scanf("%d%d", &x, &y); user.
4. If both are same then print palindrome
printf("Before Swapping\nx = %d\ny = %d\ number
n",x,y); 5. Else print not a palindrome number.
#include<stdio.h>
temp = x;
main()
{ temp = n;
int n, reverse = 0, temp;
for ( row = 1 ; row <= n ; row++ )
printf("Enter a number to check if it is a {
palindrome or not\n"); for ( c = 1 ; c < temp ; c++ )
scanf("%d",&n); printf(" ");
temp = n; temp--;
while( temp != 0 ) for ( c = 1 ; c <= 2*row - 1 ; c++ )
{ printf("*");
reverse = reverse * 10;
reverse = reverse + temp%10; printf("\n");
temp = temp/10; }
}
return 0;
if ( n == reverse ) }
printf("%d is a palindrome number.\n", n);
else c program to print diamond pattern
printf("%d is not a palindrome number.\n", #include <stdio.h>
n);
int main()
return 0; {
} int n, c, k, space = 1;
c program to print patterns of numbers and printf("Enter number of rows\n");
stars scanf("%d", &n);
*
*** space = n - 1;
*****
******* for (k = 1; k <= n; k++)
********* {
#include<stdio.h> for (c = 1; c <= space; c++)
printf(" ");
main()
{ space--;
int row, c, n, temp;
for (c = 1; c <= 2*k-1; c++)
printf("Enter the number of rows in pyramid printf("*");
of stars you wish to see ");
scanf("%d",&n); printf("\n");
} }
if ( c == i )
space = 1; {
printf("%d\n",i);
for (k = 1; k <= n - 1; k++) count++;
{ }
for (c = 1; c <= space; c++) i++;
printf(" "); }
space++; return 0;
}
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*"); C program for prime number or not
#include<stdio.h>
printf("\n");
} main()
{
return 0; int n, c = 2;
}
c program for prime number printf("Enter a number to check if it is prime\
#include<stdio.h> n");
scanf("%d",&n);
main()
{ for ( c = 2 ; c <= n - 1 ; c++ )
int n, i = 3, count, c; {
if ( n%c == 0 )
printf("Enter the number of prime numbers {
required\n"); printf("%d is not prime.\n", n);
scanf("%d",&n); break;
}
if ( n >= 1 ) }
{ if ( c == n )
printf("First %d prime numbers are :\n",n); printf("%d is prime.\n", n);
printf("2\n");
} return 0;
}
for ( count = 2 ; count <= n ; )
{ armstrong number c program
for ( c = 2 ; c <= i - 1 ; c++ )
{ armstrong number c program: c programming
if ( i%c == 0 ) code to check whether a number is armstrong
break; 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 printf("Enter the maximum range upto which
armstrong number as 33 + 73 + 13 = 371. Some you want to find armstrong numbers ");
other armstrong numbers are: 0, 1, 153, 370, scanf("%ld",&number);
407.
#include <stdio.h> printf("Following armstrong numbers are
found from 1 to %ld\n",number);
main()
{ for( c = 1 ; c <= number ; c++ )
int number, sum = 0, temp, remainder; {
temp = c;
printf("Enter a number\n"); while( temp != 0 )
scanf("%d",&number); {
r = temp%10;
temp = number; sum = sum + r*r*r;
temp = temp/10;
while( temp != 0 ) }
{ if ( c == sum )
remainder = temp%10; printf("%ld\n", c);
sum = sum + sum = 0;
remainder*remainder*remainder; }
temp = temp/10;
} getch();
return 0;
if ( number == sum ) }
printf("Entered number is an armstrong
number."); Fibonacci series in c
else /* Fibonacci Series c language */
printf("Entered number is not an armstrong #include<stdio.h>
number.");
main()
return 0; {
} int n, first = 0, second = 1, next, c;
c program to generate and print armstrong
numbers printf("Enter the number of terms\n");
#include<stdio.h> scanf("%d",&n);
#include<conio.h>
printf("First %d terms of Fibonacci series
main() are :-\n",n);
{
int r; for ( c = 0 ; c < n ; c++ )
long number = 0, c, sum = 0, temp; {
if ( c <= 1 )
next = c; c program to print Pascal triangle
else 1
{ 11
next = first + second; 121
first = second; 1331
second = next;
} #include<stdio.h>
printf("%d\n",next);
} long factorial(int);
return 0; main()
} {
int i, n, c;
c program to print Floyd's triangle
1 printf("Enter the number of rows you wish to
23 see in pascal triangle\n");
456 scanf("%d",&n);
7 8 9 10
for ( i = 0 ; i < n ; i++ )
#include <stdio.h> {
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
int main() printf(" ");
{
int n, i, c, a = 1; for( c = 0 ; c <= i ; c++ )
printf("%ld
printf("Enter the number of rows of Floyd's ",factorial(i)/(factorial(c)*factorial(i-c)));
triangle to print\n");
scanf("%d", &n); printf("\n");
}
for (i = 1; i <= n; i++)
{ return 0;
for (c = 1; c <= i; c++) }
{
printf("%d ",a); long factorial(int n)
a++; {
} int c;
printf("\n"); long result = 1;
}
for( c = 1 ; c <= n ; c++ )
return 0; result = result*c;
}
return ( result ); for (c = 1; c < size; c++)
} {
if (array[c] > maximum)
c program to add two numbers using pointers {
#include<stdio.h> maximum = array[c];
location = c+1;
main() }
{ }
int first, second, *p, *q, sum;
printf("Maximum element is present at
printf("Enter two integers to add\n"); location number %d and it's value is %d.\n",
scanf("%d%d", &first, &second); location, maximum);
return 0;
p = &first; }
q = &second;
c program to find minimum element in array
sum = *p + *q; #include <stdio.h>
printf("Sum of entered numbers = %d\ main()
n",sum); {
int array[100], minimum, size, c, location = 1;
return 0;
} printf("Enter the number of elements in
array\n");
c program to find maximum element in array scanf("%d",&size);
#include <stdio.h>
printf("Enter %d integers\n", size);
int main()
{ for ( c = 0 ; c < size ; c++ )
int array[100], maximum, size, c, location = 1; scanf("%d", &array[c]);
printf("Enter the number of elements in array\ minimum = array[0];
n");
scanf("%d", &size); for ( c = 1 ; c < size ; c++ )
{
printf("Enter %d integers\n", size); if ( array[c] < minimum )
{
for (c = 0; c < size; c++) minimum = array[c];
scanf("%d", &array[c]); location = c+1;
}
maximum = array[0]; }
printf("Minimum element is present at C program for binary search
location number %d and it's value is %d.\n", #include<stdio.h>
location, minimum);
return 0; main()
} {
int c, first, last, middle, n, search, array[100];
linear search in c
#include<stdio.h> printf("Enter number of elements\n");
scanf("%d",&n);
main()
{ printf("Enter %d integers\n", n);
int array[100], search, c, number;
for ( c = 0 ; c < n ; c++ )
printf("Enter the number of elements in array\ scanf("%d",&array[c]);
n");
scanf("%d",&number); printf("Enter value to find\n");
scanf("%d",&search);
printf("Enter %d numbers\n", number);
first = 0;
for ( c = 0 ; c < number ; c++ ) last = n - 1;
scanf("%d",&array[c]); middle = (first+last)/2;
printf("Enter the number to search\n"); while( first <= last )
scanf("%d",&search); {
if ( array[middle] < search )
for ( c = 0 ; c < number ; c++ ) first = middle + 1;
{ else if ( array[middle] == search )
if ( array[c] == search ) /* if required {
element found */ printf("%d found at location %d.\n",
{ search, middle+1);
printf("%d is present at location %d.\n", break;
search, c+1); }
break; else
} last = middle - 1;
}
if ( c == number ) middle = (first + last)/2;
printf("%d is not present in array.\n", }
search); if ( first > last )
printf("Not found! %d is not present in the
return 0; list.\n", search);
}
return 0;
} */
c program to reverse an array for (c = 0; c < n; c++)
a[c] = b[c];
c program to reverse an array :- This program
reverses the array elements. For example if a is printf("Reverse array is\n");
an array of integers with three elements
such that for (c = 0; c < n; c++)
a[0] = 1 printf("%d\n", a[c]);
a[1] = 2
a[2] = 3 return 0;
then on reversing the array will be }
a[0] = 3
a[1] = 2 c program to insert an element in an array
a[0] = 1 #include <stdio.h>
#include <stdio.h>
int main()
int main() {
{ int array[100], position, c, n, value;
int n, c, d, a[100], b[100];
printf("Enter number of elements in array\n");
printf("Enter the number of elements in array\ scanf("%d", &n);
n");
scanf("%d", &n); printf("Enter %d elements\n", n);
printf("Enter the array elements\n"); for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < n ; c++)
scanf("%d", &a[c]); printf("Enter the location where you wish to
insert an element\n");
/* scanf("%d", &position);
* Copying elements into array b starting from
end of array a printf("Enter the value to insert\n");
*/ scanf("%d", &value);
for (c = n - 1, d = 0; c >= 0; c--, d++) for (c = n - 1; c >= position - 1; c--)
b[d] = a[c]; array[c+1] = array[c];
/* array[position-1] = value;
* Copying reversed array into original.
* Here we are modifying original array, this is printf("Resultant array is\n");
optional.
for (c = 0; c <= n; c++) #include <stdio.h>
printf("%d\n", array[c]);
void merge(int [], int, int [], int, int []);
return 0;
} int main() {
int a[100], b[100], m, n, c, sorted[200];
c program to delete an element from an array
#include <stdio.h> printf("Input number of elements in first array\
n");
main() scanf("%d", &m);
{
int array[100], position, c, n; printf("Input %d integers\n", m);
for (c = 0; c < m; c++) {
printf("Enter number of elements in array\n"); scanf("%d", &a[c]);
scanf("%d", &n); }
printf("Enter %d elements\n", n); printf("Input number of elements in second
array\n");
for ( c = 0 ; c < n ; c++ ) scanf("%d", &n);
scanf("%d", &array[c]);
printf("Input %d integers\n", n);
printf("Enter the location where you wish to for (c = 0; c < n; c++) {
delete element\n"); scanf("%d", &b[c]);
scanf("%d", &position); }
if ( position >= n+1 ) merge(a, m, b, n, sorted);
printf("Deletion not possible.\n");
else printf("Sorted array:\n");
{
for ( c = position - 1 ; c < n - 1 ; c++ ) for (c = 0; c < m + n; c++) {
array[c] = array[c+1]; printf("%d\n", sorted[c]);
}
printf("Resultant array is\n");
return 0;
for( c = 0 ; c < n - 1 ; c++ ) }
printf("%d\n", array[c]);
} void merge(int a[], int m, int b[], int n, int
sorted[]) {
return 0; int i, j, k;
}
j = k = 0;
C program to merge two arrays
for (i = 0; i < m + n;) {
if (j < m && k < n) { for (c = 0; c < n; c++)
if (a[j] < b[k]) { scanf("%d", &array[c]);
sorted[i] = a[j];
j++; for (c = 0 ; c < ( n - 1 ); c++)
} {
else { for (d = 0 ; d < n - c - 1; d++)
sorted[i] = b[k]; {
k++; if (array[d] > array[d+1]) /* For decreasing
} order use < */
i++; {
} swap = array[d];
else if (j == m) { array[d] = array[d+1];
for (; i < m + n;) { array[d+1] = swap;
sorted[i] = b[k]; }
k++; }
i++; }
}
} printf("Sorted list in ascending order:\n");
else {
for (; i < m + n;) { for ( c = 0 ; c < n ; c++ )
sorted[i] = a[j]; printf("%d\n", array[c]);
j++;
i++; return 0;
} }
}
} insertion sort in c
} /* insertion sort ascending order */
c program for bubble sort #include <stdio.h>
/* Bubble sort code */
int main()
#include <stdio.h> {
int n, array[1000], c, d, t;
int main()
{ printf("Enter number of elements\n");
int array[100], n, c, d, swap; scanf("%d", &n);
printf("Enter number of elements\n"); printf("Enter %d integers\n", n);
scanf("%d", &n);
for (c = 0; c < n; c++) {
printf("Enter %d integers\n", n); scanf("%d", &array[c]);
} for ( d = c + 1 ; d < n ; d++ )
{
for (c = 1 ; c <= n - 1; c++) { if ( array[position] > array[d] )
d = c; position = d;
}
while ( d > 0 && array[d] < array[d-1]) { if ( position != c )
t = array[d]; {
array[d] = array[d-1]; swap = array[c];
array[d-1] = t; array[c] = array[position];
array[position] = swap;
d--; }
} }
}
printf("Sorted list in ascending order:\n");
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
for (c = 0; c <= n - 1; c++) { printf("%d\n", array[c]);
printf("%d\n", array[c]);
} return 0;
}
return 0;
} c program to add two matrix
First Matrix :-
selection sort in c 12
#include<stdio.h> 34
Second matrix :-
main() 45
{ -1 5
int array[100], n, c, d, position, swap; then output of the program ( sum of First and
Second matrix ) will be
printf("Enter number of elements\n"); 57
scanf("%d", &n); 29
printf("Enter %d integers\n", n); #include <stdio.h>
for ( c = 0 ; c < n ; c++ ) main()
scanf("%d", &array[c]); {
int m, n, c, d, first[10][10], second[10][10],
for ( c = 0 ; c < ( n - 1 ) ; c++ ) sum[10][10];
{
position = c; printf("Enter the number of rows and columns
of matrix ");
scanf("%d%d", &m, &n); {
printf("Enter the elements of first matrix\n"); int m, n, c, d, matrix[10][10], transpose[10]
[10];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ ) printf("Enter the number of rows and columns
scanf("%d", &first[c][d]); of matrix ");
scanf("%d%d",&m,&n);
printf("Enter the elements of second matrix\ printf("Enter the elements of matrix \n");
n");
for( c = 0 ; c < m ; c++ )
for ( c = 0 ; c < m ; c++ ) {
for ( d = 0 ; d < n ; d++ ) for( d = 0 ; d < n ; d++ )
scanf("%d", &second[c][d]); {
scanf("%d",&matrix[c][d]);
for ( c = 0 ; c < m ; c++ ) }
for ( d = 0 ; d < n ; d++ ) }
sum[c][d] = first[c][d] + second[c][d];
for( c = 0 ; c < m ; c++ )
printf("Sum of entered matrices:-\n"); {
for( d = 0 ; d < n ; d++ )
for ( c = 0 ; c < m ; c++ ) {
{ transpose[d][c] = matrix[c][d];
for ( d = 0 ; d < n ; d++ ) }
printf("%d\t", sum[c][d]); }
printf("\n"); printf("Transpose of entered matrix :-\n");
}
for( c = 0 ; c < n ; c++ )
return 0; {
} for( d = 0 ; d < m ; d++ )
{
c program to transpose a matrix printf("%d\t",transpose[c][d]);
12 }
34 printf("\n");
56 }
then transpose of above matrix will be
135 return 0;
246 }
#include<stdio.h> Matrix multiplication in c
#include <stdio.h>
main()
int main() }
{ }
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10] printf("Product of entered matrices:-\n");
[10];
for ( c = 0 ; c < m ; c++ )
printf("Enter the number of rows and columns {
of first matrix\n"); for ( d = 0 ; d < q ; d++ )
scanf("%d%d", &m, &n); printf("%d\t", multiply[c][d]);
printf("Enter the elements of first matrix\n");
printf("\n");
for ( c = 0 ; c < m ; c++ ) }
for ( d = 0 ; d < n ; d++ ) }
scanf("%d", &first[c][d]);
return 0;
printf("Enter the number of rows and columns }
of second matrix\n");
scanf("%d%d", &p, &q); C program to find string length
#include<stdio.h>
if ( n != p ) #include<string.h>
printf("Matrices with entered orders can't be
multiplied with each other.\n"); main()
else {
{ char a[100];
printf("Enter the elements of second matrix\ int length;
n");
printf("Enter a string to calculate it's length\
for ( c = 0 ; c < p ; c++ ) n");
for ( d = 0 ; d < q ; d++ ) gets(a);
scanf("%d", &second[c][d]);
length = strlen(a);
for ( c = 0 ; c < m ; c++ )
{ printf("Length of entered string is = %d\
for ( d = 0 ; d < q ; d++ ) n",length);
{
for ( k = 0 ; k < p ; k++ ) return 0;
{ }
sum = sum + first[c][k]*second[k][d];
} c program to compare two strings
#include<stdio.h>
multiply[c][d] = sum; #include<string.h>
sum = 0;
main() }
{
char a[100], b[100]; int compare_string(char *first, char *second)
{
printf("Enter the first string\n"); while(*first==*second)
gets(a); {
if ( *first == '\0' || *second == '\0' )
printf("Enter the second string\n"); break;
gets(b);
first++;
if( strcmp(a,b) == 0 ) second++;
printf("Entered strings are equal.\n"); }
else if( *first == '\0' && *second == '\0' )
printf("Entered strings are not equal.\n"); return 0;
else
return 0; return -1;
} }
C program to compare two strings without string copying in c programming
using strcmp #include<stdio.h>
#include<stdio.h> #include<string.h>
int compare_string(char*, char*); main()
{
main() char source[] = "C program";
{ char destination[50];
char first[100], second[100], result;
strcpy(destination, source);
printf("Enter first string\n");
gets(first); printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
printf("Enter second string\n");
gets(second); return 0;
}
result = compare_string(first, second);
c program to concatenate strings
if ( result == 0 ) #include<stdio.h>
printf("Both strings are same.\n"); #include<conio.h>
else #include<string.h>
printf("Entered strings are not equal.\n");
main()
return 0; {
char a[100], b[100]; main()
{
printf("Enter the first string\n"); char a[100], b[100];
gets(a);
printf("Enter the string to check if it is a
printf("Enter the second string\n"); palindrome\n");
gets(b); gets(a);
strcat(a,b); strcpy(b,a);
strrev(b);
printf("String obtained on concatenation is
%s\n",a); if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
getch(); else
return 0; printf("Entered string is not a palindrome.\
} n");
Reverse string return 0;
/* String reverse in c*/ }
#include<stdio.h>
#include<string.h> remove vowels string c
#include <stdio.h>
main() #include <string.h>
{
char arr[100]; int check_vowel(char);
printf("Enter a string to reverse\n"); int main()
gets(arr); {
char s[100], t[100];
strrev(arr); int i, j = 0;
printf("Reverse of entered string is \n%s\ printf("Enter a string to delete vowels\n");
n",arr); gets(s);
return 0; for(i = 0; s[i] != '\0'; i++) {
} if(check_vowel(s[i]) == 0) { //not a vowel
t[j] = s[i];
c palindrome program, c program for j++;
palindrome }
#include <stdio.h> }
#include <string.h>
t[j] = '\0';
printf("Enter the position and length of
strcpy(s, t); //We are changing initial string substring\n");
scanf("%d%d",&position, &length);
printf("String after deleting vowels: %s\n", s);
pointer = substring( string, position, length);
return 0;
} printf("Required substring is \"%s\"\n",
pointer);
int check_vowel(char c) free(pointer);
{
switch(c) { return 0;
case 'a': }
case 'A':
case 'e': /*C substring function: It returns a pointer to
case 'E': the substring */
case 'i':
case 'I': char *substring(char *string, int position, int
case 'o': length)
case 'O': {
case 'u': char *pointer;
case 'U': int c;
return 1;
default: pointer = malloc(length+1);
return 0;
} if (pointer == NULL)
} {
printf("Unable to allocate memory.\n");
Substring in c programming, c substring exit(EXIT_FAILURE);
#include <stdio.h> }
#include <malloc.h>
for (c = 0 ; c < position -1 ; c++)
char* substring(char*, int, int); string++;
main() for (c = 0 ; c < length ; c++)
{ {
char string[100], *pointer; *(pointer+c) = *string;
int position, length; string++;
}
printf("Enter a string\n");
gets(string); *(pointer+c) = '\0';
return pointer; }
} pointer++;
}
c program to sort a string in alphabetic order pointer = s;
#include <stdio.h> }
#include <stdlib.h> *(result+d) = '\0';
#include <string.h>
strcpy(s, result);
void sort_string(char*); free(result);
}
main()
{ c program remove spaces, blanks from a string
char string[100]; #include <stdio.h>
printf("Enter some text\n"); int main()
gets(string); {
char text[100], blank[100];
sort_string(string); int c = 0, d = 0;
printf("%s\n", string);
printf("Enter some text\n");
return 0; gets(text);
}
while (text[c] != '\0')
void sort_string(char *s) {
{ if (!(text[c] == ' ' && text[c+1] == ' ')) {
int c, d = 0, length; blank[d] = text[c];
char *pointer, *result, ch; d++;
}
length = strlen(s); c++;
}
result = (char*)malloc(length+1);
blank[d] = '\0';
pointer = s;
printf("Text after removing blanks\n%s\n",
for ( ch = 'a' ; ch <= 'z' ; ch++ ) blank);
{
for ( c = 0 ; c < length ; c++ ) return 0;
{ }
if ( *pointer == ch )
{ strlwr, strupr in c
*(result+d) = *pointer;
d++;
Here we will change string case with and printf("Entered string in upper case is \"%s\"\
without strlwr, strupr functions. n", string);
#include<stdio.h> return 0;
#include<string.h> }
main() void upper_string(char *string)
{ {
char string[] = "Strlwr in C"; while(*string)
{
printf("%s\n",strlwr(string)); if ( *string >= 'a' && *string <= 'z' )
{
return 0; *string = *string - 32;
} }
============== string++;
#include<stdio.h> }
#include<string.h> }
main() Change string to lower case without strlwr
{ #include<stdio.h>
char string[] = "strupr in c";
void lower_string(char*);
printf("%s\n",strupr(string));
main()
return 0; {
} char string[100];
=====================
Change string to upper case without strupr printf("Enter a string to convert it into lower
#include<stdio.h> case\n");
gets(string);
void upper_string(char*);
lower_string(string);
main()
{ printf("Entered string in lower case is \"%s\"\
char string[100]; n", string);
printf("Enter a string to convert it into upper return 0;
case\n"); }
gets(string);
void lower_string(char *string)
upper_string(string); {
while(*string)
{ }
if ( *string >= 'A' && *string <= 'Z' )
{ c program to find frequency of characters in a
*string = *string + 32; string
} #include<stdio.h>
string++; #include<string.h>
}
} main()
{
c program to swap two strings char string[100], ch;
int c = 0, count[26] = {0};
#include<stdio.h>
#include<string.h> printf("Enter a string\n");
#include<malloc.h> gets(string);
#include<conio.h>
while ( string[c] != '\0' )
main() {
{ /* Considering characters from 'a' to 'z' only
char first[100], second[100], *temp; */
printf("Enter the first string "); if ( string[c] >= 'a' && string[c] <= 'z' )
gets(first); count[string[c]-'a']++;
printf("Enter the second string "); c++;
gets(second); }
printf("\nBefore Swapping\n"); for ( c = 0 ; c < 26 ; c++ )
printf("First string: %s\n",first); {
printf("Second string: %s\n\n",second); if( count[c] != 0 )
printf("%c occurs %d times in the entered
temp = (char*)malloc(100); string.\n",c+'a',count[c]);
}
strcpy(temp,first);
strcpy(first,second); return 0;
strcpy(second,temp); }
printf("After Swapping\n"); anagram in c
printf("First string: %s\n",first); #include <stdio.h>
printf("Second string: %s\n",second);
int check_anagram(char [], char []);
getch();
return 0; int main()
{ if (first[c] != second[c])
char a[100], b[100]; return 0;
int flag; }
printf("Enter first string\n"); return 1;
gets(a); }
printf("Enter second string\n"); c program to read a file
gets(b); #include<stdio.h>
#include<stdlib.h>
flag = check_anagram(a, b);
main()
if (flag == 1) {
printf("\"%s\" and \"%s\" are anagrams.\n", char ch, file_name[25];
a, b); FILE *fp;
else
printf("\"%s\" and \"%s\" are not printf("Enter the name of file you wish to see
anagrams.\n", a, b); ");
gets(file_name);
return 0;
} fp = fopen(file_name,"r"); // read mode
int check_anagram(char a[], char b[]) if( fp == NULL )
{ {
int first[26] = {0}, second[26] = {0}, c = 0; perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
while (a[c] != '\0') }
{
first[a[c]-'a']++; printf("The contents of %s file are :- \n\n",
c++; file_name);
}
while( ( ch = fgetc(fp) ) != EOF )
c = 0; printf("%c",ch);
while (b[c] != '\0') fclose(fp);
{ return 0;
second[b[c]-'a']++; }
c++;
} c program to copy files
#include <stdio.h>
for (c = 0; c < 26; c++) #include <stdlib.h>
{
main() #include<stdlib.h>
{
char ch, source_file[20], target_file[20]; main()
FILE *source, *target; {
FILE *fs1, *fs2, *ft;
printf("Enter name of file to copy\n");
gets(source_file); char ch, file1[20], file2[20], file3[20];
source = fopen(source_file, "r"); printf("Enter name of first file ");
gets(file1);
if( source == NULL )
{ printf("Enter name of second file ");
printf("Press any key to exit...\n"); gets(file2);
exit(EXIT_FAILURE);
} printf("Enter name of file which will store
contents of two files ");
printf("Enter name of target file\n"); gets(file3);
gets(target_file);
fs1 = fopen(file1,"r");
target = fopen(target_file, "w"); fs2 = fopen(file2,"r");
if( target == NULL ) if( fs1 == NULL || fs2 == NULL )
{ {
fclose(source); perror("Error ");
printf("Press any key to exit...\n"); printf("Press any key to exit...\n");
exit(EXIT_FAILURE); getch();
} exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target); ft = fopen(file3,"w");
printf("File copied successfully.\n"); if( ft == NULL )
{
fclose(source); perror("Error ");
fclose(target); printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
return 0; }
}
while( ( ch = fgetc(fs1) ) != EOF )
c program to merge two files fputc(ch,ft);
#include<stdio.h>
#include<conio.h> while( ( ch = fgetc(fs2) ) != EOF )
fputc(ch,ft);
main()
printf("Two files were merged into %s file {
successfully.\n",file3); int status;
char file_name[25];
fclose(fs1);
fclose(fs2); printf("Enter the name of file you wish to
fclose(ft); delete\n");
gets(file_name);
getch();
return 0; status = remove(file_name);
}
if( status == 0 )
c program to list files in directory printf("%s file deleted successfully.\
#include<stdio.h> n",file_name);
#include<conio.h> else
#include<dir.h> {
printf("Unable to delete the file\n");
main() perror("Error");
{ }
int done;
struct ffblk a; return 0;
}
printf("Press any key to view the files in the
current directory\n"); c program to generate random numbers
#include <stdio.h>
getch(); #include <stdlib.h>
done = findfirst("*.*",&a,0); int main() {
int c, n;
while(!done)
{ printf("Ten random numbers in [1,100]\n");
printf("%s\n",a.ff_name);
done = findnext(&a); for (c = 1; c <= 10; c++) {
} n = rand()%100 + 1;
printf("%d\n", n);
getch(); }
return 0;
} return 0;
}
c program to delete a file
#include<stdio.h> c program to add two complex numbers
#include <stdio.h> main()
{
struct complex struct date d;
{
int real, img; getdate(&d);
};
printf("Current system date is
main() %d/%d/%d",d.da_day,d.da_mon,d.da_year);
{ getch();
struct complex a, b, c; return 0;
}
printf("Enter a and b where a + ib is the first
complex number.\n"); c program to get ip address
printf("a = "); #include<stdlib.h>
scanf("%d", &[Link]);
printf("b = "); main()
scanf("%d", &[Link]); {
printf("Enter c and d where c + id is the system("C:\\Windows\\System32\\ipconfig");
second complex number.\n"); system("pause");
printf("c = ");
scanf("%d", &[Link]); return 0;
printf("d = "); }
scanf("%d", &[Link]);
C program to shutdown or turn off computer
[Link] = [Link] + [Link]; C programming code for Windows XP
[Link] = [Link] + [Link]; #include <stdio.h>
#include <stdlib.h>
if ( [Link] >= 0 )
printf("Sum of two complex numbers = %d + main()
%di\n",[Link],[Link]); {
else char ch;
printf("Sum of two complex numbers = %d
%di\n",[Link],[Link]); printf("Do you want to shutdown your
computer now (y/n)\n");
return 0; scanf("%c",&ch);
}
if (ch == 'y' || ch == 'Y')
c program to print date system("C:\\WINDOWS\\System32\\
#include<stdio.h> shutdown -s");
#include<conio.h>
#include<dos.h> return 0;
}
C programming code for Windows 7
#include <stdio.h>
#include <stdlib.h>
main()
{
char ch;
printf("Do you want to shutdown your
computer now (y/n)\n");
scanf("%c",&ch);
if (ch == 'y' || ch == 'Y')
system("C:\\WINDOWS\\System32\\
shutdown /s");
return 0;
}