Lab Manual Focp
Lab Manual Focp
This course will show you how to use the C language to create useful programs.
Interactive exercises ranging from simple to challenging will illustrate all the
important features of the language.
The course is composed of sections. The first section will introduce the student
to the C programming language, as well as some general programming issues.
At the start of this section, the student can verify that he or she meets the
course prerequisites.
The second section presents the basic details of the C language as they differ
from those of C++. Following this section is a focus on some advanced language
issues involving functions and memory management. Another section presents
string manipulation and file I/O along with an overview of C's standard
libraries. Finally, the course concludes with a lesson on building projects from
several files, and a comprehensive look at creating useful data structures in C.
SOFTWARE : C LANGUAGE
START
read a , b , c
No Yes
is a>b and a>c is b>a and b>c
Yes
No
print
Largest of three numbers: a print
Largest of three numbers: b
print
Largest of three numbers: c
STOP
ALGORITHM TO FIND LARGEST OF THREE NUMBERS
1. float a,b,c;
2. print ‘Enter any three numbers:’;
3. read a,b,c;
4. if((a>b)&&(a>c))
5. print ‘Largest of three numbers:’, a;
6. else
7. if((b>a)&&(b>c))
8. print ‘Largest of three numbers:’, b;
9. else
10.print ‘Largest of three numbers:’, c;
PROGRAM TO FIND LARGEST OF THREE NUMBERS
#include<stdio.h>
#include<conio.h>
void main( )
{
float a,b,c;
printf(“Enter any three numbers:”);
scanf(“%f%f%f”,&a,&b,&c);
if((a>b)&&(a>c))
{
printf(“Largest of three numbers: %f”,a);
}
else
if((b>a)&&(b>c))
{
printf(“Largest of three numbers: %f”,b);
}
else
{
printf(“Largest of three numbers: %f”,c);
}
getch( );
}
OUTPUT :
START
Yes
i=0 i<10
read a[i]
No
i++
i=0
i<10 Yes
is a[i]>a[j] t=1
j=i+1
No
t=0 is t==1
j<10
j++ No Yes
i++
STOP
ALGORITHM TO FIND LARGEST NUMBER OUT OF TEN
NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,s=0,j,t;
clrscr();
printf(“Enter ten numbers:”);
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=1;
}
else
{
t=0;
break;
}
}
if(t==1)
{
printf("\nLargest no.:%d",a[i]);
break;
}
}
getch();
}
OUTPUT:
Enter ten numbers: 23
4
59
10
91
35
66
33
9
97
Largest no.: 97
FLOWCHART TO READ A STRING AND WRITE IT IN REVERSE
ORDER
START
Read array a
i=0
i=0
No
a[i]!= ‘\0’ j=g-1 No
i<g
Yes
Yes
g++
b[j]=a[i]
i++
j—
i++
print
Reverse order of string: b
stop
ALGORITHM TO READ A STRING AND WRITE IT IN REVERSE
ORDER
1. character a[20],i , b[20],g=0,j;
2. print ‘Enter any string:’
3. read array a;
4. Length of the string in the array is calculated by using for loop in ‘g’;
5. string in array ‘a’ is reversed in array ‘b’ by incrementing value of ‘i’ and
decrementing value of ‘j’ by for loop;
6. print ‘Reverse order of string:’,b;
PROGRAM TO READ A STRING AND WRITE IT IN REVERSE
ORDER
#include<stdio.h>
#include<conio.h>
void main()
{
char a[20],i , b[20],g=0,j;
clrscr();
printf("Enter any string: ");
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
g++;
}
j=g-1;
for(i=0;i<g;i++)
{
b[j]=a[i];
j--;
}
printf("Reverse order of string: %s",b);
getch();
}
OUTPUT :
Enter any string: Twinkle
START
Read array a
i=0
i=0
No
a[i]!= ‘\0’ j=g-1 No
i<g
Yes
Yes
g++
b[j]=a[i]
i++ i=0
j—
i++
Yes Yes No
a[i]==b[i] i<g
No
m=1 i++
m=0
1.
1.
Yes
m=0
No
Print m=1
String is Palindrome
Print
String is not Palindrome
STOP
ALGORITHM TOCHECK THAT THE INPUT STRING IS A
PALINDROME OR NOT
i=0
j=0
No
a[i]!= ‘\0’
b[j]!= ‘\0’
Yes
c[i]=a[i]
c[i+j]=b[j]
i++
j++
print
The Concatenated string is: c
stop
ALGORITHM TO CONCATINATE TWO STRINGS
1. character array a[25],b[25],c[25];
2. integer i,j;
3. print ‘Enter first string:”;
4. read a;
5. print ‘Enter second string:”;
6. read b;
7. string ‘a’ is copied to array ‘c’ by using for loop;
8. string ‘b’ is copied to array ‘c’next to the first string by using for loop;
9. print ‘The concatenated string is:’,c;
WRITE A PROGRAM TO CONCATINATE TWO STRINGS
#include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25],c[25];
int i.j;
clrscr();
printf(“Enter first string:”);
scanf(“%s”,a);
printf(“Enter second string:”);
scanf(“%s”,b);
for(i=0;a[i]!=’\0’;i++)
c[i]=a[i];
for(j=0; b[j]!=’\0’; j++)
c[i+j]=b[j];
c[i+j]=’\0’;
printf(“The concatenated string is:\n%s,c”);
getch();
}
OUTPUT :
START
read m,n,p,q;
No No
Print i<m i<p
‘Matrix cannot be multiplied’
Yes
Yes
No No
i=0 j<n j<q
j=0
s=0
Yes Yes
i<m
j<q s++
j++
c[i][j]=c[i][j]+a[i][s]*b[s][j] I++
s=0 1.
1.
i=0
j=0
i<m
j<q
Print
‘Product of matrix A and matrix B is’
print c[i][j]
STOP
ALGORITHM TO MULTIPLY TWO MATRICES
c[i][j]=c[i][j]+a[i][s]*b[s][j];
}
}
printf("\nProduct of matrix A and matrix B is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
Input row and column of matrix-A
3
4
Input row and column of matrix-B
2
4
Matrix cannot be multiplied
START
Read a,b,c
Quad(a,b,c);
d=(b2)-(4*a*c)
Yes Is No is Yes
d<0 d=0
No r=-b/2*a
stop
ALGORITHM TO SOLVE QUADRATIC EQUATION
1. integer a,b,c;
2. float d,r1,r2,r3,r4,r;
3. print ‘Enter values of a,b,c of a quadratic equation:’;
4. read a,b,c;
5. value of a,b,c is transferred to function ‘quad’and body of function is:
6. d b x b-4 x a x c;
7. if(d<0)
8. print ‘Value of Discriminant is negative’;
9. else
10.if(d=0)
11.print ‘Roots are real’
12.r=-b/2*a;
13.print ‘First and Second root of equation:’,r;
14.else
15.r1 -b+sqrt(d);
16.r2 2*a;
17.r r1/r2;
18.r3 -b-sqrt(d);
19.r4 r3/r2;
20.print ‘First root of equation: ’, r;
21.print ‘Second root of equation: ’, r4;
WRITE A PROGRAM TO SOLVE QUADRATIC EQUATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
void quad(int a, int b ,int c);
int a,b,c;
float r;
clrscr();
printf(“Enter values of a,b,c of a quadratic equation:\n” );
scanf(“%d%d%d”,&a,&b,&c);
quad(a,b,c);
getch();
}
START
print
How many no. will you enter:
Read n
i=0
No
i<n l=a[1] i=0
Yes
read a[i] No
i<n
i++ Yes
a[i]>l
i++ l=a[i]
print
‘ Largest no. is:’,l
1.
1
sl=a[1] i=0
i<n
a[i]>sl
a[i]=l
i++ sl=a[i]
print
‘Second largest no. is:’sl
STOP
ALGORITHM TO FIND LARGEST AND SECOND LARGEST
NUMBER OUT OF GIVEN NUMBER
1. integer i,a[55],n,l=0,sl;
2. print"How many no. will you enter: ";
3. read n
4. values will be entered in array ‘a’ by using for loop
5. l=a[1]
6. by using for loop and incrementing value of ‘i’ a[i] is checked for largest no. with
‘l’(a[i]>l)
7. the largest value is stored in ‘l’ and printed
8. print "Largest element is:",l;
9. again the above process is continued but if a[i]equals to largest value ‘l’ then
value of ‘i’ is incremented and second largest value is stored in ‘sl’
10.print "Second Largest element is:",sl;
WRITE A PROGRAM TO FIND LARGEST AND SECOND
LARGEST NUMBER OUT OF GIVEN NUMBER
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[55],n,l=0,sl;
clrscr();
printf("How many no. will you enter: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
l=a[1];
for(i=0;i<n;i++)
{
if(a[i]>l)
{
l=a[i];
}
}
printf("Largest element is:%d",l);
sl=a[1];
for(i=0;i<n;i++)
{
if(a[i]>sl)
{
sl=a[i];
}
}
printf("Second Largest element is:%d",sl);
getch();
}
OUTPUT:
How many no. will you enter:
10
Enter numbers
20
41
15
70
90
35
20
55
28
16
Largest element is: 90
Second Largest element is: 70
ALGORITHM TO FIND WHETHER A NUMBER IS PRIME OR NOT
1. Declare two integer values I & n and initialize third integer value.
3. If n%2 = 0 then f = 1.
#include<stdio.h>
#include<conio.h>
void main( )
{
int num, i;
clrscr( ) ;
printf(“”\n enter a no.”) ;
scanf(“%d”, & num) ;
i=2 ;
While(I<=(num-1))
{
if(num % I= =0)
{
printf(“not a prime no.”);
break ;
}
I++ ;
}
if(i = = num)
printf(“prime number”);
getch();
}
OUTPUT :
Enter a number : 3
Prime number
Enter a number : 9
Not a Prime number
ALGORITHM TO FIND THE MALE AND FEMALE
AVERAGE HEIGHT
STEP 1 : Declare the variable i,mh,fh as integer value and mavg,favg,msum,fsum as float
value.
STEP 6: Stop
WRITE A PROGRAM TO FIND AVERAGE OF MALE &
FEMALE HEIGHT IN THE CLASS
#include<stdio.h>
#include<conio.h>
void main()
{
int mh[5],fh[5],i;
float mavg,favg,msum=0,fsum=0;
clrscr();
printf(“enter the height of male\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&mh[i]);
msum=msum+mh[i];
}
mavg=msum/5;
20
22
25
24
23
enter the height of female
22
20
21
23
22
STEP 5. Stop.
-
WRITE A PROGRAM TO FIND FACTORIAL OF A NUMBER
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i.fact=1;
clrscr();
printf(“Enter the number \n”);
scanf(“%d%”,&a);
for(i=1;i<=a;i++)
{
fact=fact*i;
}
printf(“the factorial is %d”,fact);
getch();
}
OUTPUT:
Enter the number
6
the factorial is
720
ALGORITHM TO PRINT THE FIBONACCI SERIES
STEP 6. Stop.
-
WRITE A PROGRAM TO PRINT FIBONACCI SERIES
#include<stdio.h>
#include<conio.h>
void main()
{
int f,s,n,c,t;
clrscr();
printf(“Enter the number of series \n”);
scanf(“%d”,&n);
f=1;
s=1;
printf(“%d\t”,f);
printf(“%d\t”,s);
for(c=1;c<=n-2;c++)
{
t=f+s;
printf(“%d\t”,t);
f=s;
s=t;
}
getch();
}
OUTPUT:
Enter the number of series
6
1 1 2 3 5 8
ALGORITHM TO FIND THE REVERSE OF A NUMBER
STEP 4 : b=n%10
s=(s*10)+b
n=n/10
STEP 5 : print the reverse number in s.
STEP 6. Stop.
-
WRITE A PROGRAM TO REVERSE OF NUMBER
#include<stdio.h>
#include<conio.h>
void main()
{
int n,b,s=0;
clrscr();
printf(“Enter the number \n”);
scanf(“%d”,&n);
while(n!=0)
{
b=n%10;
s=(s*10)+b;
n=n/10;
}
printf(“The reverse number is %d”,s);
getch();
}
OUTPUT:
Enter the number
123
STEP 3: temp=a
a=b
b=temp
STEP 5. Stop.
-
WRITE A PROGRAM TO SWAP OF TWO NUMBER
#include<stdio.h>
#include<conio.h>
void main()
{
int a,,b,temp;
clrscr();
printf(“Enter the number of a&b \n”);
scanf(“%d\n%d\n”,&a,&b);
temp=a;
a=b;
b=temp;
printf(“the swapped values are \n”);
printf(“%d\n%d\n”,a,b);
getch();
}
OUTPUT:
Enter the number of a&b
4
8
STEP 3: using for loop i is compare when it is not equal to and less than a.
j is initialize for blank space.
k is initialize to print the star.
STEP 4. Stop.
-
WRITE A PROGRAM TO PRINT PYRAMID STAR
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a,j,k;
clrscr();
printf(“enter the number of line \n”);
scanf(“%d”,&a);
for(i=0;i<a;i++)
{
printf(“\n”);
for(j=0;j<10-i;j++)
{
printf(“ ”);
}
for(k=0;k<=2*i;k++)
{
printf(“*”);
}
}
getch();
}
OUTPUT:
*
***
*****
*******
*********
ALGORITHM TO PRINT THE SUM OF TWO MATRICES
STEP 1 : Declare two dimensional array a,b,c and int variable i,j,k,r1,r2,c1,c2.
STEP 3: using for loop i,j, enter the element of matrix a in a[i][j]
using for loop i,j, enter the element of matrix b in b[i][j].
STEP 4 : c[i][j]=a[i][j]+b[i][j]
STEP 6. Stop
WRITE A PROGRAM TO SUM OF TWO MATRICES
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,r1,r2,c1,c2;
clrscr();
printf("input the order of matrix A:");
scanf("%d%d",&r1,&c1);
printf("input the order of matrix B:");
scanf("%d%d",&r2,&c2);
printf("enter the element of matrix A(row wise) \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}}
printf("enter the element of matrix B(row wise) \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}}
printf("the element of matrix A is \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
printf("%d",a[i][j]);
printf("\t");
}}
printf("the element of matrix B is \n");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d",b[i][j]);
printf("\t");
}}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}}
printf("the addition of matrix is :");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d",c[i][j]);
printf("\t");
}}
getch();
}
OUTPUT:
input the order of matrix A:
2
2
input the order of matrix B:
2
2
3 6
7 9
7 12
9 17
New ideas/ experiments required for Computer programming lab
behind university syllabus
1. LIST OF FREQUENTLY ASKED QUESTIONS
• What can I safely assume about the initial values of variables which
are not explicitly initialized? If global variables start out as ``zero,''
is that good enough for null pointers and floating-point zeroes?
• What is the right type to use for boolean values in C? Why isn't it a
standard type? Should #defines or enums be used for the true and false
values?
• What's the difference between ``char const *p'' and ``char * const
p''?
• char c;
• while((c = getchar()) != EOF)...