SlideShare a Scribd company logo
C programs cs567
Add Complex Numbers
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct complex
{
int real;
int img;
};
int main()
{
struct complex a, b, c;
printf(" Enter a and b where a + ib is the first complex number. ");
printf("n a = ");
scanf(" %d ", &a.real);
printf(" b = ");
scanf(" %d ", &a.img);
printf(" Enter c and d where c + id is the second complex number. ");
printf("n c = ");
scanf(" %d ", &b.real);
printf(" d = ");
scanf(" %d ", &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf(" Sum of two complex numbers = %d + %di ", c.real, c.img);
else
printf(" Sum of two complex numbers = %d %di ", c.real, c.img);
getch();
return 0;
}
Add without ADD Operator
#include<stdio.h>
int main()
{
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
C programs cs567
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers:%d",sum);
return 0;
}
Armstrong Number
void main()
{
int n,b=0,t;
clrscr();
printf("Enter the no");
scanf("%d",&n);
t=n;
while(n>0)
{
a=n%10;
b=b+a*a*a;
n=n/10;
}
if(b==t)
{
printf("Armstrong no");
}
else
{
printf("Not an armstrong no");
}
getch();
}
Binary Search
int BinarySearch(int *array, int number_of_elements, int key)
{
int low = 0, high = number_of_elements-1, mid;
while(low <= high)
{
mid = (low + high)/2;
C programs cs567
if(array[mid] < key)
{
low = mid + 1;
}
else if(array[mid] == key)
{
return mid;
}
else if(array[mid] > key)
{
high = mid-1;
}
}
return -1;
}
int main()
{
int number_of_elements;
scanf("%d",&number_of_elements);
int array[number_of_elements];
int iter;
for(iter = 1;iter < number_of_elements;iter++)
{
if(array[iter]< array[iter - 1])
{
printf("Given input is n not sortedn");
return 0;
}
}
int key;
scanf("%d",&key);
/* Calling this functions searches for the key and returns its index. It returns -1 if key is
not found.*/
int index;
index = BinarySearch(array,number_of_elements,key);
if(index==-1)
{
printf("Element not foundn");
}
else
{
C programs cs567
printf("Element is at index %dn ",index);
}
return 0;
}
Bubble Sort
#include <stdio.h>
void bubble_sort(long [], long);
int main()
{
long array[100], n, c, d, swap;
printf("Enter number of elements:");
scanf("%ld", &n);
printf("Enter %ld longegersn", n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:n");
for ( c = 0 ; c < n ; c++ )
printf("%ldn", array[c]);
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
t = list[d];
list[d] = list[d+1];
list[d+1]= t;
}
}
}
}
C programs cs567
Bucket Sort
#include<stdio.h>
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[n];
for(i=0; i < n; i++)
{
count[i] = 0;
}
for(i=0; i < n; i++)
{
(count[array[i]])++;
}
for(i=0,j=0; i < n; i++)
{
for(; count[i]>0;(count[i])--)
{
array[j++] = i;
}
}
}
int main()
{
int array[100];
int num;
int i;
printf("Enter How many Numbers : ");
scanf("%d",&num);
printf("Enter the %d elements to be sorted:n",num);
for(i = 0; i < num; i++ )
{
scanf("%d",&array[i]);
}
printf("n The array of elements before sorting : n");
for (i = 0;i < num;i++)
{
printf("%d ", array[i]);
}
printf("n The array of elements after sorting : n");
Bucket_Sort(array, num);
for (i = 0;i < n;i++)
C programs cs567
{
printf("%d ", array[i]);
}
printf("n");
return 0;
}
Celsius To Fahrenheit
#include <stdio.h>
#include <conio.h>
void main()
{
float c, f;
clrscr();
printf(" Enter temp in centigrade: ");
scanf("%f",&c);
f = ( 1.8 * c ) + 32;
printf(" Temperature in Fahrenheit = %f", f);
getch();
}
Character is Vowel or not
#include <stdio.h>
main()
{
char ch;
printf("Enter a charactern");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' ||
ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.n", ch);
else
printf("%c is not a vowel.n", ch);
return 0;
}
Combinations and Permutations
C programs cs567
#include <stdio.h>
#include <conio.h>
main()
{
int n , r, ncr( int , int);
long npr( int , int);
long double fact( int);
printf(" Enter value of n & r n");
scanf("%d %d",&n , &r);
if( n>= r)
{
printf( "%d C %d is %d n", n,r,ncr( n , r));
printf("%d P %d is %ld", n,r,npr( n, r));
}
else
{
printf("WRONG INPUT?? enter the correct input");
}
}
long double fact( int p)
{
long double facts = 1;
int i;
for( i = 1; i<= p; i++)
facts = facts * i;
return( facts);
}
int ncr ( int n, int r)
{
return( fact( n) / (fact( r) * fact(n- r) ) ) ;
}
long npr( int n , int r)
{
return( fact( n) / fact( n- r));
}
Convert Binary to Decimal,Octal,Hexadecimal
#include<stdio.h>
#include<string.h>
void hexadecimal();
void main()
{
int num, bnum, dec = 0, base = 1, rem ,dec1=0,oct[25],dec2=0,flag=0,i=0,counter=0,j;
printf("Enter the binary number(1s and 0s)n");
C programs cs567
scanf("%d", &num);
bnum = num;
while( num > 0)
{
rem = num % 10;
if((rem==0) || (rem==1))
{
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
flag=1;
}
else
{
flag=0;
printf("n Enter binary number n");
break;
}
}
if(flag==1)
{
printf("The Binary number is = %dn", bnum);
printf("Its decimal equivalent is =%dn", dec);
dec1=dec;
dec2=dec1;
while(dec>0)
{
rem=dec%8;
oct[i]=rem;
dec=dec/8;
i++;
counter++;
}
counter--;
printf("n Its octal equivalent is:");
while(counter>=0)
{
printf("%d" ,oct[counter]);
counter--;
}
printf("nIts Hexa Decimal equivalant is: ");
hexadecimal(dec2);
}
}
void hexadecimal(long n)
{
long i;
C programs cs567
if(n>0)
{
i=n%16;
n=n/16;
hexadecimal(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}
Copy the String
#include<stdio.h>
C programs cs567
void stringCopy(char[],char[]);
int main()
{
char str1[100],str2[100];
printf("Enter any string: ");
scanf("%s",str1);
stringCopy(str1,str2);
printf("After copying: %s",str2);
return 0;
}
void stringCopy(char str1[],char str2[])
{
int i=0;
while(str1[i]!='u0000')
{
str2[i] = str1[i];
i++;
}
str2[i]='u0000';
}
C programs cs567
Counting Frequencies Of Elements Of Array
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define S 6
main()
{
int a[S], freq[S];
int i, j, k,n = S;
clrscr();
for(i = 0; i < S; i++)
{
printf(" n Enter a[%d] element: ", i);
scanf(" %d ", &a[i]);
freq[i] = 1;
}
printf(" Original Arrayn ");
for(i = 0; i < S; i++)
printf(" %d ", a[i]);
/* Main Logic Starts Here */
for(i = 0; i < n; i++)
for(j = i + 1; j < n; j++)
{
if(a[i] == a[j])
{
for(k = j; k < n; k++)
a[k] = a[k+1];
freq[i]++;
n--;
}
}
printf(" nArray with freqn ");
printf(" nElement Freqn ");
for(i = 0; i < n; i++)
printf("%d %dn ", a[i], freq[i]);
getch();
}
C programs cs567
Count the Digit in a Number
#include<stdio.h>
int main()
{
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);
while(num)
{
num=num/10;
count++;
}
printf("Total digits is:%d",count);
return 0;
}
Factorial
#include <stdio.h>
#include <conio.h>
long int factorial(int n);
void main()
{
int n;
clrscr();
printf("Enter the number:n");
scanf("%d",&n);
printf("Factorial of %d is %ld",n,factorial(n));
getch();
}
long int factorial(int n)
{
if(n<=1)
{
return(01);
}
else
{
n=n*factorial(n-1);
return(n);
C programs cs567
}
}
Fibonacci Series
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("n Enter n for how many times generate series");
scanf("%d",&n);
printf("n FIBONACCI SERIES n");
printf("t%dt%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("t%d",c);
}
getch();
}
File-Copy one file contents to another
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(int argc, char *argv[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(argc!=3)
{
puts("Invalid number of arguments.");
exit(0);
C programs cs567
}
fs = fopen(argv[1],"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
ft = fopen(argv[2],"w");
if (ft==NULL)
{
puts("Target file cannot be opened.");
fclose(fs);
exit(0);
}
while(1)
{
ch=fgetc(fs);
if (ch==EOF)
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}
File Example
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("file.txt","w");
printf("nEnter data to be stored in to the file:");
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
return 0;
}
C programs cs567
File-How the data stored on the disk is read
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be openedn");
gets(filename);
fptr = fopen (filename, "r"); /*open for reading*/
if (fptr == NULL)
{
printf("Cannot open filen");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
}
File-File operations
#include <stdio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
fptr = fopen ("emp.txt", "w"); /*open for writing*/
if (fptr == NULL)
{
printf("File does not exists n");
return;
}
printf("Enter the namen");
C programs cs567
scanf("%s", name);
fprintf(fptr, "Name = %sn", name);
printf("Enter the agen");
scanf("%d", &age);
fprintf(fptr, "Age = %dn", age);
printf("Enter the salaryn");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2fn", salary);
fclose(fptr);
}
Find The Roots Of A Quadratic Equation
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float a, b, c, d, realp, imgp, r1, r2;
clrscr();
printf(" Enter the 3 numbersn ");
scanf(" %f %f %f " ,&a, &b, &c);
if ( a == 0 || b == 0 || c == 0 )
{
printf(" Error input only non zero numbersn ");
}
else
{
d = b * b - 4 * a * c;
if ( d == 0 )
{
printf(" Roots are equaln ");
r1 = r2 = - b / ( 2 * a );
printf(" Root1 = %f, Root2 = %f ", r1, r2 );
}
else if(d>0)
{
printf( "Roots are real & distinctn" );
r1 = ( - b + sqrt ( fabs ( d ) ) ) / ( 2 * a );
r2 = ( - b - sqrt ( fabs ( d ) ) ) / ( 2 * a );
printf(" Root1 = %f, Root2 = %f", r1, r2);
C programs cs567
}
else
{
printf(" Roots are imaginaryn ");
realp = - b / ( 2 * a );
imgp = sqrt ( fabs ( d ) ) / ( 2 * a );
printf(" Root1 = %f + i%f, Root2 = %f - i%f ",realp, imgp, realp, imgp);
}
}
getch();
}
Find the value of cos(x)
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float acc, term, den, x, cosx=0, cosval;
clrscr();
printf("Enter the value of x (in degrees)n");
scanf("%f",&x);
x1 = x;
/* Converting degrees to radians*/
x = x*(3.142/180.0);
cosval = cos(x);
printf("Enter the accuary for the resultn");
scanf("%f", &acc);
term = 1;
cosx = term;
n = 1;
do
{
den = 2*n*(2*n-1);
term = -term * x * x / den;
cosx = cosx + term;
n = n + 1;
C programs cs567
} while(acc <= fabs(cosval - cosx));
printf("Sum of the cosine series = %fn", cosx);
printf("Using Library function cos(%d) = %fn", x1,cos(x));
}
Find the value of sin(x)
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float acc, term, den, x, sinx=0, sinval;
clrscr();
printf("Enter the value of x (in degrees)n");
scanf("%f",&x);
x1 = x;
/* Converting degrees to radians*/
x = x*(3.142/180.0);
sinval = sin(x);
printf("Enter the accuary for the resultn");
scanf("%f", &acc);
term = x;
sinx = term;
n = 1;
do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;
} while(acc <= fabs(sinval - sinx));
printf("Sum of the sine series = %fn", sinx);
printf("Using Library function sin(%d) = %fn", x1,sin(x));
}
C programs cs567
Floyd Triangle
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print:n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("n");
}
return 0;
}
GCD LCM Using Euclids Algorithm
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr();
printf("Enter two numbersn");
scanf("%d %d", &num1,&num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
C programs cs567
remainder = num1 % num2;
while(remainder !=0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d n", num1,num2,gcd);
printf("LCM of %d and %d = %d n", num1,num2,lcm);
}
Heap Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int b[10],no,i,j,c,p,temp;
clrscr();
printf("nn Enter no of elements..");
scanf("%d",&no);
printf(" Enter the nos..");
for(i=0;i<no;i++)
scanf("%d",&b[i]);
for(i=1;i<no;i++)
{
c=i;
do
{
p=(c-1)/2;
if(b[p]<b[c])
{
temp=b[p];
b[p]=b[c];
b[c]=temp;
}
c=p;
} while(c!=0);
C programs cs567
}
for(j=no-1;j>=0;j--)
{
temp=b[0];
b[0]=b[j];
b[j]=temp;
p=0;
do
{
c=2*p+1;
if((b[c]<b[c+1]) && c<j-1)
c++;
if(b[p]<b[c] && c<j)
{
temp=b[p];
b[p]=b[c];
b[c]=temp;
}
p=c;
}while(c<j);
}
printf(" The sorted nos are..");
for(i=0;i<no;i++)
printf("%d",b[i]);
getch();
}
Insertion Sort
#include<stdio.h>
void main()
{
int A[20], N, Temp, i, j;
clrscr();
printf("ENTER THE NUMBER OF TERMS...: ");
scanf("%d", &N);
printf("n ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=0; i<N; i++)
{
gotoxy(25,11+i);
C programs cs567
scanf("ntt%d",&A[i]);
}
for(i=1; i<N; i++)
{
Temp = A[i];
j = i-1;
while(Temp<A[j] && j>=0)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("nTHE ASCENDING ORDER LIST IS...:n");
for(i=0; i<N; i++)
printf("n%d", A[i]);
getch();
}
Interpolation Search
#include <stdio.h>
#include <stdlib.h>
#define MAX 200
int interpolation_search(int a[], int bottom, int top, int item)
{
int mid;
while (bottom <= top)
{
mid = bottom + (top - bottom)* ((item - a[bottom]) / (a[top] - a[bottom]));
if (item == a[mid])
return mid + 1;
if (item < a[mid])
top = mid - 1;
else
bottom = mid + 1;
}
return -1;
}
int main()
C programs cs567
{
int arr[MAX];
int i, num;
int item, pos;
printf("nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("Enter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("n ELEMENTS AREn: ");
for (i = 0; i < num; i++)
printf("%d ", arr[i]);
printf("n Search For : ");
scanf("%d", &item);
pos = interpolation_search(&arr[0], 0, num, item);
if (pos == -1)
printf("n Element %d not found n", item);
else
printf("n Element %d found at position %d n", item, pos);
return 0;
}
Inverse of the Matrix
#include<stdio.h>
#include<math.h>
float detrminant(float[][], float);
void cofactors(float[][], float);
void trans(float[][], float[][], float);
main()
{
float a[25][25], n, d;
int i, j;
printf("Enter the order of the matrix:n");
scanf("%f", &n);
printf("Enter the elemnts into the matrix:n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
C programs cs567
scanf("%f", &a[i][j]);
}
}
d = detrminant(a, n);
printf("nTHE DETERMINANT IS=%2f", d);
if (d == 0)
printf("nMATRIX IS NOT INVERSIBLEn");
else
cofactors(a, n);
}
float detrminant(float a[25][25], float k)
{
float s = 1, det = 0, b[25][25];
int i, j, m, n, c;
if (k == 1)
{
return (a[0][0]);
}
else
{
det = 0;
for (c = 0; c < k; c++)
{
m = 0;
n = 0;
for (i = 0; i < k; i++)
{
for (j = 0; j < k; j++)
{
b[i][j] = 0;
if (i != 0 && j != c)
{
b[m][n] = a[i][j];
if (n < (k - 2))
n++;
else
{
n = 0;
m++;
}
}
C programs cs567
}
}
det = det + s * (a[0][c] * detrminant(b, k - 1));
s = -1 * s;
}
}
return (det);
}
void cofactors(float num[25][25], float f)
{
float b[25][25], fac[25][25];
int p, q, m, n, i, j;
for (q = 0; q < f; q++)
{
for (p = 0; p < f; p++)
{
m = 0;
n = 0;
for (i = 0; i < f; i++)
{
for (j = 0; j < f; j++)
{
b[i][j] = 0;
if (i != q && j != p)
{
b[m][n] = num[i][j];
if (n < (f - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
fac[q][p] = pow(-1, q + p) * detrminant(b, f - 1);
}
}
trans(num, fac, f);
}
C programs cs567
void trans(float num[25][25], float fac[25][25], float r)
{
int i, j;
float b[25][25], inv[25][25], d;
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
b[i][j] = fac[j][i];
}
}
d = detrminant(num, r);
inv[i][j] = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
inv[i][j] = b[i][j] / d;
}
}
printf("nTHE INVERSE OF THE MATRIX:n");
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
printf(" %2f", inv[i][j]);
}
printf("n");
}
}
Largest Among n digit
#include<stdio.h>
#include<conio.h>
void main()
{
int max_num(int a[],int n);
C programs cs567
int max,i,n;
int a[50];
clrscr();
printf("Enter n number:");
scanf("%d",&n);
printf("Enter the numbers:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=max_num(a,n);
printf("The largest number is %d",max);
getch();
}
int max_num(int a[],int n)
{
int i,m=0;
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
return m;
}
LeapYear
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap yearn");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.n", year);
else
printf("%d is not a leap year.n", year);
return 0;
}
C programs cs567
Linear Search
#include<stdio.h>
main()
{
int array[100], search, c, number;
printf("Enter the number of elements in arrayn");
scanf("%d",&number);
printf("Enter %d numbers n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to searchn");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.n", search);
return 0;
}
Malloc Example
#include<stdio.h>
int main()
{
int *ptr_one;
ptr_one = (int*)malloc(sizeof(int));
if (ptr_one == 0)
{
printf("ERROR: Out of memoryn");
return 1;
}
*ptr_one = 25;
printf("%d", *ptr_one);
C programs cs567
free(ptr_one);
return 0;
}
Matrix- Matrix Multiplication
#include <stdio.h>
main()
{
int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of first matrix (less than 10)n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix (less than 10)n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter rows and columns of First matrix n");
printf("Row wisen");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
printf("First Matrix is :n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d ",m1[i][j]);
printf("n");
}
printf("Enter rows and columns of Second matrix n");
printf("Row wisen");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
printf("Second Matrix is:n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d ",m2[i][j]);
printf("n");
}
printf("Multiplication of the Matrices:n");
for(i=0;i<r1;i++)
C programs cs567
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
mult[i][j]+=m1[i][k]*m2[k][j];
printf("%d ",mult[i][j]);
}
printf("n");
}
}
else
{
printf("Matrix multiplication cannot be done");
}
return 0;
}
Merge Sort
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main()
{
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++)
{
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",merge[i]);
C programs cs567
}
return 0;
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high)
{
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high))
{
if(arr[l]<=arr[m])
{
temp[i]=arr[l];
l++;
}
else
{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=arr[k];
i++;
C programs cs567
}
}
else
{
for(k=l;k<=mid;k++)
{
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}
Number is Even Or Odd
#include<stdio.h>
{
int n;
printf("Enter an integern");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Evenn");
else
printf("Odd:");
return 0;
}
Palindrome
#include<stdio.h>
#include <string.h>
main()
{
char a[100], b[100];
printf("Enter the string to check if it is a palindromen");
gets(a);
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 )
C programs cs567
printf("Entered string is a palindrome.n");
else
printf("Entered string is not a palindrome.n");
return 0;
}
Pascal Triangle
#include<stdio.h>
long fact(int);
int main()
{
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++)
{
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("n");
}
return 0;
}
long fact(int num)
{
long f=1;
int i=1;
while(i<=num)
{
f=f*i;
i++;
}
return f;
}
C programs cs567
Perfect Number
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("Enter a number:");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
Power of Given Number
#include<stdio.h>
int main()
{
int pow,num,i=1;
long int sum=1;
printf("Enter a number: ");
scanf("%d",&num);
printf("nEnter power: ");
scanf("%d",&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf("n%d to the power %d is: %ld",num,pow,sum);
return 0;
}
C programs cs567
Prime Number
#include <stdio.h>
#include <conio.h>
Int main()
{
int i,j=2,ch=0;
clrscr();
printf("nENTER ANY NUMBER");
scanf("%d",& i);
while(j < =i/2)
{
if(i%j==0)
{
printf("%d IS NOT PRIME",i);
ch=1;
break;
}
else
{
j++;
}
}
if(ch==0)
{
printf("%d IS PRIME",i);
}
}
Print Semicolon without using a semicolon
#include <stdio.h>
int main(void)
{
//prints the character with ascii value 59, i.e., semicolon
if (printf("%cn", 59))
{
//prints semicolon
C programs cs567
}
return 0;
}
Quick Sort
#include <stdio.h>
#define MAXSIZE 500
void quickSort(int elements[], int maxsize);
void sort(int elements[], int left, int right);
int elements[MAXSIZE];
int main()
{
int i, maxsize;
printf("nHow many elements you want to sort: ");
scanf("%d",&maxsize);
printf("nEnter the values one by one: ");
for (i = 0; i < maxsize; i++)
{
printf ("nEnter element %i :",i);
scanf("%d",&elements[i]);
}
printf("n Array before sorting:n");
for (i = 0; i < maxsize; i++)
printf("[%i], ",elements[i]);
printf ("n");
quickSort(elements, maxsize);
printf("n Array after sorting:n");
for (i = 0; i < maxsize; i++)
printf("[%i], ", elements[i]);
}
void quickSort(int elements[], int maxsize)
{
sort(elements, 0, maxsize - 1);
}
void sort(int elements[], int left, int right)v
C programs cs567
{
int pivot, l, r;
l = left;
r = right;
pivot = elements[left];
while (left < right)
{
while ((elements[right] >= pivot) && (left < right))
right--;
if (left != right)
{
elements[left] = elements[right];
left++;
}
while ((elements[left] <= pivot) && (left < right))
left++;
if(left != right)
{
elements[right] = elements[left];
right--;
}
}
elements[left] = pivot;
pivot = left;
left = l;
right = r;
if (left < pivot)
sort(elements, left, pivot - 1);
if (right > pivot)
sort(elements, pivot + 1, right);
}
Radix Sort
#include <stdio.h>
#define MAX 100
#define SHOWPASS
void print(int *a, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%dt;", a[i]);
C programs cs567
}
void radix_sort(int *a, int n)
{
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++)
{
if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("nnPASS : ");
print(a, n);
#endif
}
}
int main()
{
int arr[MAX];
int i, num;
printf("nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("n Enter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("n ARRAY : ");
print(&arr[0], num);
radix_sort(&arr[0], num);
printf("nn SORTED : ");
print(&arr[0], num);
C programs cs567
return 0;
}
Random Number Generator
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int n, max, num, c;
printf("Enter the number of random numbers you want ");
scanf("%d",&n);
printf("Enter the maximum value of random number ");
scanf("%d",&max);
printf("%d random numbers from 0 to %d are :-n",n,max);
randomize();
for ( c = 1 ; c <= n ; c++ )
{
num = random(max);
printf("%dn",num);
}
getch();
return 0;
}
Recursive Binary Search
#include<stdio.h>
int main()
{
int a[10],i,n,m,c,l,u;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: " );
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
C programs cs567
scanf("%d",&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("Number is not found.");
else
printf("Number is found.");
return 0;
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else if(m<a[mid])
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
Reverse Number
#include <stdio.h> n main()
{
int n, reverse = 0;
printf("Enter a number to reverse:n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
C programs cs567
}
printf("Reverse of entered number is = %dn", reverse);
return 0;
}
Selection Sort
#include <stdio.h>
main()
{
int A[20], N, Temp, i, j;
printf(" ENTER THE NUMBER OF TERMS...: ");
scanf("%d",&N);
printf("n ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=1; i<=N; i++)
{
scanf("ntt%d", &A[i]);
}
for(i=1; i<=N-1; i++)
for(j=i+1; j<=N;j++)
if(A[i]>A[j])
{
Temp = A[i];
A[i] = A[j];
A[j] = Temp;
}
printf("THE ASCENDING ORDER LIST IS...:n");
for(i=1; i<=N; i++)
printf("n %d",A[i]);
}
Shell Sort
#include <stdio.h>
void shellsort(int A[],int max)
{
int stop,swap,limit,temp,k;
int x=(int)(max/2)-1;
while(x>0)
C programs cs567
{
stop=0;
limit=max-x;
while(stop==0)
{
swap=0;
for(k=0; kA[k+x])
{
temp=A[k];
A[k]=A[k+x];
A[k+x]=temp;
swap=k;
}
}
limit=swap-x;
if(swap==0)
stop=1;
}
x=(int)(x/2);
}
int main()
{
int i,ELEMENTS,X[100];
printf("Enter the number of elements to be sorted:");
scanf("%d",&ELEMENTS);
printf("Enter the elements to be sorted:n");
for(i = 0; i < ELEMENTS; i++ )
{
scanf("%d",&X[i]);
}
printf("Unsorted Array:n");
for(i=0;i < ELEMENTS;i++)
printf("%d ",X[i]);
shellsort(X,ELEMENTS);
printf("n SORTED ARRAYn");
for(i=0;i < ELEMENTS;i++)
printf("%d ",X[i]);
printf("n");
}
C programs cs567
Sort A Given Number Of Strings
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char str[4][10];
int i;
clrscr();
for( i = 0; i < 4; i++ )
{
printf(“ nEnter name %d: ”, i+1 );
scanf(“ %s ”, str[i] );
}
printf(“n Original Ordern”);
for(i = 0; i < 4; i++ )
printf(“ %st ”, str[i] );
for(i = 0; i < 4; i++ )
{
for(j = i + 1; j < 4; j++ )
if( strcmp(str[i], str[j]) > 0 )
{
strcpy( temp, str[i] );
strcpy( temp, str[i], str[j] );
strcpy( str[j], temp );
}
}
printf(“n Sorted Ordern”);
for( i = 0; i < 4; i++ )
printf(“ %st ”, str[i]);
getch();
}
String Compare
#include<stdio.h>
int stringCompare(char[],char[]);
int main()
{
char str1[100],str2[100];
int compare;
C programs cs567
printf("Enter first string: ");
scanf("%s",str1);
printf("Enter second string: ");
scanf("%s",str2);
compare = stringCompare(str1,str2);
if(compare == 1)
printf("Both strings are equal.");
else
printf("Both strings are not equal");
return 0;
}
int stringCompare(char str1[],char str2[])
{
int i=0,flag=0;
while(str1[i]!='u0000' && str2[i]!='u0000')
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
i++;
}
if (flag==0 && str1[i]=='u0000' && str2[i]=='u0000')
return 1;
else
return 0;
}
Strong Number
void strong_number()
{
int num,i,p,r,sum=0,save_num;
printf("n Enter a number");
scanf("%d",&num);
save_num=num;
while(num)
{
i=1,p=1;
r=num%10;
while(i<=r)
C programs cs567
{
p=p*i;
i++;
} //while
sum=sum+p;
num=num/10;
} //while
if(sum==save_num)
printf("%d is a Strong number", save_num);
else
printf("%d is not a Strong number", save_num);
}
Subtract without subtract operator
#include<stdio.h>
int main()
{
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
return 0;
}
Sum of Digit
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,num,x,sum=0;
printf("Enter a number=");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
C programs cs567
}
printf("Sum of digits of a number=%d",sum);
getch();
}
Swap two numbers using bitwise operators
#include <stdio.h>
int main()
{
int i = 65;
int k = 120;
printf("n value of i=%d k=%d before swapping", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("n value of i=%d k=%d after swapping", i, k);
return 0;
}
Swap String
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
main()
{
char first[100], second[100], *temp;
printf("Enter the first string ");
gets(first);
printf("Enter the second string ");
gets(second);
printf("nBefore Swappingn");
printf("First string: %sn",first);
printf("Second string: %snn",second);
temp = (char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("After Swappingn");
printf("First string: %sn",first);
C programs cs567
printf("Second string: %sn",second);
getch();
return 0;
}
Swap Two Number
#include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and yn");
scanf("%d", &x, &y);
printf("Before Swappingn x = %dny = %dn",x,y);
temp = x;
x = y;
y = temp;
printf("After Swappingn x = %dny = %dn",x,y);
return 0;
}
Swap Without using third variable
#include <stdio.h>
void main()
{
int a,b;
printf("Enter number1: ie a");
scanf("%d",&a);
printf("Enter number2:ie b ");
scanf("%d",&b);
printf(value of a and b before swapping is a=%d,b=%d"a,b);
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swapping is a=%d,b=%d"a,b);
}
Topological Sort
#include<stdio.h>
#define MAX 200
int n,adj[MAX][MAX];
C programs cs567
int front = -1,rear = -1,queue[MAX];
void main()
{
int i,j = 0,k;
int topsort[MAX],indeg[MAX];
create_graph();
printf(“The adjacency matrix is:n”);
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;ilt;=n;i++)
{
if(adj[k][i]==1)
{
adj[k][i]=0;
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
printf("Nodes after topological sorting are:n");
for(i=0;i<=n;i++)
printf("%d",topsort[i]);
printf("n");
}
create_graph()
{
int i,max_edges,origin,destin;
printf("n Enter number of vertices:");
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
C programs cs567
{
printf("n Enter edge %d (00 to quit):",i);
scanf("%d %d",&origin,&destin);
if((origin == 0) && (destin == 0))
{
printf("Invalid edge!!n");
i–;
}
else
adj[origin][destin] = 1;
}
return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
printf(“Queue Underflow”);
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
if(adj[i][node] == 1)
in_deg++;
return in_deg;
}
}
C programs cs567
Transpose A Matrix
#include <stdio.h>
#include <conio.h>
int main()
{
int m, n, i, j;
int mat[10][10], trans[10][10];
printf(" Enter the number of rows and columns of matrix ");
scanf(" %d %d ", &m, &n);
printf(" Enter the elements of matrix n ");
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
scanf(" %d ", &mat[i][j] );
}
}
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
trans[j][i] = mat[i][j];
}
}
printf(" Transpose of entered matrix :-n ");
for( i = 0 ; i < n ; i++ )
{
for( j = 0 ; j < m ; j++ )
{
printf(" %dt ", trans[i][j] );
}
printf(" n ");
}
getch();
return 0;
}
C programs cs567

