C Programs

1.   C program to find roots of quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a,b,c,disc,root1,root2,real,img;
clrscr();
printf("Enter the value of coefficients\n");
printf("a:");scanf("%f",&a);
printf("b:");scanf("%f",&b);
printf("c:");scanf("%f",&c);

if((a==0)&&b)
{
printf("Unique root: %f\n",-c/b);
getch();
exit(0);
}
disc=(b*b)-(4*a*c);

if(disc>0)
{
printf("Roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1: %f\n",root1);
printf("Root2: %f\n",root2);
}

else if(disc<0)
{
printf("Roots are imaginary\n");
real=-b/(2*a);
img=sqrt(fabs(disc))/(2*a);
printf("Root1: %f +i%f\n",real,img);
printf("Root2: %f -i%f\n",real,img);
}


else
{
printf("Roots are equal\n");
root1=-b/(2*a);
printf("Roots: %f\n",root1);
}
getch();
}





2. Simulation of simple calculator

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a,b;
char ch;
int v;
clrscr();
printf("Simulation of simple calculator,With usual notations\n");
fflush(stdin);
v=scanf("%f%c%f",&a,&ch,&b);
if(v!=3)
{
printf("Invalid Entry\n");
getch();
exit(0);
}
switch(ch)
{
case '+':printf("Sum: %f\n",a+b);
       break;
case '-':printf("Diffrence: %f\n",a-b);
       break;
case '*':printf("Product: %f\n",a*b);
       break;
case '/':if(b==0)
      {
      printf("Divide by zero error\n");
      getch();
      exit(0);
      }
       printf("Divison: %f\n",a/b);
       break;
default:printf("Invalid entry\n");
}
getch();
}


3. To check the given number is prime or not

#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
int i=2,n;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
{
printf("%d is not prime number\n",n);
getch();
exit(0);
}
i++;
}
printf("%d is prime number\n",n);
getch();
}




4. To generate prime numbers between given range.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,flag;
clrscr();
printf("Enter the range\n");
scanf("%d%d",&m,&n);
for(i=m;i<=n;i++)
{
flag=1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=0;
break;
}
}
if(flag)
printf("%d\t",i);
}
getch();
}


5. GCD and LCM of two numbers by Euclid's Algorithm

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,r,a,b;
clrscr();
printf("Enter the values of M and N:");
scanf("%d%d",&m,&n);
a=m;
b=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
printf("%d is GCD of M and N\n",m);
printf("%d is LCM of M and N\n",(a*b)/m);
getch();
}





6. To check weather given string is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20];
int n,i,j;
clrscr();
printf("Enter the string\n");
scanf("%s",s);
n=strlen(s);
j=n-1;
for(i=0;i<j;i++,j--)
{
if(s[i]!=s[j])
break;
}
if(i<j)
 printf("%s:is not palindrome\n",s);
 else
 printf("%s:is palindrome",s);
getch();
}




7. Sin of an angle
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pi 3.14
void main()
{
int n,i,k,deg;
float x=0,num,den,sum=0,term;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the value of degree\n");
scanf("%d",&deg);
x=(deg*pi)/180;
num=x;
den=1;
k=2;
for(i=1;i<=n;i++)
{
 term=num/den;
 sum=sum+term;
 num=-num*x*x;
 den=den*k*(k+1);
 k=k+2;
}
printf("using sum of %d terms sin(%d)=%f\n",n,deg,sum);
printf("using built in function sin(%d) =%f",deg,sin(x));
getch();
}




8. To find cos of an angles from 0 deg to 180 in steps of 10 using series.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,k,deg;
float num,den,x,sum,term;
clrscr();
for(deg=0;deg<=180;deg+=10)
{
sum=0;
den=1;
num=1;
k=2;
x=deg*3.14/180;
for(i=1;i<=10;i++)
{
term=num/den;
sum+=term;
num=-num*x*x;
den=den*k*(k-1);
k+=2;
}
printf("Using sum of 10 terms cos(%d): %f\n",deg,sum);
}
getch();
}



9. Product of 2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q,trace=0;
clrscr();
printf("Enter the order of matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix A\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the order of matrix B\n");
scanf("%d%d",&p,&q);
printf("Enter the elements of matrix B\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
if(n==p)
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
printf("Product matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%3d",c[i][j]);
printf("\n");
}
if(m==q)
{
for(i=0;i<m;i++)
trace+=c[i][i];
printf("Trace=%d",trace);
}
else
printf("Trace is not possible\n");
}
else
printf("Product not possible\n");
getch();
}



10. Difference of two sets
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],res[10],i,j,k=0,m,n,l;
clrscr();
printf("Enter the cardinality of set A\n");
scanf("%d",&m);
printf("Enter the elements of set A\n");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("Enter the cardinality of set B\n");
scanf("%d",&n);
printf("Enter the elements of set B\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<m;i++)
{
for(j=i+1;j<m&&a[i]!=a[j];j++);
if(j==m)
{
for(l=0;l<n&&b[l]!=a[i];l++);
if(l==n)
{
res[k++]=a[i];
}
}
}
printf("Difference of A and B \n");
for(i=0;i<k;i++)
printf("%d\t",res[i]);
getch();
}



11. To check whether the given number is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
long int n,rev=0,m,r;
clrscr();
printf("Enter the number:");
scanf("%ld",&n);
m=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n/=10;
}
if(rev==m)
printf("%ld is palindrome",m);
else
printf("%ld is not palindrome",m);
getch();
}





12. To change the case of character in string 
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void main()
{
char s[20];
int i;
clrscr();
printf("Enter the string \n");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(isalpha(s[i]))
{
if(isupper(s[i]))
s[i]=tolower(s[i]);
else if(islower(s[i]))
s[i]=toupper(s[i]);
}
}
printf("converted string is %s",s);
getch();
}
       



13. String handling function.

    C program to find the length of a string


#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int i=0;
clrscr();
printf("Enter the string\n");
gets(str);
while(str[i]!='\0')
{
i++;
}
printf("Length of %s =%d",str,i);
getch();
}


C program to copy one string to another 

#include<stdio.h>
#include<conio.h>
void main()
{
char s[50],c[50];
int i=0;
clrscr();
printf("Enter the string\n ");
gets(s);
while(s[i]!='\0')
{
c[i]=s[i];
i++;
}
c[i]='\0';
printf("string copied %s",c);
getch();
}

           Download C program



C program for concatenation of two string


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[50];
int i,n,j;
clrscr();
printf("Enter the string 1\n");
gets(s1);
printf("Enter the string 2\n");
gets(s2);
n=strlen(s1);
for(i=n,j=0;s2[j]!='\0';j++,i++)
s1[i]=s2[j];
s1[i]='\0';
printf("concatenated string %s",s1);
getch();
}



14. C programs on recursion


Recursive function to compute the factorial of given number
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n;
clrscr();
printf("Enter the value of N:");
scanf("%d",&n);
printf("Factorial of %d : %d",n,fact(n));
getch();
}
int fact(int n)
{
if(n==1)
return 1;
return(n*fact(n-1));
}
                 Download C program


  Recursive function to compute fibinocci series of N terms.
#include<stdio.h>
#include<conio.h>
int fib(int);
void main()
{
int n,i;
clrscr();
printf("Enter the number of terms:");
scanf("%d",&n);
printf("Req. fibinocci series\n");
for(i=0;i<n;i++)
printf("%d\t",fib(i));
getch();
}
int fib(int n)
{
if(n==0||n==1)
return n;
return(fib(n-1)+fib(n-2));
}
 
          Download C program:


Recursive function to compute x^n where x and n are integers.

#include<stdio.h>
#include<conio.h>
int power(int,int);
void main()
{
int x,n;
clrscr();
printf("Enter the value of x and n:");
scanf("%d%d",&x,&n);
printf("%d^%d : %d",x,n,power(x,n));
getch();
}
int power(int x,int n)
{
if(n==0)
return 1;
return(x*power(x,n-1));
}

                    Download C program:



Tower of Hanoi

#include<stdio.h>
#include<conio.h>
void toh( int n,char s,char t,char d);
int move;
void main()
{
int n;
clrscr();
printf("Enter the number of disks\n");
scanf("%d",&n);
toh(n,'A','B','C');
printf("No of moves:%d",move);
getch();
}

void toh( int n,char s,char t,char d)
{
if(n==1)
{
printf("Move disk %d from %c to %c\n",n,s,d);
move++;
return;
}
toh(n-1,s,d,t);
printf("Move disk %d from %c to %c\n",n,s,d);
move++;
toh(n-1,t,s,d);
}







Permutation of string (without repetition)


#include<stdio.h>
#include<conio.h>
void swap (char *x,char *y);
void permit(char *a,int i,int n);
int k=0;
main()
{
char a[]="ABCDE";
clrscr();
permit(a,0,4);
printf("\n%d",k);
getch();
return(0);
}

void swap (char *x,char *y)
{
char temp;
temp=*x;
*x=*y;
*y=temp;
}

void permit (char *a,int i,int n)
{
int j;
if(i==n)          {
printf("%s\t",a);    k++;}
else
{
for(j=i;j<=n;j++)
{
swap((a+i),(a+j));
permit(a,i+1,n);
swap((a+i),(a+j));
}
}
}

                         Download C program




15. Program to read N names and sort them in alphabetical order using Bubble sort.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10][10],temp[10];
int n,i,j;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the names\n");
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n-1;i++)
{
 for(j=0;j<n-1-i;j++)
 {
 if(strcmp(a[j],a[j+1])>0)
 {
  strcpy(temp,a[j]);
  strcpy(a[j],a[j+1]);
  strcpy(a[j+1],temp);
  }
  }
  }
 printf("Sorted names \n");
 for(i=0;i<n;i++)
 printf("%s\n",a[i]);
 getch();
 }

                     
Download C program          

16. Sum of array elements with pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,*p,i,sum=0;
clrscr();
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<n;i++)
{
 sum=sum+*(p+i);
}
printf("sum of array =%d",sum);
getch();
}
               Download C program

17. To find Intersection of A and B.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],res[10],i,j,k=0,m,n,l;
clrscr();
printf("Enter the order of set a\n");
scanf("%d",&m);
printf("Enter the elements of set a\n");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("Enter the order of set b\n");
scanf("%d",&n);
printf("Enter the elements of set b\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<m;i++)
{
for(j=i+1;j<m&&a[i]!=a[j];j++);
if(j==m)
{
for(l=0;l<n&&b[l]!=a[i];l++);
if(l!=n)
{
res[k++]=a[i];
}
}
}
printf("Intersection of a and b \n");
for(i=0;i<k;i++)
printf("%d",res[i]);
getch();
}


18.To find Union of A and B.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,k=0,j,a[10],b[10],c[10];
clrscr();
printf("Enter the order of set A\n");
scanf("%d",&m);
printf("Enter the elements of set A\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the order of set B\n");
scanf("%d",&n);
printf("Enter the elements of set B\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<m;i++)
{
for(j=0;j<k&&a[i]!=c[j];j++);
if(j==k)
c[k++]=a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<k&&b[i]!=c[j];j++);
if(j==k)
c[k++]=b[i];
}
printf("Union of a and b= ");
for(i=0;i<k;i++)
printf("%d ",c[i]);
getch();
}

19. Program to read N numbers into array A, if element is even transfer it into array B else transfer it into C. Display all three arrays.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[20],i,n,l=0,k=0;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==0)
b[l++]=a[i];
else
c[k++]=a[i];
}
printf("\nArray A:  ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);

printf("\nArray B:  ");
for(i=0;i<l;i++)
printf("%d\t",b[i]);

printf("\nArray C:  ");
for(i=0;i<k;i++)
printf("%d\t",c[i]);
getch();
}

                     Download C program

20. Program to conduct liner search of an array of N elements .If search is success display its position else display unsuccessful search.

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[20],i,n,k;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element:");
scanf("%d",&k);
for(i=0;i<n;i++)
{
if(a[i]==k)
{
printf("Element found at %d position\n",i+1);
getch();
exit(0);
}
}
printf("Element is not found");
getch();
}


                  Download C program

21. Program to read N numbers into array A. Displays sum of negative numbers, positive numbers and average of all numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
float v,sp=0,sn=0,p=0,n=0,t,i,a;
clrscr();
printf("Enter the number of elements: ");
scanf("%f",&t);
printf("Enter the elements:");
for(i=0;i<t;i++)
{
scanf("%f",&v);
if(v>0)
{
p++;
sp+=v;
}
else
{
n++;
sn+=v;
}
}
a=(sp-sn)/t;
printf("Sum of positive numbers: %f",sp);
printf("\nSum of negative numbers: %f",sn);
printf("\nAverage of elements: %f",a);
getch();
}

                     Download C program


22. Program to sort array of N elements using Bubble sort.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
a[j]=a[j]+a[j+1];
a[j+1]=a[j]-a[j+1];
a[j]=a[j]-a[j+1];
}
}
}
printf("Sorted array:\t");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

                    Download C program


23. Program to evaluate the polynomial of order N.
P(x)=AnXn+An-1Xn-1+………….+A1+A0.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,x,n,s=0,i;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the coff.:");
for(i=0;i<=n;i++)
{
scanf("%d",&a);
s+=a*pow(x,i);
}
printf("\nReq. sum: %d",s);
getch();
}
                   Download C program

24. Program to read an array of N elements. For given array find mean, Variance and Standard deviation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[20],n,m=0,v,sd,m1,i;
clrscr();
printf("Enter the value of N:");
scanf("%f",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
m+=a[i];
}
m1=m;
m=m/n;
printf("Mean : %f\n",m);
v=pow((m1-m),2)/n;
sd=sqrt(v);
printf("Variance: %f\n",v);
printf("Standard deviation: %f",sd);
getch();
}


                    Download C program




25. Program to read an integer matrix of order m & n ,identify whether matrix is identity or not.

#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
int a[10][10],m,n,i,j;
clrscr();
printf("Identity matrix is defined only for sq.matrix\n");
printf("Enter the order 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",&a[i][j]);

for(i=0;i<m;i++)
for(j=0;j<n;j++)
{

if(i==j)
{
    if(a[i][j]!=1)
    {
    printf("Not identity matrix");
    getch();
    exit(0);
    }
}
else
{

     if(a[i][j]!=0)
    {
    printf("Not identity matrix");
    getch();
    exit(0);
    }

}
}
printf("Identity matrix");
getch();
}

                    Download C program.




26. Program to read an array in ascending order and search given keyword from array by binary search & print no. of probes  involved in it.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,count=0,mid,low,high,key,i;
clrscr();
printf("Enter the no. of elements:");
scanf("%d",&n);
printf("Enter the elements in ascending order:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element:");
scanf("%d",&key);
low=0;high=n-1;
while(low<=high)
{
  mid=(low+high)/2;
  count++;
  if(a[mid]==key)
  {
  printf("Element found at %d position",mid+1);
  break;
  }
  else if(a[mid]<key)
  low=mid+1;
  else
  high=mid-1;
}
if(low>high)
printf("%d is not found",key);
else
printf("No. of probes : %d",count);
getch();
}

                 Download C program.

No comments:

Post a Comment