Home

Introduction to C

Program 1

---------------------------------------

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf("This is my first program in C\n");
getche();
}

----------------------------------------
Enter the program in your C or Turbo C++ editor.
Save it as fprogram.c
Press alt F9 to compile. Correct mistakes pointed out,if any.
Press ctl+F9 to run.
The output is
This is my first program in C
--------------------------------------------------
Data types: int,long,float,double,char,char[20].
printf("...") is used for output.
For scanning,use scanf() and control strings %d,%ld,%f,%lf,%c,%s respectively.
Note the & in scanf (see next program)
If you do not use getche() function, you have to press F5
to see the output.
The function clrscr() is used to clear the screen.
stdio.h is a header file which contains input,output statements.
conio.h is used for clrscr() and getche() statements.
Everything between /*  */ is treated as comment.
You can also use
//this is a comment
for a single line comment.
Note that every statement should end with ;
\n is used to proceed to next line.
It is called new line character.
Notice that & is not used in scanf for str in the next program.
----------------------------------------

Program 2

Use of scanf()
-----------------------------------------

#include <stdio.h>
#include <conio.h>
void main()
{
int a;
float b;
char ch;
char str[80];
printf("Enter a character\n");
scanf("%c",&ch);
printf("Enter a string\n");
scanf("%s",str);
printf("Enter an integer\n");
scanf("%d",&a);
printf("Enter a float value\n");
scanf("%f",&b);
printf("ch=%c\n",ch);
printf("string=%s\n",str);
printf("a=%d\n",a);
printf("b=%f\n",b);
}

-----------------------------------------

Program 3

Calculation of simple interest
-----------------------------------------

#include <stdio.h>
#include <conio.h>
void main()
{
/* program to calculate simple interest */
float p,n,r,i;
clrscr();
printf("Enter principal,number of years,rate of interest:");
scanf("%f%f%f",&p,&n,&r);
i=p*n*r/100;
printf("interest=%.2f\n",i);
// .2f gives answer to decimal places
getche();
}

--------------------------------------------------

Program 4

dot and cross product of vectors using functions
-------------------------------------------------

#include <stdio.h>
#include <conio.h>
void main()
{
/* Program to calculate dot and cross product of vectors*/
// use of functions
//prototype of functions used in this program
// without these,the programs will not compile.
float dotproduct(float,float,float,float,float,float);
void  crossproduct(float,float,float,float,float,float);
float a1,b1,c1,a2,b2,c2;
clrscr();
printf("Enter the coefficients i,j,k of the first vector:");
scanf("%f%f%f",&a1,&b1,&c1);
printf("Enter the coefficients i,j,k of the second vector:");
scanf("%f%f%f",&a2,&b2,&c2);
printf("dot product is %.2f\n",dotproduct(a1,b1,c1,a2,b2,c2));
crossproduct(a1,b1,c1,a2,b2,c2);
getche();
}
//this functions returns a value
float dotproduct(float i1,float j1,float k1,float i2,float j2,float k2)
{
float x;
x=i1*i2+j1*j2+k1*k2;
return(x);
}
//this does not return a value
void crossproduct(float i1,float j1,float k1,float i2,float j2,float k2)
{
float coeffi,coeffj,coeffk;
coeffi=j1*k2-k1*j2;
coeffj=k1*i2-i1*k2;
coeffk=i1*j2-i2*j1;
printf("Cross product is (%.2f)i+(%.2f)j+(%.2f)k",coeffi,coeffj,coeffk);
}

-------------------------------------------------------
Use of if statement
Syntax
if(conditional statement)
{
/*statements to be executed*/
}
The operators <,<=,>,>=,!=,== are called relational operators.
conditional statements may be like
x>=5,x>5,x<=5,x<5,x==5,x!=5,where x is an integer.
if the coditional statement is true,
the statements inside the if loop are executed.
Note: a=5 means assign 5 to a.
if(a==5) must be used to test whether a is equal to 5.
Use of if-else
if(conditional statement)
{
/* statements*/
}
else
{
/* statements*/
}
--------------------------

Program 5

Roots of quadratic equation
---------------------------

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* Quadratic Equation */
float a,b,c,d,r1,r2,i1,i2;
int ch;
do
{
clrscr();
printf("Enter the coefficients of x^2,x,constant term:");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
printf("Roots are real and different\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Roots are %.2f,%.2f\n",r1,r2);
}
else
if(d==0)
{
printf("Roots are real and equal\n");
r1=-b/(2*a);
r2=-b/(2*a);
printf("Roots are %.2f,%.2f\n",r1,r2);
}
else
{
d=-d;
r1=-b/(2*a);
i1=sqrt(d)/(2*a);
r2=-b/(2*a);
i2=sqrt(d)/(2*a);
printf("Roots are complex:\n");
printf("the roots are (%.2f)+i(%.2f),(%.2f)-i(%.2f)\n",r1,i1,r2,i2);
}
printf("Do you want to try another problem? y or n\n");
ch=getchar();
ch=getchar();
}while((ch=='y') || (ch=='Y'));
}

