0% found this document useful (0 votes)
5 views

PC Lab Manual

Uploaded by

bhavya reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PC Lab Manual

Uploaded by

bhavya reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Programming C

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;

print " for Radius"


read (Radius)
c=2*pi*r;
print c
a=pi*r*r;
// 2) To Find biggest of 3 numbers print a
#include<stdio.h>
void main()
{
Stop
int A,B,C;
printf("Enter any 3 Values \n");
scanf("%d%d%d",&A,&B,&C);
if(A>B)
{
if(A>C)
printf("A is Big");
else
printf("C is Big");

}
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;

print for A,B,C

read A,B,C

No if A>B Yes

Yes
No Yes No if A>C
if B>C

print "C is Big" print "B is Big" print "C is Big"


print "A is Big"

Stop

// 3) Program to demonstrate library functions in math.h


#include<stdio.h>
#include<math.h>
int main()
{
float n;
printf("Enter a number:\n");
scanf("%f",&n);
printf("The square root of the given number is:%f\n",sqrt(n));
printf("The absolute value of the given number is:%f\n",fabs(n));
printf("The cube of the given numberis:%f\n",pow(n,3));
printf("The floor of the given number is : %f\n",floor(n));
printf("The ceil of the given number is : %f\n",ceil(n));
return 0;
}

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)

print (f) f=f*i--;

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

print "The Fibinacii series are "

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

m=n; sum=0; rev=0;

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;

print"Enter numbers and press 999 to stop the input"

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

// 9) Program to find the roots of quadratic equation (demonstration of switch-case statement)

#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;

print for a, b, c of quadratic equation (aX^2 + bX + c)

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

Stop case 0 Yes


No root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);

print "root1, root2"


Yes
case 1 A
No root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);

print (root1, imaginary, root2, imaginary);


Yes
case 2 A
root1 = root2 = -b / (2 * a);

print "root1, root2"

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"

printf("Entered element are: \n"); No Yes


for(i=0;i<number;i++) for(i=0;i<number;i++)
printf("%d ",a[i]); print"Enter Elements of the array" scan(a[i])

