தேமதுரத் தமிழோசை உலகமெலாம் பரவும் வகை செய்தல் வேண்டும்
"யாதும் ஊரே யாவரும் கேளிர்
தீதும் நன்றும் பிறர்தர வாரா"
C Programming – Some Examples
/* Addition of two matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,m,m1[50][50],m2[10][10],m3[10][10];
clrscr();
printf("enter the size of the matrixces\n");
scanf("%d%d",&m,&n);
printf("enter the element of the first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&m1[i][j]);
printf("enter the element of the second matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&m2[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
m3[i][j]=m1[i][j]+m2[i][j];
printf("the resultant matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",m3[i][j]);
}
printf("\n");
}
getch();
}
/* Biggest of three numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
printf("the greatest number=%d",a);
else
printf("the greatest number=%d",c);
else
if(b>c)
printf("the greatest number=%d",b);
else
printf("the greatest number=%d",c);
getch();
/* sum of first n numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i=1;
clrscr();
printf("enter the numbers n\n");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i=i+1;
}
printf("the sum=%d",sum);
getch();
}
\* factorial *\
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1,i;
clrscr();
printf("enter the numbers\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("the factorial value=%d",fact);
getch();
}
\* swapping of two numbers *\
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("enter the number for swaping\n");
scanf("%d%d",&a,&b);
temp=a;
a=b;
b=temp;
printf("a=%d\t b=%d",a,b);
getch();
}
\ * factorial using recursion * \
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n enter a number;");
scanf("%d",&n);
printf("\n factorial value=%d",fact(n));
getch();
}
int fact(int k)
{
if(k==0)
return 1;
else
return k*fact(k-1);
}
\ * multiplication table *\
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
printf("\n a multiplication table:\n\n");
printf("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8\n\n");
for(i=1;i<=10;i++)
{
printf("%d\t",i);
for(j=1;j<=8;j++)
{
printf("%d\t",i*j);
}
printf("\n\n");
}
getch();
}
\ * ascending order * \
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i,j,temp;
clrscr();
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;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 elements in ascending order\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}
\ * Fibonacci series * \
#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1=0,n2=1,n3;
clrscr();
printf("enter the last term of series\n");
scanf("%d",&n);
n3=n1+n2;
printf("%d\t %d\t",n1,n2);
while(n3<=n)
{
printf("%d\t",n3);
n1=n2;
n2=n3;
n3=n1+n2;
}
getch();
}
\ * call by value *\
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a,b,c");
scanf("%d%d",&a,&b);
c=sum(a,b);
printf("sum of a+b is%d",c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}
\ * swapping using pointers * \
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the value of a,b\t");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("after swapping\t");
printf("a=%d\t",a);
printf("b=%d\t",b);
getch();
}
int swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
return 0;
}
/* string operations and palindrome checking*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20],s3[20],s4[20],s5[20],s6[20];
int l,i;
clrscr();
printf("enter a string to find the length and to copy the string");
scanf("%s",s1);
l=strlen(s1);
printf("the lenth of the string=%d",l);
strcpy(s1,s2);
printf("the copied string is %s\nw",s2);
printf("enter two strings compare and to concatenation");
scanf("%s%s",s3,s4);
printf("the comparision is=%d\n",i);
strcat(s3,s4);
printf("the concatinated string is=%s\n",s3);
printf("enter the string for polindrome checking");
scanf("%s",s5);
strcpy(s6,s5);
strrev(s5);
if(strcmp(s5,s6)==0)
{
printf("the given string is a polindrome");
}
else
{
printf("the given string is not polindrome");
}
getch();
}
/* simple and compound interest*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,n,r,si,ci;
clrscr();
printf("enter the values of p,n,r\n");
scanf("%f%f%f",&p,&n,&r);
si=(p*n*r)/100;
ci=p*pow((1+r/100),n)-p;
printf("the simple interest=%f",si);
printf("the compound interest=%f",ci);
getch();
}
/* Quadratic Equation*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2,r,im;
clrscr();
printf("enter the coefficients a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("the roots are imaginary\n");
im=sqrt(-d)/(2*a);
r=(-b/2*a);
printf("root 1 is %f+i%f\n",r,im);
printf("root 2 is %f-i%f\n",r,im);
}
else if(d==0)
{
printf("the real and equal roots are\n");
r1=-b/(2*a);
r2=r1;
printf("%f\t%f",r1,r2);
}
else
{
printf("the real and unequal roots are\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("%f\t%f",r1,r2);
}
getch();
}
/* Palindrome check */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int length;
clrscr();
printf("enter the string");
scanf("%s",str1);
strcpy(str2,str1);
strrev(str1);
if(strcmp(str1,str2)==0)
printf("given string is palindrome");
else
printf("given string is not a palindrome");
getch();
}
/* Payroll*/
#include<stdio.h>
#include<conio.h>
struct employee
{
char ename[20],dept[20];
float basic,hra,da,pf;
double gross,net;
};
void main()
{
struct employee[5];
int i;
clrscr();
for(i=0;i<=5;i++)
{
printf("enter the employee name and department:\n");
scanf("%s%s",e[i].ename,e[i].dept);
printf("enter basic pay:\n");
scanf("%f",&e[i].basic);
if(e[i].basic>7500)
{
e[i].hra=(3.0/100.0)*basic;
e[i].da=(50.0/100.0)*basic;
e[i].pf=(15/100.0)*basic;
e[i].gross= e[i].basic + e[i].hra+e[i].da;
e[i].net=e[i].gross-e[i].pf;
}
for(i=0;i<5;i++)
{
printf("employee.name:%s\n",e[i].name);
printf("basic:%f\n",e[i].basic);
printf("gross pay:%f\n",e[i].gross);
printf("net pay:%f\n",e[i].net);
}
getch();
}
/* Pascal Triangle*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);
}
printf("\n");
}
getch();
}
/* Multiplication of two matrices*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m1[10][10],m2[10][10],m3[10][10],i,j,k;
clrscr();
printf("enter the order of square matrixes\n");
scanf("%d",&n);
printf("enter the element of first matrix\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&m1[i][j]);
printf("enter the element of second matrix\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&m2[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
m3[i][j]=0;
for(k=0;k<n;k++)
{
m3[i][j]=m3[i][j]+m1[i][j]*m2[k][j];
}
}
}
printf("the resultant matrix is\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%d\t",m3[i][j]);
printf("\n");
getch();
}
Plant Tissue culture photos
It is a art of science.
It is more useful technique for biodiversity conservation
Program for finding roots of the Quadratic equation in C
The program written below in C is used to find roots of the quadratic equation.
#include "stdio.h"
#include "math.h"
#include "conio.h"
void main()
{
int A,B,C;
float disc,deno,x1,x2, real, img;
clrscr();
printf("\n-------------------------------------------------------");
printf("\n\n PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION ");
printf("\n\n-------------------------------------------------------");
printf("\n\n\t ENTER THE VALUES OF A,B,C: ");
scanf("%d%d%d",&A,&B,&C);
disc = (B*B)-(4*A*C);
deno = 2*A;
if(disc > 0)
{
printf("\n\t THE ROOTS ARE REAL");
x1 = (-B/deno)+(sqrt(disc)/deno);
x2 = (-B/deno)-(sqrt(disc)/deno);
printf("\n\n\t THE ROOTS ARE: %.3f and %.3f\n",x1,x2);
}
else if(disc == 0)
{
printf("\n\t THE ROOTS ARE EQUAL");
x1 = -B/deno;
printf("\n\n\t THE ROOT IS: %.3f\n",x1);
}
else
{
printf("\n\t THE ROOTS ARE COMPLEX\n");
real = (-B/deno);
img = sqrt(-disc)/deno;
printf("\n\n\t THE ROOTS ARE: (%.3f + %.3fi) and (%.3f - %.3fi) \n",real,img,real,img);
}
printf("\n-------------------------------------------------------");
getch();
}
Sample input and output
-------------------------------------------------------
PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION
-------------------------------------------------------
ENTER THE VALUES OF A,B,C: 1 -5 6
THE ROOTS ARE REAL
THE ROOTS ARE: 3.000 and 2.000
-------------------------------------------------------