More Related Content

DOC
C basics
PDF
C Programming lab
DOCX
Practical File of C Language
DOCX
DataStructures notes
DOCX
DOCX
C lab manaual
PDF
c-programming-using-pointers
C basics
C Programming lab
Practical File of C Language
DataStructures notes
C lab manaual
c-programming-using-pointers

What's hot (19)

DOC
Basic c programs updated on 31.8.2020
PDF
Data Structure using C
DOCX
SaraPIC
PDF
C programs
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
DOCX
C Programming
PDF
C Prog. - Structures
DOCX
Practical write a c program to reverse a given number
PDF
C Prog - Strings
PDF
C lab programs
PDF
C Prog. - Strings (Updated)
DOCX
Practical write a c program to reverse a given number
DOCX
Data Structures Using C Practical File
DOCX
C programs
DOCX
Chapter 8 c solution
PDF
The solution manual of c by robin
PPT
All important c programby makhan kumbhkar
PDF
88 c-programs
PPTX
Double linked list
Basic c programs updated on 31.8.2020
Data Structure using C
SaraPIC
C programs
Let us C (by yashvant Kanetkar) chapter 3 Solution
C Programming
C Prog. - Structures
Practical write a c program to reverse a given number
C Prog - Strings
C lab programs
C Prog. - Strings (Updated)
Practical write a c program to reverse a given number
Data Structures Using C Practical File
C programs
Chapter 8 c solution
The solution manual of c by robin
All important c programby makhan kumbhkar
88 c-programs
Double linked list
Ad