--------------------------------------------
Use of for
example
int i;
for(i=1;i<=4;i++)
{
printf("%d  ",i);
}
The output will be
1  2  3  4
i++ can be replaced by i=i+1
Use of while
Example
int x=5;//x is an integer initialized to 5
while(x>0)
{
printf("%d",x);
x--;
}
Output will be 5 4 3 2 1
Use of do-while
Example
int x=5;
do
{
printf("%d,",x);
x--
}while(x>0);
output will be 5,4,3,2,1,
Note that a do loop will be always carried out once.
Use of switch statement
Example
int x;
printf("1.Choice 1\n");
printf("2.Choice 2\n");
printf("Enter your choice 1 or 2");
scanf("%d",&x);
switch(x)
{
case 1:
printf("you have selected the first option"):
break;
case 2:
printf("you have selected the second option"):
break;
default:
printf("Sorry. Type 1 or 2\n"):
}
switch can also be used with characters like
case:'a'
-----------------------------------------------

Program 6

Use of for,while,do while,switch
------------------------------------------------

#include <stdio.h>
#include <conio.h>
void main()
{
// Use of for,while,do while,switch
int i,x;
clrscr();
for(i=1;i<=4;i++)
{
printf("%d  ",i);
}
/*The output will be
1  2  3  4
Use of while
*/
x=5;
printf("\n");
while(x>0)
{
printf("%d ",x);
x--;
}
/*Output will be 5 4 3 2 1
Use of do-while
Example
*/
printf("\n");
x=5;
do
{
printf("%d,",x);
x--;
}while(x>0);
/*output will be the same,but with comma instead of space.
Use of switch statement
Example
*/
printf("\n");
printf("1.Choice 1\n");
printf("2.Choice 2\n");
printf("Enter your choice 1 or 2:");
scanf("%d",&x);
switch(x)
{
case 1:
printf("you have selected the first option");
break;
case 2:
printf("you have selected the second option");
break;
default:
printf("Sorry. Type 1 or 2\n");
}
getche();
}

--------------------------------------
Logical operators
Operator && is  used for AND
Operator || is  used for OR
Example
int x,y;
x=5;
y=7;
if(x>4 && y>8)
// if both are true
{
printf("yes\n");
}
else
{
printf("no\n");
}
//output will be no
if(x>4 || y>8)
// if one of them is true
{
printf("yes\n");
}
else
{
printf("no\n");
}
//output will be yes
----------------------------------------------------
Arrays
int a[20];
This defines an integer array a with 20 values
a[0],a[1],...........a[19]
int b[3][3];
This defines a two dimensionsl array with 9 values
b[0][0],b[0][1],b[0][2]
b[1][0],b[1][1],b[1][2]
b[2][0],b[2][1],b[2][2]
----------------------------------------------

Program 7

Use of arrays
Matrix addition
-----------------------------------------------

#include <stdio.h>
#include <conio.h>
void main()
{
float x[5];
float a[2][2],b[2][2],c[2][2];
float sum=0;
int i,j;
clrscr();
printf("Enter five numbers\n");
for(i=0;i<5;i++)
{
scanf("%f",&x[i]);
sum=sum+x[i];
// this can also be written as sum+=x[i];
}
printf("The sum of the five numbers is %.2f\n",sum);
printf("Enter the first matrix row by row\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
scanf("%f",&a[i][j]);
}
printf("Enter the second matrix row by row\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
scanf("%f",&b[i][j]);
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
printf("The sum of the two matrices is\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%.2f  ",c[i][j]);
}
printf("\n");
}
getche();
}

-----------------------------------------------------
Use of Maths functions
sin(x),cos(x),tan(x),atan(x),sqrt(x)
x must be in radians.
atan(x) stands for tan-1x
The header file math.h must be included
The following program calculates the resultant
of a number of forces acting at a point.
------------------------------------------------------

Program 8

Use of maths functions
Resultant of forces
------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
double f[10],a[10],rx=0,ry=0,r;
int n,i;
clrscr();
printf("How many forces? Not more than 10:");
scanf("%d",&n);
printf("Enter magnitude of forces and\n");
printf("their inclinations to positive x-axis");
printf("in successive rows\n");
for(i=0;i<n;i++)
{
scanf("%lf%lf",&f[i],&a[i]);
a[i]=3.1416*a[i]/180;
}
for(i=0;i<n;i++)
{
rx+=f[i]*cos(a[i]);
ry+=f[i]*sin(a[i]);
}
r=sqrt(rx*rx+ry*ry);
printf("The component along x-axis is %.1lf\n",rx);
printf("The component along y-axis is %.1lf\n",ry);
printf("The resultant is %.1lf\n",r);
printf("Angle to positive x axis is %.1lf\n",atan(ry/rx)*180/3.1416);
getche();
}

Use of recurrence functions
This program calculates factorial of a number.
---------------------------------------------------

Program 9

Use of recurrence functions
Factorial of a number
---------------------------------------------------

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
long fact(long);
long n;
clrscr();
printf("Factorial. Enter number not greater than 12\n");
scanf("%ld",&n);
printf("The facorial of %ld is %ld",n,fact(n));
getche();
}
long fact(long n)
{
if(n==1) return (1);
else
return(n*fact(n-1));
}

----------------------------------------------------
1