C Lab Manual
C Lab Manual
1) Write an Algorithm, flowchart and program to swap the two given numbers
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. Input a,b #include<conio.h>
3. Compute a = a+b main()
4. Compute b=a-b {
5. Compute a = a-b int a,b;
6. Print “After swapping” clrscr();
7. Print a,b printf("\n Enter A and B values");
8. Stop scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After Swapping");
printf("\n A value = %d",a);
printf("\n B value = %d",b);
getch();
}
o/p:
Enter A and B values
10
20
After Swapping
A value = 20
B value = 10
2) Write an Algorithm, flowchart and program to accept rno, name and 3 subject marks. Calculate
total, average and grade
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. read rno,nm,m1,m2,m3 #include<conio.h>
3. compute tot=m1+m2+m3 main()
4. compute avg=tot/3 { int rno,m1,m2,m3,tot;
char nm[20];
5. print tot, avg float avg;
6. if (m1<35 or m2<35 or clrscr();
m3<35) then printf("\n Enter Rno,Name and 3 subject
7. print “Grade = Fail” marks");
otherwise scanf("%d%s%d%d
8. if(avg>=75) then %d",&rno,nm,&m1,&m2,&m3);
9. Print “Grade=Distinction” tot=m1+m2+m3;
Otherwise avg=tot/3;
10. if(avg>=60 and avg<752) printf("\n Total = %d",tot);
printf("\n Average = %f",avg);
then
if(m1<35 || m2<35 || m3<35)
11. Print “First” printf("\n Grade = Fail");
Otherwise else
12. if(avg>=50 and avg<60) if(avg>=75)
then printf("\n Grade = Distinction");
13. Print “Second” else
Otherwise if(avg>=60 && avg<75)
14. if(avg>=35 and avg<50) printf("\n Grade = First");
then else
if(avg>=50 && avg<60)
15. Print “Third”
printf("\n Grade = Second");
16. Stop else
if(avg>=35 && avg<50)
printf("\n Grade = Third");
getch();
}
o/p:
Enter Rno,Name and 3 subject marks
101
Arun
67
68
69
Total = 204
Average = 68.0
Grade = First
3) Write an Algorithm, flowchart and program to accept two numbers and depending on the
choice perform its arithmetic operation
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. input a,b #include<conio.h>
main()
3. input ch { int a,b,ch;
4. if ch=1 then printf("\n Enter A and B values");
5. print a+b scanf("%d%d",&a,&b);
otherwise printf("\n Arithmetic Operations");
6. if ch=2 then printf("\n
===================");
7. print a-b printf("\n 1. Addition \n 2. Subtraction \n
otherwise 3. Multiplication \n 4. Division");
8. if ch=3 then printf("\n Select the choice[1,2,3,4]:");
9. print a*b scanf("%d",&ch);
otherwise switch(ch)
{
10. if ch=4 then case 1:printf("\n Sum of A and B =
11. print a/b %d",a+b);
otherwise break;
12. print "invalid case 2:printf("\n Diff of A and B = %d",a-
choice" b);
break;
13. stop case 3:printf("\n Product of A and B=
%d",a*b);
break;
case 4:printf("\n Div of A and B =
%d",a/b);
break;
default: printf("\n Invalid choice"); }}
o/p:
Enter A and B values
10 5
Arithmetic Operations
==================
1. Addition
2. Subtraction
3. Multiplication
4. Division
Select the choice[1,2,3,4]:3
Product of A and B = 50
4) Write an Algorithm, flowchart and program to find the factorial of given number
Algorithm Flowchart Program
1. start #include<stdio.h>
2. input n #include<conio.h>
3. set f=1 main()
4. for i=1 to n step {
int n,f=1,i;
5. f=f*i
clrscr();
6. print f printf("\n Enter N value");
7. stop scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
printf("\n Factorial of %d is
= %d",n,f);
getch();
}
o/p:
Enter N value
5
Factorial of 5 is = 120
5) Write an Algorithm, flowchart and program to find whether a given number is prime or
not
Algorithm Flowchart Program
1. Start #include <stdio.h>
2. Input n #include <conio.h>
3. for i=2 to n/2 step 1 main()
Begin {
int n, i, flag = 0;
4. if(n mod i = 0) then
clrscr();
begin printf("Enter a positive integer: ");
5. set f=1 scanf("%d", &n);
6. break for(i = 2; i <= n/2; ++i)
end {
end if(n%i == 0)
7. if n=1 then {
8. print “1 is neither prime nor flag = 1;
composite” break;
otherwise }
begin }
9. if f=0 then if (n == 1)
printf("1 is neither prime nor
10. print n, “ is prime No.” composite.");
otherwise else
11. print n,” is not prime No.” {
end if (flag == 0)
12. stop printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
getch();
}
o/p:
Enter a positive integer:
7
7 is a prime number
6) Write an Algorithm, flowchart and program to find whether the given number is
palindrome or not
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. Input n Start #include<conio.h>
3. x=n main()
4. while(n<>0) do {
begin int n,n,r=0,x;
Input n
5. compute t=n mod 10 clrscr();
6. compute r=r*10+t printf("\n Enter N value");
7. compute n= n /10 scanf("%d",&n);
end x=n x=n;
8. if(x=r) then while(n!=0)
9. print “Palindrome No.” {
otherwise t=n%10;
10. print “Not Palindrome t = n mod 10 r=r*10+t;
While r = r * 10 +t
No.” (n!=0)do n=n/10;
N = n / 10
11. stop }
if(x==r)
printf("\n Palindrome
No.");
else
If r=n Palindrome
printf("\n Not Palindrome
then No. No.");
getch();
}
o/p:
Enter N value
Not Palindrome 121
Palindrome No.
Stop
7) Write an Algorithm, flowchart and program to find whether the given number is
Armstrong no. or not.
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. Input n Start #include<conio.h>
3. Set x=n main()
4. While(n<>0) do {
Begin Input n int n,n,s=0,x;
5. Compute t=n mod 10 clrscr();
6. Compute s=s+(t*t*t) printf("\n Enter N value");
7. Compute n=n/10 scanf("%d",&n);
End x=n x=n;
8. If(x=s) then while(n!=0)
9. Print “Armstrong No.” {
Otherwise t=n%10;
t = n mod 10
10. Print “Not Armstrong No.” While s = s +(t*t*t)
s=s+(t*t*t);
11. stop (n!=0)do n=n/10 n=n/10;
}
if(x==s)
printf("\n Armstrong
No.");
else
If s=x Armstrong printf("\n Not Armstrong
then No. No.");
getch();
}
o/p:
Enter N value
Not Armstrong 153
Armstrong No.
Stop
8) Write an Algorithm, flowchart and program to sort elements in array by using Bubble
Sort technique
Algorithm Flowchart Program
1. Start #include <stdio.h>
2. Input n #include <conio.h>
3. for i=0 to n-1 step 1 main() {
4. input a[i] int a[100], n, i, j, t;
5. for i=0 to n-1 step 1 clrscr();
begin printf("Enter N value: ");
6. for i=0 to n-1 step 1 scanf("%d", &n);
begin printf(“\n Enter %d values in
7. if a[i] > a[j] then Array”,n);
begin for(i = 0; i < n; ++i)
8. t=a[i] scanf(“%d”,&a[i]);
9. a[i]=a[j] for(i=0;i<n;i++){
10. a[j]=t for(j=i+1;j<n;j++) {
end if(a[i]>a[j]) {
end t=a[i];
end a[i]=a[j];
11. print “After Sorting” a[j]=t ; }
12. for i=0 to n-1 step 1 }
13. print a[i] }
14. stop printf(“\n After sorting”);
for(i=0;i<n;i++)
printf(“%d ”,a[i])
getch();
}
o/p:
Enter N value
5
Enter 5 values in Array
4 7 1 3 2
After Sorting
1 2 3 4 7
9) Write an Algorithm, flowchart and program search a given value in array by using
binary search technique
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. input n #include<conio.h>
3. for i=0 to n-1 step 1 main(){
begin int a[100],n,i,j,mid,low=0,high,s,f=0;
printf("\n Ente N value");
4. for j=0 to n-1 step 1
scanf("%d",&n);
begin printf("\n Enter %d values in Ascending
5. input a[i][j] Order",n);
end for(i=0;i<n;i++)
end scanf("%d",&a[i]);
6. for i=0 to n-1 step 1 printf("\n Enter the value to Search");
begin scanf("%d",&s);
7. for j=0 to n-1 step 1 high=n-1;
while(low<=high) {
begin
mid=(low+high)/2;
8. input b[i][j] if(a[mid]<s)
end low=mid+1;
end else
9. for i=0 to n-1 step 1 if(a[mid]>s)
begin high=mid-1;
10.for j=0 to n-1 step 1 else
begin if(a[mid]==s){
11.compute c[i][j]=a[i][j]+b[i] f=1;
[j] printf("\n Value Found");
break;
end
} }
end if(f==0)
12.for i=0 to n-1 step 1 printf("\n Value Not Found");
begin }
13.for j=0 to n-1 step o/p:
begin Enter N value 5
14.print c[i][j] Enter 5 values in Ascending order
3 6 9 43 67
end Enter the value to search
end 9
15. stop Value found
10) Write an Algorithm, flowchart and program to perform addition matrix
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. input n #include<conio.h>
3. for i=0 to n-1 step 1 main(){
begin int a[10][10],b[10][10],c[10][10],n,i,j;
printf("\n Enter N value");
4. for j=0 to n-1 step 1
scanf("%d",&n);
begin printf("\n Enter %d x %d values in A
5. input a[i][j] Array",n,n);
end for(i=0;i<n;i++)
end {
6. for i=0 to n-1 step 1 for(j=0;j<n;j++)
begin {
7. for j=0 to n-1 step 1 scanf("%d",&a[i][j]);
}
begin
}
8. input b[i][j] printf("\n Enter %d x %d values in B
end Array",n,n);
end for(i=0;i<n;i++)
9. for i=0 to n-1 step 1 {
begin for(j=0;j<n;j++)
10.for j=0 to n-1 step 1 {
begin scanf("%d",&b[i][j]);
}
11.compute c[i][j]=a[i][j]+b[i]
}
[j] for(i=0;i<n;i++) {
end for(j=0;j<n;j++) {
end c[i][j]=a[i][j]+b[i][j];
12.for i=0 to n-1 step 1 }
begin }
13.for j=0 to n-1 step printf("\n Resultant Addition Matrix");
begin for(i=0;i<n;i++) {
14.print c[i][j] for(j=0;j<n;j++) {
end printf("%d",c[i][j]);
}
end
printf("\n");
15. stop }
}
o/p:
Enter N value
2
Enter 2 x 2 values in A Array
1 2
3 4
Enter 2 x 2 values in B Array
5 6
7 8
Resultant Addition Matrix
6 8
10 12
o/p:
Enter N value
4
Enter 4 Records
Enter Rno,Name,Course and Fees
101
raju
bca
25000
Enter Rno,Name,Course and Fees
102
siva
bca
25000
Enter Rno,Name,Course and Fees
103
saritha
bca
30000
Enter Rno,Name,Course and Fees
104
sami
bca
32000
Rno Name Course Fees
================================
101 raju bca 25000
102 siva bca 25000
103 saritha bca 30000
104 sami bca 32000
13) Write an Algorithm, flowchart and program to calculate sum of N natural numbers
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. Read n #include<conio.h>
3. X=sum(n) main()
4. Print x {
5. Stop int n,x;
Function sum(n) clrscr();
begin printf(“\n Enter N value”);
1. If(n=0) then scanf(“%d”,&n);
2. s=0 x=sum(n);
otherwise printf(“\n Sum = %d”,x);
3. s=n+sum(n-1) getch();
4. return s }
5. end sum(int n)
{
int s=0;
if(n==0)
s=0;
else
s=n+sum(n-1);
return s
}
o/p:
Enter N value
5
Sum = 15
14) Write an Algorithm, flowchart and program to create text file and write characters
and read characters from file.
Algorithm Flowchart Program
1. Start #include<stdio.h>
2. File fp #include<conio.h>
3. fp=open("data.txt","write" main()
) {
4. fp.write(c) FILE *fp;
5. stop char c;
fp=fopen("data.txt","w");
while((c=getchar())!=EOF){
fp.putc(fp,c);
}
fp.close();
printf("\n File Created");
fp=open("data.txt","r");
printf("\n Contents of File are:\n");
while((c=fgetc(fp))!=EOF){
putchar(c);
}
fclose(fp);
}
o/p:
My name is Kiran Kumar
I am a student Sri Hari Degree
Kadapa
^z
File Created.
Contents of File are:
My name is Kiran Kumar
I am a student Sri Hari Degree
Kadapa
15) Write an Algorithm, flowchart and program to create file and write student
information and read them from file.
Algorithm Flowchart Program
1. start #include<stdio.h>
2. Record stud #include<conio.h>
main()
begin {
rno,fees struct stud
nm,co {
end int rno,fees;
3. do char nm[20],co[10];
}x;
begin FILE *fp;
4. print 1. Adding Records int ch=0;
print 2. Reding Records do
print 3. Exit {
5. print "Select the choice:" printf("\n File Operations ");
printf("\n
6. input ch ======================");
7. if ch=1 then printf("\n 1. Adding Records \n 2. Read
begin Records \n 3. Exit");
printf("\n Select the Choice[1,2,3]:");
fp=open("student.dat","writi scanf("%d",&ch);
if(ch==1)
ng") {
input fp=fopen("student.dat","a+b");
rec.rno,rec.nm,rec.co,rec.fe printf("\n Enter Rno,Name,Course and
es Fees"); scanf("%d%s%s
fp.write(rec) %d",&x.rno,x.nm,x.co,&x.fees);
fwrite(&x,sizeof(x),1,fp);
close(fp) fclose(fp);
end }
otherwise else
8. if ch=2 then
begin if(ch==2)
{
fp=open("student.dat","reading") fp=fopen("student.dat","rb");
while(fp<>Eof) printf(“\n Rno \t Name \t Course \t
begin Fees”);
printf(“\n
print =======================”);
rec.rno,rec.name,rec.co,rec. while(fread(&x,sizeof(x),1,fp)!=0)
fees {
end printf("\n%d\t%s\t%s\t
%d",x.rno,x.nm,x.co,x.fees);
end }
close(fp) fclose(fp);
otherwise }
9. if ch=3 then else
10. exit if(ch==3)
printf("\n Thank You for Using Me..
11. while(ch>=1 and Bye");
ch<=3) }while(ch>=1 && ch<=3);
12. stop getch();
}
o/p:
File Operations
==============
1. Adding Records
2. Reading Records
3. Exit
Select the choice[1,2,3]:
1
Enter Rno,Name,Course and Fees
101
Kiran
bca
25000
File Operations
=================
1. Adding Records
2. Reading Records
3. Exit
Select the choice[1,2,3]:
1
Enter Rno,Name,Course and Fees
102
siva
bca
25000
File Operations
=================
1. Adding Records
2. Reading Records
3. Exit
Select the choice[1,2,3]:
2
Rno Name Course Fees
101 kiran bca 25000
102 siva bca 25000