Similar to C programms (20)

DOCX
PPTX
Mcs011 solved assignment by divya singh
PDF
The solution manual of programming in ansi by Robin
PPTX
Programming in C Basics
PPT
Cbasic
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
PPTX
DOCX
Data structures
PDF
Fundamentals C programming and strong your skills.
PDF
C Programming Project
PDF
Programming in C Lab
PDF
C lab programs
PDF
C programming Assignments and Questions.pdf
DOC
C-programs
PPTX
Programming in C
DOCX
B.Com 1year Lab programs
Mcs011 solved assignment by divya singh
The solution manual of programming in ansi by Robin
Programming in C Basics
Cbasic
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
Data structures
Fundamentals C programming and strong your skills.
C Programming Project
Programming in C Lab
C lab programs
C programming Assignments and Questions.pdf
C-programs
Programming in C
B.Com 1year Lab programs
Ad

More from Mukund Gandrakota (12)

PDF
Edc unit 8
PDF
Edc unit 7
PDF
Edc unit 6
PDF
Edc unit 5
PDF
Edc unit 4
PDF
Edc unit 3
PDF
Edc unit 2
PDF
Edc unit 1
PDF
Java script programms
PDF
Java programs
PDF
C++ programs
PDF
career after graduated in cse
Edc unit 8
Edc unit 7
Edc unit 6
Edc unit 5
Edc unit 4
Edc unit 3
Edc unit 2
Edc unit 1
Java script programms
Java programs
C++ programs
career after graduated in cse

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
composite construction of structures.pdf
PPTX
Welding lecture in detail for understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
OOP with Java - Java Introduction (Basics)
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
R24 SURVEYING LAB MANUAL for civil enggi
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
bas. eng. economics group 4 presentation 1.pptx
Sustainable Sites - Green Building Construction
Internet of Things (IOT) - A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Lecture Notes Electrical Wiring System Components
composite construction of structures.pdf
Welding lecture in detail for understanding

