PC Lab Manual
PC Lab Manual
Lab Manual
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 1) Program to read radius of a circle and to find area and circumference
#include<stdio.h>
#define pi 3.14
void main()
{
float r,c,a;
printf("Enter the radius of the circle:\n");
scanf("%f",&r);
c=2*pi*r;
printf("The circumference of the given radius is %f\n",c);
a=pi*r*r;
printf("The area of the given radius is %f\n",a);
Start
}
float r,c,a;
}
else
{
if(B>C)
printf("B is Big");
else
printf("C is Big");
}
}
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
Start
int A,B,C;
read A,B,C
No if A>B Yes
Yes
No Yes No if A>C
if B>C
Stop
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
Start
float n;
print for n
read(n)
print(sqrt(n))
print(fabs(n));
print (pow(n,3));
print(floor(n));
print(ceil(n));
Stop
// 4) Program to generate the factorial of a given number
#include<stdio.h>
void main()
{
int n,f=1;
printf("Enter a Number \n");
scanf("%d",&n);
while(n>=1)
f=f*n--;
printf("The Factorial of given number is %d\n",f);
}
Start
int n,f=1;
print for n
read for n
while(n>=1)
Stop
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 5) Program to generate n fibonacii sequence
#include<stdio.h>
void main()
{
int l,p=0,q=1,r;
printf("Enter the upper limit \n");
scanf("%d",&l);
printf("The Fibinacii series are :\n %d ",p);
while(q<=l)
{
printf("%d ",q);
r=p+q;
p=q;
q=r;
}
} Start
int l,p=0,q=1,r;
print for l
read for l
No Yes
while(q<=l)
Stop
print(q)
r=p+q;
p=q;
q=r;
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
/* 6) Program to read a number,
find the sum of the digits,
reverse the number and check
it for palindrome */
#include<stdio.h>
void main()
{
int n,m,r,sum,rev;
printf("Enter a number:\n");
scanf("%d",&n);
m=n;
sum=0;
rev=0;
while(n!=0)
{
r=n%10;
sum=sum+r;
rev=rev*10+r;
n=n/10;
}
printf("\nThe sum of the digits in the given integer is %d",sum); Start
printf("\nThe reverse of the given number is %d",rev);
if(m==rev)
printf("\n Is a polyndrome number\n");
else int n,m,r,sum,
printf("\n Is NOT a polyndrome number\n"); rev;
}
print for n
read for n
No Yes
while(n!=0)
print (Sum)
print (rev)
r=n%10; sum
No Yes =sum+r; rev=
if(m==rev)
rev*10+r; n=
print"not polyndrome" print"polyndrome number" n/10;
Stop
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
/* 7) Program to read numbers from keyboard continuously till the user presses 999 and to find the
sum of only positive numbers */
s
#include<stdio.h>
void main()
{
int n,sum=0;
printf("Enter numbers and press 999 to stop the input:\n");
do
{
scanf("%d",&n);
if((n>0)&&(n!=999))
sum=sum+n;
}while(n!=999);
printf("Sum of numbers = %d \n",sum);
}
Start
int n ,sum=0;
read for n
Yes
if((n>0)&&(n!=999))
No sum=sum+n;
Yes
No while(n!=999)
Stop
/* 8) Program to read percentage of marks and to display appropriate message (Demonstration of else-
if ladder) */
#include<stdio.h>
void main()
{
int per;
printf("Enter the percentage obtained by the student:\n");
scanf("%d",&per);
if(per>=80)
printf("Distinction\n");
else if(per>=60)
printf("First class\n");
else if(per>=50)
printf("Second class\n");
else if(per>=40)
printf("Pass\n");
else
printf("Fail");
}
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
Start
int per;
print for per
read(per)
False True
if(per>=80)
False
if(per>=60) True
True print"Distinction"
False
if(per>=50) print"First Class"
False
True
if(per>=40) print"Second Class"
print"Pass"
print"Fail"
Stop
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
int n;
printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
scanf("%f%f%f", &a, &b, &c);
if(a==0)
{
printf("It is not a quadratic equation!");
exit(0);
}
discriminant = (b * b) - (4 * a * c);
if(discriminant>0)
n=0;
else if(discriminant<0)
n=1;
else
n=2;
switch(n)
{
case 0:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct and real roots exists: %.2f and %.2f \n",root1, root2);
break;
case 1:
root1 = root2 = -b / (2 * a);
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f \n",root1, imaginary, root2,
imaginary);
break;
case 2:
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f \n", root1, root2);
break;
}
}
Start
float a, b, c;
float root1, root2, imaginary;
float discriminant;
int n;
scan(a, b, c);
No Yes
if(a==0)
discriminant = (b * b) - (4 * a * c);
print "It is not a quadratic equation!"
No Yes A
if(discriminant>0)
No
n=0;
if(discriminant<0)
n=2;
Yes
n=1;
Yes
A switch
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
/* 10) Program to read marks scored by a student and find the sum, average and result using switch
case. */
#include<stdio.h>
int main() Start
{
int s1,s2,s3,total;
float score; int s1,s2,s3,total;
printf("Enter Marks of Sub1, Sub2, Sub3 (0-100) \n"); float score;
scanf("%d%d%d",&s1,&s2,&s3);
total=s1+s2+s3; print for Sub1, Sub2, Sub3 (0-100)
score=total/3;
printf("Obtained Total: %d Score: %f & ",total,score); scan(s1,s2,s3);
switch((int)score/10)
{ total=s1+s2+s3;
case 10: score=total/3;
case 9:
printf("Grade: A\n"); print (total,score)
break;
A No Yes
switch((int)score/10)
case 8:
printf("Grade: B\n"); Yes
case 10,9
break; Stop No print "Grade A"
case 7: A
case 8Yes
printf("Grade: C\n"); No
print "Grade B"
break;
A
case 7 Yes
case 6: print "Grade C"
printf("Grade: D\n"); A
Yes
break; case 6
No
print "Grade D"
case 5: A
case 5Yes
printf("Grade: E\n"); No
break; print "Grade E"
Yes A
default
default: print "Grade F"
printf("Grade: F\n"); A
break;
}
return 0;
}
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 11) Program to remove Duplicate Element in a single dimensional Array
#include<stdio.h>
#include<stdlib.h> Start
void main()
{
int a[50],i,j,k, count = 0, dup[50], number; int a[50],i,j,k, count = 0,
dup[50], number;
printf("Enter size of the array\n");
scanf("%d",&number);
printf("Enter Elements of the array:\n");
for(i=0;i<number;i++) print for size of array
{
scanf("%d",&a[i]); scan(number);
dup[i] = -1;
} print"Enter Elements of the array"
for(i=0;i<number;i++) No Yes
{ for(i=0;i<number;i++) array"dup[i] = -1;
{ Yes
No for(i=0;i<number;i++)
No for(k = j; k <number; Yes
a[k] = a[k+1]; print(a[i]);
k++)
} j--; number--;
a[k] = a[k+1];
j--; Stop
number--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:\n");
for(i=0;i<number;i++)
printf("%d ",a[i]);
}
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 12) Program to find GCD of two integers using function
#include <stdio.h>
int main()
{
int x, y, GCD = 0;
printf ( " Enter any two numbers \n ");
scanf("%d%d",&x,&y);
GCD=GetGCD(x,y);
printf ( " GCD of the two numbers %d and %d is %d\n", x, y, GCD);
return 0;
}
Start
Yes
No while (y != 0)
print for 2 numbers No Yes
if(a[i] == a[j])
return x;
scan(x,y); y = y - x; x = x - y;
A GCD= GetGCD
A
print(x, y, GCD)
Stop
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
Part B
// 1) Program to perform all bitwise operations on two integers.
Start
#include <stdio.h>
int main()
{ int i,a,b;
int i,a,b; print for values
printf("ENTER 2 Integer value:");
scanf("%d%d",&a,&b);
printf("\nAND Operation = %d \n", a&b); scan(a,b);
printf("OR Operation = %d \n", a|b); print(a&b)
printf("XOR Operation = %d \n", a^b);
printf("Output = %d\n",~35); print(a|b)
printf("Output = %d\n",~-12);
for (i=0; i<=2; ++i) print(a^b);
printf("Right shift by %d bit: %d\n", i, a>>i); print(~35);
for (i=0; i<=2; ++i)
printf("Left shift by %d bit: %d\n", i, a<<i);
return 0;
} Yes
for (i=0; i<=2; ++i)
No
No Yes
for (i=0; i<=2; ++i) print(i, a>>i)
print(i, a<<i)
STOP
/* 2) Program to read a string and to find the number of alphabets, digits, vowels, consonants, spaces
and special characters.*/
#include<stdio.h>
#include<string.h>
void main()
{
char s[1000];
int i,alphabets=0,digits=0,specialchar=0;
int blankspaces=0,vowels=0,consonents=0;
printf("Enter the string:\n");
fgets(s,1000,stdin);
for(i=0;s[i]!=0;i++)
{
if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
{
alphabets++;
if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')
vowels++ ;
else
consonents++;
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
}
else if(s[i]>=48&&s[i]<=57)
digits++;
else if(s[i]==32)
blankspaces++;
else
specialchar++;
}
printf("The number of alphabets are %d\n",alphabets);
printf("The number of vowels are %d\n",vowels);
printf("The number of consonents are %d\n",consonents);
printf("The number of digits are %d\n",digits);
printf("The number of blankspaces are %d\n",blankspaces);
printf("The number of special characters are %d\n",specialchar);
}
Start
fgets(s,1000,stdin);
No Yes
for(i=0;s[i]!=0;i++)
A No If(
(s[i]>=65&&s[i]<=90)
Yes
||(s[i]>=97&&s[i]<=122))
No Yes
if(s[i]>=48&&s[i]<=57)
digits++; alphabets++;
No if(s[i]=='a'
||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||
Yes
s[i]=='I'||s[i]=='o'|| s[i]=='O'||s[i]=='u'||
s[i]=='U')
consonents++;
vowels++ ;
No Yes
if(s[i]==32)
specialchar++;
blankspaces++;
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
A
print(alphabets);
print(vowels);
print(consonents);
print(digits);
print(blankspaces);
print(specialchar);
STOP
// 3) Program to find the length & reverse of a string without using built in function
#include<stdio.h> Start
void main()
{
char str[50]; char str[50];
int i,length=0; int i,length=0;
printf("Enter a string\n");
scanf("%s",str); print for string
for(i=0;str[i]!='\0';i++)
length++; scan(str)
printf("The length of the string is %d\n",length);
for(i=length;i>=0;i--)
printf("%c",str[i]); No Yes
} for(i=0;str[i]!='\0';i++)
print(length) length++;
No Yes
for(i=length;i>=0;i--)
print(str[i])
STOP
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 4) Program to read, display and to find the trace and norm of a matrix in order M X N
#include<stdio.h> findNormal
#include<math.h>
const int MAX = 100; int i,j,sum=0;
sum+=mat[i][j]*mat[i][j];
return(sqrt(sum)); findTrace
for(j=0;j<n;j++) K Yes
No
scanf("%d",&mat[i][j]); Stop for (i=0;i<n;i++)
printf("Trace of Matrix = %d\n",findTrace(mat,n)); No Yes
for (j=0; j<n; j++)
printf("Normal of Matrix = %d\n",findNormal(mat,n)); findTrace(mat,n));
} scan(mat[i][j]);
else findNormal(mat,n));
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 5) Program to find first and second largest element in an array.
Start
#include <stdio.h>
scanf("%d", &a[i]);
large1 = a[0]; for (i = 0; i < n; i++)
for (i = 0; i < n; i++)
large2 = a[0];
if (a[i] > large1) if (a[i] > large1)
Read R2,C2
printf(" ENTER THE Rows & Cols of MATRIX2 \n");
scanf("%d%d",&R2,&C2); if(R1==R2&&C1==C2)
if(R1==R2&&C1==C2) B
Stop
for(i=0;i<R1;i++)
scan(M1[i][j])
for(j=0;j<C1;j++)
scanf("%d",&M1[i][j]);
for(i=0;i<R1;i++)
for(j=0;j<C1;j++)
ADD[i][j]=M1[i][j]+M2[i][j];
SUB[i][j]=M1[i][j]-M2[i][j];
for(j=0;j<C1;j++) for(i=0;i<R1;i++)
scanf("%d",&M2[i][j]);
for(j=0;j<C1;j++)
A
print(ADD[i][j])
Print “ NEXT LINE “
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
for(i=0;i<R1;i++)
for(j=0;j<C1;j++)
{
ADD[i][j]=M1[i][j]+M2[i][j];
SUB[i][j]=M1[i][j]-M2[i][j]; A
}
for(i=0;i<R1;i++) for(i=0;i<R1;i++)
{ for(j=0;j<C1;j++)
for(j=0;j<C1;j++) print(SUB[i][j])
printf("%d ",ADD[i][j]); B Print “ NEXT LINE “
printf("\n");
}
printf(" THE SUBTRACTED MATRIX IS \n");
for(i=0;i<R1;i++)
{
for(j=0;j<C1;j++)
printf("%d ",SUB[i][j]);
printf("\n");
}
}
else
printf("Addition/Subtraction is not possible \n");
}
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
for(j=1;j<=p;j++)
promatrix
{
sum=0;
for(k=1;k<=n;k++)
sum=sum+m1[i][k]*m2[k][j]; int i,j,k,sum;
No Yes
m3[i][j]=sum; M for(i=1;i<=m;i++)
} No Yes
for(j=1;j<=p;j++)
}
sum=0;
void printmatrix(int m[10][10],int row,int col) No Yes
for(k=1;k<=n;k++)
{
sum=sum+m1[i][k]*m2[k][j];
int i,j; m3[i][j]=sum;
for(i=1;i<=row;i++)
prIntmatrix
{ for(j=1;j<=col;j++)
printf("%6d",m[i][j]);
printf("\n");
} int i,j;
} P1/P2 No Yes
/P3 for(i=1;i<=row;i++)
No Yes
void main() for(j=1;j<=col;j++)
{ print(m[i][j]);
int m1[10][10],m2[10][10],m3[10][10],r1,c1,r2,c2; Print Next line
else
{ Print for R1 C1of Matrix
printf("Enter %d elements for martix 1: \n",r1*c1);
readmatrix(m1,r1,c1);
Scan ( R1, C1)
printf("Enter %d elements for matrix 2: \n",r2*c2);
readmatrix(m2,r2,c2); Print for R2 C2of Matrix
promatrix(m1,m2,m3,r1,c2,c1);
printf("The entered matrix 1 is:\n");
Scan ( R2, C2)
printmatrix(m1,r1,c1);
printf("The entered matrix 2 is:\n");
printmatrix(m2,r2,c2); No Yes
printf("The Multiplied matrix is:\n"); if(c1!=r2)
printmatrix(m3,r1,c2);
print"Multiplication is not possible"
}
}
K
print"Enter for elements for martix 1"
readmatrix
print"elements of martix 1"
R1
print"Enter for elements for martix 2" printmatrix
P1
print"elements of martix 2"
readmatrix
R2 printmatrix
P2
M printmatrix
K
P3
STOP
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
// 8) Program to check a number for prime by defining isprime( ) function
#include<stdio.h>
void isprime(int n)
{ isprime
int flag=0,i;
for(i=2;i<=n/2;i++) int flag=0,i;
{ No Yes
A for(i=2;i<=n/2;i++)
if(n%i==0) No Yes
{ No Yes
if(n%i==0)
flag=1;
flag=1; if(n==1)
A
break; No Yes
Start
Print "1 is neither prime nor composite";
if(flag==0)
}
} Print(“Not Prime”); print(n);
if(n==1)
printf("1 is neither prime nor composite"); int n,i;
else K Print "Enter a number:";
{
if(flag==0) scan(n);
No Yes
int main() for (i = 0; i < 3; ++i)
{ print"Displaying Information”; s[i].roll = i +
scan(s[i].name);
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
A
{
s[i].roll = i + 1; No Yes
for (i = 0; i < 3; ++i)
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter name: "); print("Roll numbern", i + 1);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 3; ++i)
{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].Name);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
};
scan(stud.roll);
struct student
{ print"Enter street name, house number and state number"
char name[30];
int roll; scanf(stud.adrs.street, &stud.adrs.houseno, &stud.adrs.stateno)
struct address adrs; /* Nested structure */
};
A
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
A
int main()
{ Print "Student detail is:";
struct student stud;
Print(stud.name, stud.roll)
printf("Enter name and roll number of student:\n");
Print(stud.adrs.street, stud.adrs.houseno, stud.adrs.stateno)
scanf("%s%d",stud.name, &stud.roll);
printf("Enter street name, house number and state number:\n");
scanf("%s%d%d",stud.adrs.street, &stud.adrs.houseno, &stud.adrs.stateno);
printf("Student detail is:\n"); STOP
printf("Name: %s\tRoll: %d\n", stud.name, stud.roll);
printf("Address:%s, House no. -%d, state: %d",stud.adrs.street, stud.adrs.houseno, stud.adrs.stateno);
return 0;
}
int temp;
temp=*x;
*x=*y;
*y=temp;
int main() {
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur
int n, i, *ptr, sum = 0; Start
printf("Enter number of elements: ");
scanf("%d", &n);
int n, i, *ptr, sum = 0;
ptr = (int*) malloc(n * sizeof(int));
Printf "Enter number of elements: ")
if(ptr == NULL) {
printf("Error! memory not allocated."); scanf(n)
exit(0);
} ptr = (int*) malloc(n * sizeof(int));
STOP
Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur