C Lab Manual
C Lab Manual
C Lab Manual
For
Programming Lab (IT291)
B.Tech. (IT)
SEM II
January, 2014
IT291
2. Course Title:
Programming Lab
3. Core
Elective
C Language
I year II Semester
7. Course objective:
8. List of programs:
9. Evaluation procedure
INDEX
DESCRIPTION EVALUATION
TOTAL MARKS
15 M
5M
5M
15 M
40 M
60 M
Experiment process:
Required Software/ Software Tool:
- Windows Operating System and/ or Linux Operating System
- Turbo C/C++ IDE.
Common Procedure:
Step 1: For the given problem statement design
Flowchart/Algorithm/Logic
Step 2: Define variables and functions which will show the flow of program
I: Create a folder in either E or F drive with your Id Number or Name Followed by
RollNo.
II: Start the TC (Turbo C) from Desktop Icon or Go To Directory D:/TC/BIN/ and
run tc.exe. An Editor window will be opened.
III: Click on File Menu --> New. New (.c) file will be created. Write C code in the file
with .c extension. Again Click on File - Save a dialog box is going open write the path to
your directory e.g. E:\Student_Roll\FileName.C and Press OK. Now your C program is
going to save at your directory.
IV: Go To Option-->Directories Check that Include Directory is Set As
D:\TC\Include and Library Directory is Set To D:\TC\LIB.
Step 3: If the OS is Windows than compile and run .c file using turbo C.
Compile code using gcc compiler for Linux, which will create a.out executable file.
Step 4: Test the program using sample input and write down output.
Exercise 1:
Write a Program in C to find the sum of individuals Digits of a positive Integer.
Solution:
Algorithm
1)
2)
3)
4)
Input a number n.
Check n is +ve, if not goto step 6.
S=0,m=n.
Repeat the following until n=0.
i. r=n%10.
ii. s+=s+r.
iii. n=n/10.
5) print s, m.
6) stop.
Flowchart
Start
Input n
Yes
No
If
n<0
S=0.m=n
Yes
r=n%10
S=s+r
n=n/10
Print not possible
print m, s
Stop
Exercise 2:
Write a program a Fibonacci sequence is defined as follows.
The first and second terms in the sequence are 0 and 1. Subsequent terms are found by
adding the precedes two preceding two terms in the sequence, Write a C Program to
Generate the first n terms of the sequence.
Solution:
Algorithm
1.
2.
3.
4.
5.
Flowchart
Start
Input n
f1=0, f2=1
Print f1, f2
Repeat the following for
n-2 times
f=f1+f2
Print f
f1=f2
f2=f
Stop
Exercise 3:
Write a C program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.
Solution:
Algorithm:
1. Enter a no n
2. for i=1 to n do
(a) k=prime(i).
(b) if(k==1)
(i) Print prime, i
3. Stop.
Algorithm: prime(i)
1.
2.
3.
4.
5.
6.
7.
8.
p=sqrt(i).
j=2,l=1
while(j<=p)( repeat 3 through 7)
r=i%j
if(r==0) then no 6, else step 7.
l=0, goto step 8.
j++.
return(l)
Flowchart:
start
Input n
No
For i=1 to n
yes
k=prime(i)
No
If
k==1
Yes
Print i
stop
Prime(i)
p=sqrt(i)
j=2,l=1
While(j<=p)
True
r=i%j
No
If r==0
j++
Yes
l=0
return(l)
False
Exercise 4:
Write a C Program to find the roots of a quadratic equation.
Algorithm:
Flowchart:
Start
Enter values a, b, c
d= b2 - 4 ac
Yes
ch = 1
If
d=0
No
No
If
d>0
Yes
ch = 2
ch = 3
Print a, b, c, d
Switch
case ch
1
Print Equal roots
2
Print roots are real
& unequal
3
Print Imaginary
roots
x1 = x2 = - b / 2a
x1 = - b+d/2a
Print x1, x2
Print x1 is
complex
x2=-b-d/2a
Print x1, x2
Stop
Print x2 is
complex
Exercise 6:
Write a C Program, which takes two integers operands and one operator from the user,
performs the operation and prints the result. (Consider the operators +,-,*,/,% and use Switch
Statement ).
Algorithm:
1. Begin
2. Enter a, b values.
3. Print MENU.
(i)
Print + Addition.
(ii)
Print - Subtraction.
(iii)
Print * Multiplication.
(iv)
Print / Division.
(v)
Print % Remainder.
(vi)
Print E Exit.
4. Print Enter your choice.
5. If op==E then goto step 8 otherwise follow the below steps
6. Switch(op)
a. case +:
i. Print Addition.
ii. c=a+b.
iii. Print Sum=c.
iv. break
b. case -:
v. Print Subtraction.
vi. c=a-b.
vii. Print Difference=c.
viii. break
c. case *:
ix. Print Multiplication.
x. c=a*b.
xi. Print Product=c.
xii. break
d. case /:
xiii. Print Division.
xiv.
c=a/b.
xv.
Print Quotient=c.
xvi.
break
e. case %:
xvii. Print Remainder.
xviii. c=a%b.
xix.
Print Remainder=c.
xx. break
f. default:
xxi.
Print Invalid Option.
xxii. break
7. while(1) then goto step 3.
8. Stop.
Flowchart:
Start
Input a, b
Menu
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
E Exit
Input choice op
Yes
If op = E
No
Switch
(OP)
+
c=a+b
c=ab
c=a*b
Print
sum c
Print
Diff c
Print
product c
Stop
c=a/b
Print
Quaff c
c=a%b
Print
remainder c
Exercise 7:
Write a C program that uses functions to perform the following operations.
i) To insert a sub-string into given main string from a given position.
Algorithm:
1. Enter the main string: s
2. Determine length of string: s i.e. l
3. Print the main string: s
4. Input position of the substring to be inserted: p
5. if p is ve or p is greater than length of main string (l), then goto step 6 otherwise goto step 7 .
6. print ie substring out of the main string position, goto step 13.
7. input the substring, s1.
8. l1=string length of s1.
9. if l + l1 > L then go to step 10 otherwise go to step 11.
10. substring cant be inserted, because it is too long goto step 13.
11. call insert(s, p, s1)
12. print After string insertion the main string is: s.
13. stop.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define L 80
#define P 40
main()
{
int p,l,i,l1;
char s[L],s1[P];
void inst(char s[],int p,char s1[]);
clrscr();
printf("Enter the Main string:\n");
scanf("%[^\n]",s);
l=strlen(s);
printf("The main string is \n%s",s);
printf("\nPosition of the substring to be inserted:\n");
scanf("%d",&p);
if((p<0)||(p>l))
printf("Substring position is out of the main string.");
else { getchar();
printf("Enter substring:\n");
scanf("%[^\n]",s1);
l1=strlen(s1);
if((l+l1)>L)
printf("\nstring can't be inserted because too long.");
else
{
inst(s,p,s1);
printf("\nAfter string insertion:\n");
printf("%s",s);
}
}
getch();
}/*End of main functioin*/
Output:
Enter the Main string:
Java is oops language
The main string is
Java is oops language
Position of the substring to be inserted:
8
Enter substring:
an
After string insertion:
Java is an oops language
Flowchart:
Start
Input main string: s
l=strlen(s)
print sub string: s1
Input substring p
Yes
No
Input substring s1
Print subs teing of a
main string
l1=strlen(s1)
Yes
If l +l1>l
No
Insert(s, p, s1)
Print string cant
be inserted
Print after
insertion: s
Stop
Last=last - 1
P1 = l (p - 1)
S(last) = s[i]
Last = last 1; l = l - 1
P1 = p1 - 1
Yes
While(p!=0)
N0
K=0
For i=p to p+ l1
No
Yes
S[i]= s1[k]
K=k+1
return
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define ML 80
main()
{
char line[ML];
int p,l,n,pos,nc,na,k,i;
clrscr();
printf("Enter the line of text:");
scanf("%[^\n]",line);
l=strlen(line);
Output:
1) Enter the line of text: ABJ KALAM IS A FORMER PRECIDENT
Enter the position of the starting character
14
Enter No of characters to be deleted ie should be <=17
18
The text is deleted from 14 till the end.
After deletion the string: ABJ KALAM IS A
2) Enter the line of text: PRASAD BV
Enter the position of the starting character
6
Enter No of characters to be deleted ie should be <=3
2
After Deletion the string is: PRASADV
Flowchart:
Start
Input a line of text line
L=strlen(line)
Input position of selecting character
pos
Input No of characters to be deleted
nc
na = l pos
Yes
If nc > na
No
P=pos + nc
K=p
For i=1 to l - k
Yes
Line[pos] = line[p]
Line[pos+]=\0
Pos = pos + 1
P=p+1
Line[pos] = \0
Print After deletion of a line
stop
No
Exercise 8:
a)Write a C Program to find the factorial of given number use both recursive and
non-recursive functions.
i) To find the factorial of a given integer.
ii) To find the GCD (greatest common divisor) of two given integers.
iii) To solve Towers of Hanoi problem.
Non-Recursive Approach:
Algorithm:
(1) Input a +ve integer No n.
(2) Check whether n is +ve or not.
i)
If n is ve print cant be evaluated the factorial goto step (4).
ii)
Else calculate fact i.e., n! call function fact(n).
(3) Print factorial of n.
(4) Stop.
Algorithm for fact():
(1) f = 1
(2) Repeat i=1 to n times.
f = f * i.
(3) Return f.
Flowchart:
Flowchart: fact(n)
Start
Start
Enter a +ve no n
f=1
for i = 1 to n
Yes
if
n<0
No
Yes
f*=i
f=fact(n)
Print cant
calculate
Print f, n
Stop
Return f
No
Recursive Approach
Algorithm:
FlowChart:
Start
Input n
Print n,
fact(n)
Stop
Flowchart: fact(n)
fact(n)
If n<=1
return(1)
return(n*fact(n-1))
ii. Write a C Program to find GCD (Greatest Common Divisor) of two given integers
use both recursive and non-recursive functions.
Recursive Approach
Algorithm:
Flowchart: main()
Start
Input 2 Nos, a, b
gcd=hcf(a, b)
Print a, b, gcd
Stop
Flowchart: hcf(p, q)
hcf(p, q)
r = p-(p/q2)
If
r==0
No
Yes
return(q)
return(r)
hcf(p, q)
iii. Write a 'C' Program to solve tower of HANOI problem using both
Recursive and Non-Recursive.
Recursive Approach
Algorithm:
Flowchart:
Start
snvalue=L
invalue=C
dnvalue=R
Input Disks, n
Stop
If n!=0
No
Yes
hanoi(n-1,sndl,dndl,indl)
return
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int nvalue;
char snvalue='L',invalue='C',dnvalue='R';
void hanoi();
clrscr();
printf("Enter number of disks: ");
scanf("%d",&nvalue);
printf("\nTower of Hanoi problem with %d disks \n,nvalue");
hanoi(nvalue,snvalue,invalue,dnvalue);
printf("\n");
getch();
}
void hanoi(int n,char sndl,char indl,char dndl)
{
if(n!=0)
{
/*Move n-1 disks from starting needle to intermediate needle*/
hanoi(n-1,sndl,dndl,indl);
/*Move disk n from start to destination*/
printf("Move disk %d from %c to %c \n",n,sndl,dndl);
/*Move n-1 disks from intermediate needle to destination needle*/
hanoi(n-1,indl,sndl,dndl);
}
return;
}
Output:
Enter number of disks: 3
Tower of Hanoi problem with 3 disks
Move disk 1 from L to R
Move disk 2 from L to C
Move disk 1 from R to C
Move disk 3 from L to R
Move disk 1 from C to L
Move disk 2 from C to R
Move disk 1 from L to R
Exercise 11:
b) Write a C function that uses functions to perform the following:
i. Write a C program that displays the position or index in the string S where the
string T begins or -1 if S doesnt contain T.
Algorithm:
1.
2.
3.
4.
If found
Yes
Print string is
found
Stop
No
Print not
found
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define L 80
#define M 40
main()
{
char s[L],t[M];
char *found;
clrscr();
printf("Enter the string S ie first one\n");
scanf("%[^\n]",s);
getchar();
printf("Enter the string T to be searched ie end string\n");
scanf("%[^\n]",t);
found=strstr(s,t);
if(found)
printf("%s string found in %s string",t,s);
else
printf("Not found:-1");
getch();
}
Output:
i) Enter the string S ie first one
HELLO HOW ARE YOU
Enter the string T to be searched ie end string
L
L string found in HELLO HOW ARE YOU string
ii. Write a C program to count the lines, words and characters in a given text.
Algorithm:
1. nw = na = nc = 0.
2. Repeat the following until no character is empty.
i.
ii.
iii.
iv.
6. Stop.
Flowchart:
Start
nw = ne = nl = 0
While (1)
True
Input line
c = strlen(line)
Yes
If c= =0
No
nc = nc + c
No
for i = 0 to c
Yes
If line[i]=space
or line[i]=\0
No
Yes
nw = nw + 1
nl = nl + 1
Program:
#include<stdio.h>;
#include <ctype.h>c
#include<conio.h>
#include<string.h>
#define L 80
main()
{
char line[L];
int ch,i,n,nw,nc,nl,c;
clrscr();
nw=nc=nl=0;
printf("Enter the text, leave one space");
printf(" between the words and press enter at the end of the text.\n");
while(1)
{
getchar();
printf("enter string OR press enter to stop\n");
scanf("%[^\n]",line);
Output:
c=strlen(line);
if(c==0)break;
Enter the text, leave one space between the words
else
and press enter at the end of the text.
nc+=strlen(line);
for(i=0;i<=c;i++)
if(isspace(line[i])||(line[i]=='\0')) Enter string OR press enter to stop
CAN I SPEAK TO Mr.PRASAD.
nw++;
Enter string OR press enter to stop
nl++;
YA THIS IS PRASAD.
strset(line,' ');
Enter string OR press enter to stop
} /*end of while*/
MAY I KNOW HOW R U
Enter string OR press enter to stop
/*clrscr();*/
THIS IS RAMU.
printf("\nNo of lines:%d",nl);
Enter string OR press enter to stop
printf("\nNo of charactors:%d",nc);
printf("\nNo of words:%d",nw);
getch();
No of lines:4
}
No of charactors:74
No of words:18
Exercise 12:
a) Write a C program to generate Pascals triangle.
Algorithm
Algorithm: main()
1.
2.
3.
4.
5.
Algorithm: ps(pas, m)
1.
2.
3.
4.
5.
6.
7.
8.
For i=0 to m
Pas[i][0] = 1.
Pas[i][i] = 1.
For j = 0 to i 1.
Pas[i][j] = pas[i-1][j-1] + pas[i-1][j].
Next j.
Next i.
Return.
Algorithm: prps()
1.
2.
3.
4.
5.
6.
For I = 0 to m.
For j = 0 to i.
Print pas[i][j].
Next j.
Next i.
Return.
Flowchart: main()
Flowchart: prps(pas, m)
Start
Start
For I = 0, I to m
False
True
Ps(pas, m)
For j = 0 to i
True
Prps(pas, m)
Stop
return
Flowchart: ps(pas, m)
Start
for i = 0 to m
False
True
pas[i][0] = 1; pas[i][i] = 1
for j = 1 to i -1
False
True
Pas[i][i] = pas[i-1][j-1] + pas[i-1][j]
return
False
Program:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#define MR 20
#define ML 30
main()
{
int m,n,pas[MR][ML];
void ps(int pas[][ML],int m);
void prps(int pas[][ML],int m);
clrscr();
printf("Enter No of Rows/Columns");
scanf("%d%d",&m,&n);
ps(pas,m);
printf("\nPascal Triangle\n");
prps(pas,m);
getch();
}/*end of main function*/
void ps(int pas[][ML],int m)
{
int i,j;
for(i=0;i<m;i++)
{
pas[i][0]=1;
pas[i][i]=1;
for(j=1;j<i;j++)
pas[i][j]=pas[i-1][j-1]+pas[i-1][j];
}/*end of for loop*/
return;
}/*end of the function*/
void prps(int pas[][ML],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<=i;j++)
printf("%3d",pas[i][j]);
printf("\n");
}
return;
}/*end of functi on*/
Output:
Enter No of Rows/Columns5 5
Pascal Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Flowchart:
Start
False
for i=1 to 10
True
k=1
for s=1 to 10-i
False
True
Print
for j=1 to i
False
True
r=k%10
Print r
k=k+1
k=k-1
False
for j=1 to i
True
k=k+1
If k==-1
No
k=10
yes
r=k%10
Print r
Print \n
Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,j,k,r,s;
clrscr();
for(i=1;i<=10;i++)
{
k=1;
for(s=1;s<=10-i;s++) printf(" ");
for(j=1;j<=i;j++)
{
r=k%10;
printf("%d",r);
k++;
}/*end of for loop2*/
k--;
for(j=1;j<i;j++)
{
k--;
if(k==-1) k=10;
r=k%10;
printf("%d",r);
}/*end of for loop3*/
printf("\n");
}/*end of for loop1*/
getch();
}/*end of main function*/
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567890987654321
Exercise 16:
a) Write a C program which copies one file to another.
Algorithm:
1. Input the program source destination file.
As command line prompt (argv)
2. Check no of parameters ie argc
If argc !=3 goto step 10
3. Copy source file name into sfname
4. Copy destination file into dfname.
5. Open the file ie source file in read mode.
6. Check source file opening of file .
If not, print file cant be opened goto step 10, otherwise goto step 7.
7. Perform till the end of the file reading the following
(i)
(ii)
Flowchart:
Start
C:\> filename sfile
dfile
If argc!=3
Yes
No
Yes
No
Open dfile
If dfpt==NULL
Yes
No
While !feop(sfpt)
No
Read c from
Yessfpt
Write c into dfpt
Close sfile
Close dfile
Stop
Program:
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<conio.h>
#define L 80
main(int argc, char *argv[])
{
FILE *sfpt,*dfpt;
char c;
char sfname[L],dfname[L];
clrscr();
if(argc!=3)
{
printf("Invalid No of parameters.");
exit(1);
}
strcpy(sfname,argv[1]);
strcpy(dfname,argv[2]);
sfpt=fopen(sfname,"r");
if(sfpt==NULL)
printf("File can't be opend %s",sfname);
else
{
dfpt=fopen(dfname,"w");
if(dfpt==NULL)
printf("File can't be found: %s",sfname);
else
{
while(!feof(sfpt))
{
c=getc(sfpt);
putc(c,dfpt);
}
}
}
fclose(sfpt);
fclose(dfpt);
getch();
}
b) Write a C program to reverse the first n characters in a file. (Note: The file name and n
are specified on the command line)
Algorithm:
1. Input C:\> filename, sourcefile.
2. if argc!=3 then goto step (6)
3. Copy argv[1] to sfname
4. Open sfname in read mode
5. if sfpt = = NULL
6. Print file not opened, otherwise goto step (7)
7. Convert string to integer ie i = a to i(argv[2])
8. Read source file buffer ie sfpt into text up to I no of characters.
9. text[n] = \0( NULL Character assignment)
10. j = 1
11. rev[j] = \0
12. j = j 1
13. for i = 0 to l
a. rev [j] = text[i]
b. j = j l
14. Print reversed string : rev
15. Close file sfpt
16. Stop.
Flowchart:
Start
Yes
No
Copy argv[1] to sfname
Convert argv[2]
No to
integer
Yes
n = fread(text, l, sfpt)
text[n]=\0
l=length(text)
j=1
L=length(text)
rev[j]=\0
j=j+
1
False
L=length(text)
for
i=0;i<l;i++
True
rev[i] = text[i]
Print reversed
string rev
j--
Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>
#define L 800
main(int argc, char *argv[])
{
int l,i,j;
char text[L];
char rev[L];
FILE *sfpt;
char c;
char sfname[L];
int n;
clrscr();
if(argc!=3)
{
printf("Invalid No of arguments.\n");
exit(0);
}
strcpy(sfname,argv[1]);
sfpt=fopen(sfname,"r");
if(sfpt==NULL)
{
printf("File not found.\n");
exit(1);
}
else{
i=atoi(argv[2]);
n=fread(text,1,i,sfpt);
text[n]='\0';
l=strlen(text);
j=l;
rev[j]='\0';
j--;
for(i=0;i<l;i++)
{
rev[j]=text[i];
j--;
}
printf("Reversed String :\n %s",rev);
fclose(sfpt);
}
/*getch();*/
}