C programms

  • 1. C programs cs567 Add Complex Numbers #include <stdio.h> #include <conio.h> #include <stdlib.h> struct complex { int real; int img; }; int main() { struct complex a, b, c; printf(" Enter a and b where a + ib is the first complex number. "); printf("n a = "); scanf(" %d ", &a.real); printf(" b = "); scanf(" %d ", &a.img); printf(" Enter c and d where c + id is the second complex number. "); printf("n c = "); scanf(" %d ", &b.real); printf(" d = "); scanf(" %d ", &b.img); c.real = a.real + b.real; c.img = a.img + b.img; if ( c.img >= 0 ) printf(" Sum of two complex numbers = %d + %di ", c.real, c.img); else printf(" Sum of two complex numbers = %d %di ", c.real, c.img); getch(); return 0; } Add without ADD Operator #include<stdio.h> int main() { int a,b; int sum; printf("Enter any two integers: "); scanf("%d%d",&a,&b);
  • 2. C programs cs567 //sum = a - (-b); sum = a - ~b -1; printf("Sum of two integers:%d",sum); return 0; } Armstrong Number void main() { int n,b=0,t; clrscr(); printf("Enter the no"); scanf("%d",&n); t=n; while(n>0) { a=n%10; b=b+a*a*a; n=n/10; } if(b==t) { printf("Armstrong no"); } else { printf("Not an armstrong no"); } getch(); } Binary Search int BinarySearch(int *array, int number_of_elements, int key) { int low = 0, high = number_of_elements-1, mid; while(low <= high) { mid = (low + high)/2;
  • 3. C programs cs567 if(array[mid] < key) { low = mid + 1; } else if(array[mid] == key) { return mid; } else if(array[mid] > key) { high = mid-1; } } return -1; } int main() { int number_of_elements; scanf("%d",&number_of_elements); int array[number_of_elements]; int iter; for(iter = 1;iter < number_of_elements;iter++) { if(array[iter]< array[iter - 1]) { printf("Given input is n not sortedn"); return 0; } } int key; scanf("%d",&key); /* Calling this functions searches for the key and returns its index. It returns -1 if key is not found.*/ int index; index = BinarySearch(array,number_of_elements,key); if(index==-1) { printf("Element not foundn"); } else {
  • 4. C programs cs567 printf("Element is at index %dn ",index); } return 0; } Bubble Sort #include <stdio.h> void bubble_sort(long [], long); int main() { long array[100], n, c, d, swap; printf("Enter number of elements:"); scanf("%ld", &n); printf("Enter %ld longegersn", n); for (c = 0; c < n; c++) scanf("%ld", &array[c]); bubble_sort(array, n); printf("Sorted list in ascending order:n"); for ( c = 0 ; c < n ; c++ ) printf("%ldn", array[c]); return 0; } void bubble_sort(long list[], long n) { long c, d, t; for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (list[d] > list[d+1]) { t = list[d]; list[d] = list[d+1]; list[d+1]= t; } } } }
  • 5. C programs cs567 Bucket Sort #include<stdio.h> void Bucket_Sort(int array[], int n) { int i, j; int count[n]; for(i=0; i < n; i++) { count[i] = 0; } for(i=0; i < n; i++) { (count[array[i]])++; } for(i=0,j=0; i < n; i++) { for(; count[i]>0;(count[i])--) { array[j++] = i; } } } int main() { int array[100]; int num; int i; printf("Enter How many Numbers : "); scanf("%d",&num); printf("Enter the %d elements to be sorted:n",num); for(i = 0; i < num; i++ ) { scanf("%d",&array[i]); } printf("n The array of elements before sorting : n"); for (i = 0;i < num;i++) { printf("%d ", array[i]); } printf("n The array of elements after sorting : n"); Bucket_Sort(array, num); for (i = 0;i < n;i++)
  • 6. C programs cs567 { printf("%d ", array[i]); } printf("n"); return 0; } Celsius To Fahrenheit #include <stdio.h> #include <conio.h> void main() { float c, f; clrscr(); printf(" Enter temp in centigrade: "); scanf("%f",&c); f = ( 1.8 * c ) + 32; printf(" Temperature in Fahrenheit = %f", f); getch(); } Character is Vowel or not #include <stdio.h> main() { char ch; printf("Enter a charactern"); scanf("%c", &ch); if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') printf("%c is a vowel.n", ch); else printf("%c is not a vowel.n", ch); return 0; } Combinations and Permutations
  • 7. C programs cs567 #include <stdio.h> #include <conio.h> main() { int n , r, ncr( int , int); long npr( int , int); long double fact( int); printf(" Enter value of n & r n"); scanf("%d %d",&n , &r); if( n>= r) { printf( "%d C %d is %d n", n,r,ncr( n , r)); printf("%d P %d is %ld", n,r,npr( n, r)); } else { printf("WRONG INPUT?? enter the correct input"); } } long double fact( int p) { long double facts = 1; int i; for( i = 1; i<= p; i++) facts = facts * i; return( facts); } int ncr ( int n, int r) { return( fact( n) / (fact( r) * fact(n- r) ) ) ; } long npr( int n , int r) { return( fact( n) / fact( n- r)); } Convert Binary to Decimal,Octal,Hexadecimal #include<stdio.h> #include<string.h> void hexadecimal(); void main() { int num, bnum, dec = 0, base = 1, rem ,dec1=0,oct[25],dec2=0,flag=0,i=0,counter=0,j; printf("Enter the binary number(1s and 0s)n");
  • 8. C programs cs567 scanf("%d", &num); bnum = num; while( num > 0) { rem = num % 10; if((rem==0) || (rem==1)) { dec = dec + rem * base; num = num / 10 ; base = base * 2; flag=1; } else { flag=0; printf("n Enter binary number n"); break; } } if(flag==1) { printf("The Binary number is = %dn", bnum); printf("Its decimal equivalent is =%dn", dec); dec1=dec; dec2=dec1; while(dec>0) { rem=dec%8; oct[i]=rem; dec=dec/8; i++; counter++; } counter--; printf("n Its octal equivalent is:"); while(counter>=0) { printf("%d" ,oct[counter]); counter--; } printf("nIts Hexa Decimal equivalant is: "); hexadecimal(dec2); } } void hexadecimal(long n) { long i;
  • 9. C programs cs567 if(n>0) { i=n%16; n=n/16; hexadecimal(n); if(i>=10) { switch(i) { case 10: printf("A"); break; case 11: printf("B"); break; case 12: printf("C"); break; case 13: printf("D"); break; case 14: printf("E"); break; case 15: printf("F"); break; } } else printf("%ld",i); } } Copy the String #include<stdio.h>
  • 10. C programs cs567 void stringCopy(char[],char[]); int main() { char str1[100],str2[100]; printf("Enter any string: "); scanf("%s",str1); stringCopy(str1,str2); printf("After copying: %s",str2); return 0; } void stringCopy(char str1[],char str2[]) { int i=0; while(str1[i]!='u0000') { str2[i] = str1[i]; i++; } str2[i]='u0000'; }
  • 11. C programs cs567 Counting Frequencies Of Elements Of Array #include <stdio.h> #include <conio.h> #include <stdlib.h> #define S 6 main() { int a[S], freq[S]; int i, j, k,n = S; clrscr(); for(i = 0; i < S; i++) { printf(" n Enter a[%d] element: ", i); scanf(" %d ", &a[i]); freq[i] = 1; } printf(" Original Arrayn "); for(i = 0; i < S; i++) printf(" %d ", a[i]); /* Main Logic Starts Here */ for(i = 0; i < n; i++) for(j = i + 1; j < n; j++) { if(a[i] == a[j]) { for(k = j; k < n; k++) a[k] = a[k+1]; freq[i]++; n--; } } printf(" nArray with freqn "); printf(" nElement Freqn "); for(i = 0; i < n; i++) printf("%d %dn ", a[i], freq[i]); getch(); }
  • 12. C programs cs567 Count the Digit in a Number #include<stdio.h> int main() { int num,count=0; printf("Enter a number: "); scanf("%d",&num); while(num) { num=num/10; count++; } printf("Total digits is:%d",count); return 0; } Factorial #include <stdio.h> #include <conio.h> long int factorial(int n); void main() { int n; clrscr(); printf("Enter the number:n"); scanf("%d",&n); printf("Factorial of %d is %ld",n,factorial(n)); getch(); } long int factorial(int n) { if(n<=1) { return(01); } else { n=n*factorial(n-1); return(n);
  • 13. C programs cs567 } } Fibonacci Series #include <stdio.h> #include <conio.h> void main() { int a,b,c,i,n; clrscr(); a=0; b=1; printf("n Enter n for how many times generate series"); scanf("%d",&n); printf("n FIBONACCI SERIES n"); printf("t%dt%d",a,b); for(i=0;i<n;i++) { c=a+b; a=b; b=c; printf("t%d",c); } getch(); } File-Copy one file contents to another #include <stdio.h> #include <conio.h> #include <process.h> void main(int argc, char *argv[]) { FILE *fs,*ft; char ch; clrscr(); if(argc!=3) { puts("Invalid number of arguments."); exit(0);
  • 14. C programs cs567 } fs = fopen(argv[1],"r"); if(fs==NULL) { puts("Source file cannot be opened."); exit(0); } ft = fopen(argv[2],"w"); if (ft==NULL) { puts("Target file cannot be opened."); fclose(fs); exit(0); } while(1) { ch=fgetc(fs); if (ch==EOF) break; else fputc(ch,ft); } fclose(fs); fclose(ft); getch(); } File Example #include<stdio.h> int main() { FILE *fp; char ch; fp=fopen("file.txt","w"); printf("nEnter data to be stored in to the file:"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); return 0; }
  • 15. C programs cs567 File-How the data stored on the disk is read #include <stdio.h> #include <stdlib.h> void main() { FILE *fptr; char filename[15]; char ch; printf("Enter the filename to be openedn"); gets(filename); fptr = fopen (filename, "r"); /*open for reading*/ if (fptr == NULL) { printf("Cannot open filen"); exit(0); } ch = fgetc(fptr); while (ch != EOF) { printf ("%c", ch); ch = fgetc(fptr); } fclose(fptr); } File-File operations #include <stdio.h> void main() { FILE *fptr; char name[20]; int age; float salary; fptr = fopen ("emp.txt", "w"); /*open for writing*/ if (fptr == NULL) { printf("File does not exists n"); return; } printf("Enter the namen");
  • 16. C programs cs567 scanf("%s", name); fprintf(fptr, "Name = %sn", name); printf("Enter the agen"); scanf("%d", &age); fprintf(fptr, "Age = %dn", age); printf("Enter the salaryn"); scanf("%f", &salary); fprintf(fptr, "Salary = %.2fn", salary); fclose(fptr); } Find The Roots Of A Quadratic Equation #include <stdio.h> #include <conio.h> #include <math.h> void main() { float a, b, c, d, realp, imgp, r1, r2; clrscr(); printf(" Enter the 3 numbersn "); scanf(" %f %f %f " ,&a, &b, &c); if ( a == 0 || b == 0 || c == 0 ) { printf(" Error input only non zero numbersn "); } else { d = b * b - 4 * a * c; if ( d == 0 ) { printf(" Roots are equaln "); r1 = r2 = - b / ( 2 * a ); printf(" Root1 = %f, Root2 = %f ", r1, r2 ); } else if(d>0) { printf( "Roots are real & distinctn" ); r1 = ( - b + sqrt ( fabs ( d ) ) ) / ( 2 * a ); r2 = ( - b - sqrt ( fabs ( d ) ) ) / ( 2 * a ); printf(" Root1 = %f, Root2 = %f", r1, r2);
  • 17. C programs cs567 } else { printf(" Roots are imaginaryn "); realp = - b / ( 2 * a ); imgp = sqrt ( fabs ( d ) ) / ( 2 * a ); printf(" Root1 = %f + i%f, Root2 = %f - i%f ",realp, imgp, realp, imgp); } } getch(); } Find the value of cos(x) #include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> void main() { int n, x1; float acc, term, den, x, cosx=0, cosval; clrscr(); printf("Enter the value of x (in degrees)n"); scanf("%f",&x); x1 = x; /* Converting degrees to radians*/ x = x*(3.142/180.0); cosval = cos(x); printf("Enter the accuary for the resultn"); scanf("%f", &acc); term = 1; cosx = term; n = 1; do { den = 2*n*(2*n-1); term = -term * x * x / den; cosx = cosx + term; n = n + 1;
  • 18. C programs cs567 } while(acc <= fabs(cosval - cosx)); printf("Sum of the cosine series = %fn", cosx); printf("Using Library function cos(%d) = %fn", x1,cos(x)); } Find the value of sin(x) #include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> void main() { int n, x1; float acc, term, den, x, sinx=0, sinval; clrscr(); printf("Enter the value of x (in degrees)n"); scanf("%f",&x); x1 = x; /* Converting degrees to radians*/ x = x*(3.142/180.0); sinval = sin(x); printf("Enter the accuary for the resultn"); scanf("%f", &acc); term = x; sinx = term; n = 1; do { den = 2*n*(2*n+1); term = -term * x * x / den; sinx = sinx + term; n = n + 1; } while(acc <= fabs(sinval - sinx)); printf("Sum of the sine series = %fn", sinx); printf("Using Library function sin(%d) = %fn", x1,sin(x)); }
  • 19. C programs cs567 Floyd Triangle #include <stdio.h> int main() { int n, i, c, a = 1; printf("Enter the number of rows of Floyd's triangle to print:n"); scanf("%d", &n); for (i = 1; i <= n; i++) { for (c = 1; c <= i; c++) { printf("%d ",a); a++; } printf("n"); } return 0; } GCD LCM Using Euclids Algorithm #include <stdio.h> #include <conio.h> void main() { int num1, num2, gcd, lcm, remainder, numerator, denominator; clrscr(); printf("Enter two numbersn"); scanf("%d %d", &num1,&num2); if (num1 > num2) { numerator = num1; denominator = num2; } else { numerator = num2; denominator = num1; }
  • 20. C programs cs567 remainder = num1 % num2; while(remainder !=0) { numerator = denominator; denominator = remainder; remainder = numerator % denominator; } gcd = denominator; lcm = num1 * num2 / gcd; printf("GCD of %d and %d = %d n", num1,num2,gcd); printf("LCM of %d and %d = %d n", num1,num2,lcm); } Heap Sort #include<stdio.h> #include<conio.h> void main() { int b[10],no,i,j,c,p,temp; clrscr(); printf("nn Enter no of elements.."); scanf("%d",&no); printf(" Enter the nos.."); for(i=0;i<no;i++) scanf("%d",&b[i]); for(i=1;i<no;i++) { c=i; do { p=(c-1)/2; if(b[p]<b[c]) { temp=b[p]; b[p]=b[c]; b[c]=temp; } c=p; } while(c!=0);
  • 21. C programs cs567 } for(j=no-1;j>=0;j--) { temp=b[0]; b[0]=b[j]; b[j]=temp; p=0; do { c=2*p+1; if((b[c]<b[c+1]) && c<j-1) c++; if(b[p]<b[c] && c<j) { temp=b[p]; b[p]=b[c]; b[c]=temp; } p=c; }while(c<j); } printf(" The sorted nos are.."); for(i=0;i<no;i++) printf("%d",b[i]); getch(); } Insertion Sort #include<stdio.h> void main() { int A[20], N, Temp, i, j; clrscr(); printf("ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("n ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { gotoxy(25,11+i);
  • 22. C programs cs567 scanf("ntt%d",&A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp<A[j] && j>=0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("nTHE ASCENDING ORDER LIST IS...:n"); for(i=0; i<N; i++) printf("n%d", A[i]); getch(); } Interpolation Search #include <stdio.h> #include <stdlib.h> #define MAX 200 int interpolation_search(int a[], int bottom, int top, int item) { int mid; while (bottom <= top) { mid = bottom + (top - bottom)* ((item - a[bottom]) / (a[top] - a[bottom])); if (item == a[mid]) return mid + 1; if (item < a[mid]) top = mid - 1; else bottom = mid + 1; } return -1; } int main()
  • 23. C programs cs567 { int arr[MAX]; int i, num; int item, pos; printf("nEnter total elements (num < %d) : ", MAX); scanf("%d", &num); printf("Enter %d Elements : ", num); for (i = 0; i < num; i++) scanf("%d", &arr[i]); printf("n ELEMENTS AREn: "); for (i = 0; i < num; i++) printf("%d ", arr[i]); printf("n Search For : "); scanf("%d", &item); pos = interpolation_search(&arr[0], 0, num, item); if (pos == -1) printf("n Element %d not found n", item); else printf("n Element %d found at position %d n", item, pos); return 0; } Inverse of the Matrix #include<stdio.h> #include<math.h> float detrminant(float[][], float); void cofactors(float[][], float); void trans(float[][], float[][], float); main() { float a[25][25], n, d; int i, j; printf("Enter the order of the matrix:n"); scanf("%f", &n); printf("Enter the elemnts into the matrix:n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) {
  • 24. C programs cs567 scanf("%f", &a[i][j]); } } d = detrminant(a, n); printf("nTHE DETERMINANT IS=%2f", d); if (d == 0) printf("nMATRIX IS NOT INVERSIBLEn"); else cofactors(a, n); } float detrminant(float a[25][25], float k) { float s = 1, det = 0, b[25][25]; int i, j, m, n, c; if (k == 1) { return (a[0][0]); } else { det = 0; for (c = 0; c < k; c++) { m = 0; n = 0; for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { b[i][j] = 0; if (i != 0 && j != c) { b[m][n] = a[i][j]; if (n < (k - 2)) n++; else { n = 0; m++; } }
  • 25. C programs cs567 } } det = det + s * (a[0][c] * detrminant(b, k - 1)); s = -1 * s; } } return (det); } void cofactors(float num[25][25], float f) { float b[25][25], fac[25][25]; int p, q, m, n, i, j; for (q = 0; q < f; q++) { for (p = 0; p < f; p++) { m = 0; n = 0; for (i = 0; i < f; i++) { for (j = 0; j < f; j++) { b[i][j] = 0; if (i != q && j != p) { b[m][n] = num[i][j]; if (n < (f - 2)) n++; else { n = 0; m++; } } } } fac[q][p] = pow(-1, q + p) * detrminant(b, f - 1); } } trans(num, fac, f); }
  • 26. C programs cs567 void trans(float num[25][25], float fac[25][25], float r) { int i, j; float b[25][25], inv[25][25], d; for (i = 0; i < r; i++) { for (j = 0; j < r; j++) { b[i][j] = fac[j][i]; } } d = detrminant(num, r); inv[i][j] = 0; for (i = 0; i < r; i++) { for (j = 0; j < r; j++) { inv[i][j] = b[i][j] / d; } } printf("nTHE INVERSE OF THE MATRIX:n"); for (i = 0; i < r; i++) { for (j = 0; j < r; j++) { printf(" %2f", inv[i][j]); } printf("n"); } } Largest Among n digit #include<stdio.h> #include<conio.h> void main() { int max_num(int a[],int n);
  • 27. C programs cs567 int max,i,n; int a[50]; clrscr(); printf("Enter n number:"); scanf("%d",&n); printf("Enter the numbers:"); for(i=0;i<n;i++) scanf("%d",&a[i]); max=max_num(a,n); printf("The largest number is %d",max); getch(); } int max_num(int a[],int n) { int i,m=0; for(i=0;i<n;i++) { if(a[i]>m) m=a[i]; } return m; } LeapYear #include <stdio.h> int main() { int year; printf("Enter a year to check if it is a leap yearn"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.n", year); else if ( year%100 == 0) printf("%d is not a leap year.n", year); else if ( year%4 == 0 ) printf("%d is a leap year.n", year); else printf("%d is not a leap year.n", year); return 0; }
  • 28. C programs cs567 Linear Search #include<stdio.h> main() { int array[100], search, c, number; printf("Enter the number of elements in arrayn"); scanf("%d",&number); printf("Enter %d numbers n", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to searchn"); scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) { if ( array[c] == search ) /* if required element found */ { printf("%d is present at location %d.n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.n", search); return 0; } Malloc Example #include<stdio.h> int main() { int *ptr_one; ptr_one = (int*)malloc(sizeof(int)); if (ptr_one == 0) { printf("ERROR: Out of memoryn"); return 1; } *ptr_one = 25; printf("%d", *ptr_one);
  • 29. C programs cs567 free(ptr_one); return 0; } Matrix- Matrix Multiplication #include <stdio.h> main() { int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2; printf("Enter number of rows and columns of first matrix (less than 10)n"); scanf("%d%d",&r1,&c1); printf("Enter number of rows and columns of second matrix (less than 10)n"); scanf("%d%d",&r2,&c2); if(r2==c1) { printf("Enter rows and columns of First matrix n"); printf("Row wisen"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&m1[i][j]); printf("First Matrix is :n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d ",m1[i][j]); printf("n"); } printf("Enter rows and columns of Second matrix n"); printf("Row wisen"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); printf("Second Matrix is:n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d ",m2[i][j]); printf("n"); } printf("Multiplication of the Matrices:n"); for(i=0;i<r1;i++)
  • 30. C programs cs567 { for(j=0;j<c2;j++) { mult[i][j]=0; for(k=0;k<r1;k++) mult[i][j]+=m1[i][k]*m2[k][j]; printf("%d ",mult[i][j]); } printf("n"); } } else { printf("Matrix multiplication cannot be done"); } return 0; } Merge Sort #include<stdio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main() { int merge[MAX],i,n; printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++) { scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++) { printf("%d ",merge[i]);
  • 31. C programs cs567 } return 0; } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); } } void mergeSort(int arr[],int low,int mid,int high) { int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)) { if(arr[l]<=arr[m]) { temp[i]=arr[l]; l++; } else { temp[i]=arr[m]; m++; } i++; } if(l>mid) { for(k=m;k<=high;k++) { temp[i]=arr[k]; i++;
  • 32. C programs cs567 } } else { for(k=l;k<=mid;k++) { temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++) { arr[k]=temp[k]; } } Number is Even Or Odd #include<stdio.h> { int n; printf("Enter an integern"); scanf("%d",&n); if ( n%2 == 0 ) printf("Evenn"); else printf("Odd:"); return 0; } Palindrome #include<stdio.h> #include <string.h> main() { char a[100], b[100]; printf("Enter the string to check if it is a palindromen"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 )
  • 33. C programs cs567 printf("Entered string is a palindrome.n"); else printf("Entered string is not a palindrome.n"); return 0; } Pascal Triangle #include<stdio.h> long fact(int); int main() { int line,i,j; printf("Enter the no. of lines: "); scanf("%d",&line); for(i=0;i<line;i++) { for(j=0;j<line-i-1;j++) printf(" "); for(j=0;j<=i;j++) printf("%ld ",fact(i)/(fact(j)*fact(i-j))); printf("n"); } return 0; } long fact(int num) { long f=1; int i=1; while(i<=num) { f=f*i; i++; } return f; }
  • 34. C programs cs567 Perfect Number #include<stdio.h> int main() { int n,i=1,sum=0; printf("Enter a number:"); scanf("%d",&n); while(i<n) { if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d is a perfect number",i); else printf("%d is not a perfect number",i); return 0; } Power of Given Number #include<stdio.h> int main() { int pow,num,i=1; long int sum=1; printf("Enter a number: "); scanf("%d",&num); printf("nEnter power: "); scanf("%d",&pow); while(i<=pow) { sum=sum*num; i++; } printf("n%d to the power %d is: %ld",num,pow,sum); return 0; }
  • 35. C programs cs567 Prime Number #include <stdio.h> #include <conio.h> Int main() { int i,j=2,ch=0; clrscr(); printf("nENTER ANY NUMBER"); scanf("%d",& i); while(j < =i/2) { if(i%j==0) { printf("%d IS NOT PRIME",i); ch=1; break; } else { j++; } } if(ch==0) { printf("%d IS PRIME",i); } } Print Semicolon without using a semicolon #include <stdio.h> int main(void) { //prints the character with ascii value 59, i.e., semicolon if (printf("%cn", 59)) { //prints semicolon
  • 36. C programs cs567 } return 0; } Quick Sort #include <stdio.h> #define MAXSIZE 500 void quickSort(int elements[], int maxsize); void sort(int elements[], int left, int right); int elements[MAXSIZE]; int main() { int i, maxsize; printf("nHow many elements you want to sort: "); scanf("%d",&maxsize); printf("nEnter the values one by one: "); for (i = 0; i < maxsize; i++) { printf ("nEnter element %i :",i); scanf("%d",&elements[i]); } printf("n Array before sorting:n"); for (i = 0; i < maxsize; i++) printf("[%i], ",elements[i]); printf ("n"); quickSort(elements, maxsize); printf("n Array after sorting:n"); for (i = 0; i < maxsize; i++) printf("[%i], ", elements[i]); } void quickSort(int elements[], int maxsize) { sort(elements, 0, maxsize - 1); } void sort(int elements[], int left, int right)v
  • 37. C programs cs567 { int pivot, l, r; l = left; r = right; pivot = elements[left]; while (left < right) { while ((elements[right] >= pivot) && (left < right)) right--; if (left != right) { elements[left] = elements[right]; left++; } while ((elements[left] <= pivot) && (left < right)) left++; if(left != right) { elements[right] = elements[left]; right--; } } elements[left] = pivot; pivot = left; left = l; right = r; if (left < pivot) sort(elements, left, pivot - 1); if (right > pivot) sort(elements, pivot + 1, right); } Radix Sort #include <stdio.h> #define MAX 100 #define SHOWPASS void print(int *a, int n) { int i; for (i = 0; i < n; i++) printf("%dt;", a[i]);
  • 38. C programs cs567 } void radix_sort(int *a, int n) { int i, b[MAX], m = 0, exp = 1; for (i = 0; i < n; i++) { if (a[i] > m) m = a[i]; } while (m / exp > 0) { int box[10] = { 0 }; for (i = 0; i < n; i++) box[a[i] / exp % 10]++; for (i = 1; i < 10; i++) box[i] += box[i - 1]; for (i = n - 1; i >= 0; i--) b[--box[a[i] / exp % 10]] = a[i]; for (i = 0; i < n; i++) a[i] = b[i]; exp *= 10; #ifdef SHOWPASS printf("nnPASS : "); print(a, n); #endif } } int main() { int arr[MAX]; int i, num; printf("nEnter total elements (num < %d) : ", MAX); scanf("%d", &num); printf("n Enter %d Elements : ", num); for (i = 0; i < num; i++) scanf("%d", &arr[i]); printf("n ARRAY : "); print(&arr[0], num); radix_sort(&arr[0], num); printf("nn SORTED : "); print(&arr[0], num);
  • 39. C programs cs567 return 0; } Random Number Generator #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int n, max, num, c; printf("Enter the number of random numbers you want "); scanf("%d",&n); printf("Enter the maximum value of random number "); scanf("%d",&max); printf("%d random numbers from 0 to %d are :-n",n,max); randomize(); for ( c = 1 ; c <= n ; c++ ) { num = random(max); printf("%dn",num); } getch(); return 0; } Recursive Binary Search #include<stdio.h> int main() { int a[10],i,n,m,c,l,u; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: " ); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter the number to be search: ");
  • 40. C programs cs567 scanf("%d",&m); l=0,u=n-1; c=binary(a,n,m,l,u); if(c==0) printf("Number is not found."); else printf("Number is found."); return 0; } int binary(int a[],int n,int m,int l,int u) { int mid,c=0; if(l<=u) { mid=(l+u)/2; if(m==a[mid]) { c=1; } else if(m<a[mid]) { return binary(a,n,m,l,mid-1); } else return binary(a,n,m,mid+1,u); } else return c; } Reverse Number #include <stdio.h> n main() { int n, reverse = 0; printf("Enter a number to reverse:n"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10;
  • 41. C programs cs567 } printf("Reverse of entered number is = %dn", reverse); return 0; } Selection Sort #include <stdio.h> main() { int A[20], N, Temp, i, j; printf(" ENTER THE NUMBER OF TERMS...: "); scanf("%d",&N); printf("n ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=1; i<=N; i++) { scanf("ntt%d", &A[i]); } for(i=1; i<=N-1; i++) for(j=i+1; j<=N;j++) if(A[i]>A[j]) { Temp = A[i]; A[i] = A[j]; A[j] = Temp; } printf("THE ASCENDING ORDER LIST IS...:n"); for(i=1; i<=N; i++) printf("n %d",A[i]); } Shell Sort #include <stdio.h> void shellsort(int A[],int max) { int stop,swap,limit,temp,k; int x=(int)(max/2)-1; while(x>0)
  • 42. C programs cs567 { stop=0; limit=max-x; while(stop==0) { swap=0; for(k=0; kA[k+x]) { temp=A[k]; A[k]=A[k+x]; A[k+x]=temp; swap=k; } } limit=swap-x; if(swap==0) stop=1; } x=(int)(x/2); } int main() { int i,ELEMENTS,X[100]; printf("Enter the number of elements to be sorted:"); scanf("%d",&ELEMENTS); printf("Enter the elements to be sorted:n"); for(i = 0; i < ELEMENTS; i++ ) { scanf("%d",&X[i]); } printf("Unsorted Array:n"); for(i=0;i < ELEMENTS;i++) printf("%d ",X[i]); shellsort(X,ELEMENTS); printf("n SORTED ARRAYn"); for(i=0;i < ELEMENTS;i++) printf("%d ",X[i]); printf("n"); }
  • 43. C programs cs567 Sort A Given Number Of Strings #include <stdio.h> #include <conio.h> #include <string.h> main() { char str[4][10]; int i; clrscr(); for( i = 0; i < 4; i++ ) { printf(“ nEnter name %d: ”, i+1 ); scanf(“ %s ”, str[i] ); } printf(“n Original Ordern”); for(i = 0; i < 4; i++ ) printf(“ %st ”, str[i] ); for(i = 0; i < 4; i++ ) { for(j = i + 1; j < 4; j++ ) if( strcmp(str[i], str[j]) > 0 ) { strcpy( temp, str[i] ); strcpy( temp, str[i], str[j] ); strcpy( str[j], temp ); } } printf(“n Sorted Ordern”); for( i = 0; i < 4; i++ ) printf(“ %st ”, str[i]); getch(); } String Compare #include<stdio.h> int stringCompare(char[],char[]); int main() { char str1[100],str2[100]; int compare;
  • 44. C programs cs567 printf("Enter first string: "); scanf("%s",str1); printf("Enter second string: "); scanf("%s",str2); compare = stringCompare(str1,str2); if(compare == 1) printf("Both strings are equal."); else printf("Both strings are not equal"); return 0; } int stringCompare(char str1[],char str2[]) { int i=0,flag=0; while(str1[i]!='u0000' && str2[i]!='u0000') { if(str1[i]!=str2[i]) { flag=1; break; } i++; } if (flag==0 && str1[i]=='u0000' && str2[i]=='u0000') return 1; else return 0; } Strong Number void strong_number() { int num,i,p,r,sum=0,save_num; printf("n Enter a number"); scanf("%d",&num); save_num=num; while(num) { i=1,p=1; r=num%10; while(i<=r)
  • 45. C programs cs567 { p=p*i; i++; } //while sum=sum+p; num=num/10; } //while if(sum==save_num) printf("%d is a Strong number", save_num); else printf("%d is not a Strong number", save_num); } Subtract without subtract operator #include<stdio.h> int main() { int a,b; int sum; printf("Enter any two integers: "); scanf("%d%d",&a,&b); sum = a + ~b + 1; printf("Difference of two integers: %d",sum); return 0; } Sum of Digit #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,num,x,sum=0; printf("Enter a number="); scanf("%d",&n); while(n>0) { x=n%10; sum=sum+x; n=n/10;
  • 46. C programs cs567 } printf("Sum of digits of a number=%d",sum); getch(); } Swap two numbers using bitwise operators #include <stdio.h> int main() { int i = 65; int k = 120; printf("n value of i=%d k=%d before swapping", i, k); i = i ^ k; k = i ^ k; i = i ^ k; printf("n value of i=%d k=%d after swapping", i, k); return 0; } Swap String #include<stdio.h> #include<string.h> #include<malloc.h> #include<conio.h> main() { char first[100], second[100], *temp; printf("Enter the first string "); gets(first); printf("Enter the second string "); gets(second); printf("nBefore Swappingn"); printf("First string: %sn",first); printf("Second string: %snn",second); temp = (char*)malloc(100); strcpy(temp,first); strcpy(first,second); strcpy(second,temp); printf("After Swappingn"); printf("First string: %sn",first);
  • 47. C programs cs567 printf("Second string: %sn",second); getch(); return 0; } Swap Two Number #include <stdio.h> int main() { int x, y, temp; printf("Enter the value of x and yn"); scanf("%d", &x, &y); printf("Before Swappingn x = %dny = %dn",x,y); temp = x; x = y; y = temp; printf("After Swappingn x = %dny = %dn",x,y); return 0; } Swap Without using third variable #include <stdio.h> void main() { int a,b; printf("Enter number1: ie a"); scanf("%d",&a); printf("Enter number2:ie b "); scanf("%d",&b); printf(value of a and b before swapping is a=%d,b=%d"a,b); a=a+b; b=a-b; a=a-b; printf("value of a and b after swapping is a=%d,b=%d"a,b); } Topological Sort #include<stdio.h> #define MAX 200 int n,adj[MAX][MAX];
  • 48. C programs cs567 int front = -1,rear = -1,queue[MAX]; void main() { int i,j = 0,k; int topsort[MAX],indeg[MAX]; create_graph(); printf(“The adjacency matrix is:n”); display(); for(i=1;i<+n;i++) { indeg[i]=indegree(i); if(indeg[i]==0) insert_queue(i); } while(front<=rear) { k=delete_queue(); topsort[j++]=k; for(i=1;ilt;=n;i++) { if(adj[k][i]==1) { adj[k][i]=0; indeg[i]=indeg[i]-1; if(indeg[i]==0) insert_queue(i); } } } printf("Nodes after topological sorting are:n"); for(i=0;i<=n;i++) printf("%d",topsort[i]); printf("n"); } create_graph() { int i,max_edges,origin,destin; printf("n Enter number of vertices:"); scamf("%d",&n); max_edges = n * (n - 1); for(i = 1;i <= max_edges;i++)
  • 49. C programs cs567 { printf("n Enter edge %d (00 to quit):",i); scanf("%d %d",&origin,&destin); if((origin == 0) && (destin == 0)) { printf("Invalid edge!!n"); i–; } else adj[origin][destin] = 1; } return; } display() { int i,j; for(i = 0;i <= n;i++) { for(j = 1;jrear) { printf(“Queue Underflow”); return; } else { del_item = queue[front]; front = front + 1; return del_item; } } int indegree(int node) { int i,in_deg = 0; for(i = 1;i <= n;i++) if(adj[i][node] == 1) in_deg++; return in_deg; } }
  • 50. C programs cs567 Transpose A Matrix #include <stdio.h> #include <conio.h> int main() { int m, n, i, j; int mat[10][10], trans[10][10]; printf(" Enter the number of rows and columns of matrix "); scanf(" %d %d ", &m, &n); printf(" Enter the elements of matrix n "); for( i = 0 ; i < m ; i++ ) { for( j = 0 ; j < n ; j++ ) { scanf(" %d ", &mat[i][j] ); } } for( i = 0 ; i < m ; i++ ) { for( j = 0 ; j < n ; j++ ) { trans[j][i] = mat[i][j]; } } printf(" Transpose of entered matrix :-n "); for( i = 0 ; i < n ; i++ ) { for( j = 0 ; j < m ; j++ ) { printf(" %dt ", trans[i][j] ); } printf(" n "); } getch(); return 0; }