for(i=0;i<number;i++) No Yes
{ for(i=0;i<number;i++) array"dup[i] = -1;

for(j = i+1; j < number; j++) Yes


print(a[i]);
No
{ for(i=0;i<number;i++)

if(a[i] == a[j]) No Yes


{ print"Enter Elements of the array"
for(j = i+1; j < number; j++)

for(k = j; k <number; k++) No Yes


if(a[i] == a[j])

{ 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 GetGCD ( int x, int y)


{
while (y != 0)
{
if ( x > y)
x = x - y;
else
y = y - x;
}
return x;
}

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

int x, y, GCD = 0; GetGCD

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

print"Enter the string”

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;

int findNormal(int mat[10][10], int n) for (i=0;i<n;i++)


{
return(sqrt(sum)); for (j=0; j<n; j++)
int i,j,sum=0;
for (i=0;i<n;i++)
for (j=0; j<n; j++)
A sum+=mat[i][j]*mat[i][j];

sum+=mat[i][j]*mat[i][j];
return(sqrt(sum)); findTrace

int findTrace(int mat[10][10], int n) int i,sum = 0;


{
int i,sum = 0; for (i=0;i<n;i++)
for (i=0; i<n; i++) return sum; sum += mat[i][i];
sum += mat[i][i];
return sum;
} B
Start
int main()
{
int i,j,mat[10][10],m,n; int i,j,mat[10][10],m,n;
printf("Enter the order of matrix \n");
scanf("%d%d",&m,&n); for order of Matrix

if(m==n) for order of Matrix


{ No Yes
printf("Enter the Matrix Elements \n"); if(m==n)

for(i=0;i<n;i++) print" for Matrix Elements "


print"TRACE AND NORM IS NOT POSSIBLE FOR THE ORDER"

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));

printf("TRACE AND NORM IS NOT POSSIBLE FOR THE ORDER \n");


return 0;
K
}

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>

int main() int a[10],n;


int large1,large2,i;
{
int a[10],n; printf("enter
print("enter number
numberof
of elements
elementsyou
you want
wantin
in array
array:");
:");

int large1,large2,i; scan(n);

Print for "enter elements"


printf("enter number of elements you want in array :");
scanf("%d",&n); for (i = 0; i < n; i++)
printf("enter elements");
for (i = 0; i < n; i++) large1 = a[0]; scan(a[i]);

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)

large1 = a[i]; for (i = 0; i < n; i++) large1 = a[i];

if (a[i] > large2 && a[i] < large1)


print( large1, large2);
large2 = a[0];
for (i = 1; i < n; i++) Stop large2 = a[i];

if (a[i] > large2 && a[i] < large1)


large2 = a[i];
printf("First and second largest number is %d and %d \n", large1, large2);
return 0;
}

// 6) Program to perform addition and subtraction of Matrices Start


#include<stdio.h>
void main()
{ int SUB[10][10],ADD[10][10],M1[10][10],M2[10][10];
int i,j,R1,C1,R2,C2;
int SUB[10][10],ADD[10][10],M1[10][10],M2[10][10];
int i,j,R1,C1,R2,C2; Print for R1,C1
printf(" ENTER THE Rows & Cols of MATRIX1 \n"); Read R1,C1
scanf("%d%d",&R1,&C1); Print for R2,C2

Read R2,C2
printf(" ENTER THE Rows & Cols of MATRIX2 \n");
scanf("%d%d",&R2,&C2); if(R1==R2&&C1==C2)

print("Addition/Subtraction is not possible "); Print for MATRIX1 ELEMENTS

if(R1==R2&&C1==C2) B
Stop
for(i=0;i<R1;i++)

{ Print for MATRIX 2 ELEMENTS


for(j=0;j<C1;j++)

scan(M1[i][j])

printf(" ENTER THE MATRIX1 %d ELEMENTS\n",R1*C1); for(i=0;i<R1;i++)

for(i=0;i<R1;i++) Print for MATRIX 2 ELEMENTS


for(j=0;j<C1;j++)
scan(M2[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];

printf(" ENTER THE MATRIX2 %d ELEMENTS\n",R1*C1);


for(i=0;i<R1;i++) Print “ The Added MATRIX is “

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
}

printf(" THE added MATRIX IS \n"); Print “ The Subtracred MATRIX is “

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");
}

// 7) Program to read, display and multiply two m x n matrices using function


#include<stdio.h>
readmatrix

void readmatrix(int a[10][10],int row,int col)


{
int i,j; int i,j;
for(i=1;i<=row;i++) No Yes
R1/R2 for(i=1;i<=row;i++)
for(j=1;j<=col;j++) Yes
for(i=1;i<=row;i++)
scanf("%d",&a[i][j]); No
} scan(a[i][j]);

void promatrix(int m1[10][10],int m2[10][10],int m3[10][10],int m, int p,int n)


{
int i,j,k,sum;
for(i=1;i<=m;i++)

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

printf("Enter order of rows & Cols of Matrix 1:\n");


scanf("%d%d",&r1,&c1);
printf("Enter order of rows & Cols of Matrix 2:\n");
scanf("%d%d",&r2,&c2);
Start
if(c1!=r2)
printf("Multiplication is not possible\n"); int m1[10][10],m2[10][10],m3[10][10],r1,c1,r2,c2;

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

prodmatrix print"elements of martix3"

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);

printf("%d is a prime number",n);


else isprime
printf("%d is not a prime number",n);
} K
}
void main()
{ Stop
int n,i;
printf("Enter a number:\n");
scanf("%d",&n);
isprime(n);
}

// 9) Program to demonstrate student structure to read & display records of n students


#include <stdio.h>
struct student Start
{
char Name[50];
int roll; int i;
float marks;
} s[3]; Print "Enter a number:";

No Yes
int main() for (i = 0; i < 3; ++i)
{ print"Displaying Information”; s[i].roll = i +

int i; print("For roll number", s[i].roll);

printf("Enter information of students:\n"); A Print "Enter name: "

scan(s[i].name);

// storing information Print ("Enter marks: ");

for (i = 0; i < 3; ++i)


scan(s[i].marks);

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);

scanf("%s", s[i].Name); STOP


print(s[i].name);
printf("Enter marks: ");
scanf("%f", &s[i].marks); print(s[i].marks);

}
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;
}

// 10) Program to demonstrate the concept of nested structure


#include<stdio.h> Start
struct address
{
int houseno; struct student stud;
char street[20];
int stateno; print"Enter name and roll number of student:

};
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;
}

// 11) Program to swap two integers using call-by-value and call-by-reference.


#include<stdio.h>
void swap(int*,int*); Start
void main()
{ int a,b;
int a,b;
printf("Enter values for a and b\n"); print( for a and b);
scanf("%d%d",&a,&b); read( a and b);
printf("Before swapping:a=%d and b=%d\n",a,b);
swap(&a,&b);
printf("After swapping :a=%d and b=%d\n",a,b);
} A swap
void swap(int *x,int *y)
{
int temp;
temp=*x; STOP
*x=*y;
*y=temp;
} swap

int temp;
temp=*x;
*x=*y;
*y=temp;

// 12) Program to calculate the sum of n numbers entered by the user A


#include <stdio.h>
#include <stdlib.h>

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));

printf("Enter elements: "); No Yes


if(ptr == NULL)
for(i = 0; i < n; ++i)
{
print"Error!”
scanf("%d", ptr + i);
sum += *(ptr + i);
} print"Enter elements:
A
printf("Sum = %d", sum);
No Yes
for(i = 0; i < n; ++i)
free(ptr);
return 0; printf(sum) scan( ptr + i)
}
sum += *(ptr + i);
free(ptr);

STOP

Dept. of Computer Science & BCA Vedavathi Government First Grade College, Hiriyur

You might also like