Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Fcs-Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

B V Raju Institute of Technology,

Vishnupur, Narsapur-502313 Dist. Medak.Telangana

LABORATORY MANUAL

I B.Tech. I SEM

FUNDAMENTALS OF COMPUTER SCIENCE LAB

AY-2020-21

Prepared by

Mr S. Anjanayya, Assistant Professor


Fundamentals of computer science Lab

S.No Program Name Page No

1 Algorithm to add two numbers entered by the user. 7

2 Algorithm to Find the largest number among three 7


different numbers
3 Algorithm to Roots of a quadratic equation 8
ax2 + bx + c = 0
4 Algorithm to find Factorial of a number entered 9
by the user.
5 Algorithm to Check whether a number is a prime 9
number or not
6 Algorithm to Find the Fibonacci series till term ≤ 1000. 10

7 Algorithm to Find the GCD of two numbers. 11

8 Program to find largest of n numbers 11

9 Program to find LCM of two numbers 12

10 Program to check given number is Armstrong number or 13


not
11 Program to print Armstrong numbers up to given 14
number
12 Program to check given number is magic number or not 15

13 Program to print magic numbers up to given number 15

14 Program to check given number is perfect number or not 16

15 Program to print perfect numbers up to given number 17

16 Program to check given number is prime number or not 18

17 Program to print prime numbers up to given number 18

18 Program to check given number is strong number or not 19

19 Program to print Strong number series up to given 20


number
20 Program to convert Binary to Decimal 21

21 Program to convert Decimal to Binary 21

22 Program to find an element using linear search 22

23 Program to find an element using Binary Search 23

24 Program to left rotate the elements of Array 24

Department of CSE.BVRIT
2
Fundamentals of computer science Lab

25 Program to right rotate the elements of Array 25

26 Program to print Lower triangular matrix 26

27 Program to print Upper triangular matrix 27

28 Program to find sum of each row and column of a Matrix 28

29 Print matrix elements in diagonal pattern 29

30 Print Matrix elements in reverse diagonal pattern 30

31 Program to Reverse array elements 31

32 Program to display given matrix elements in spiral form 31

33 Program to illustrate command line arguments 33

34 Program for finding the largest integer among three 33


numbers using command line arguments

35 Program to demonstrate use of variable number of 34


arguments.
36 Program to implement function which can take the 35
variable number of parameters and return their average
37 Program to demonstrate function pointer example 35

38 Program to demonstrate function pointer example 36

39 Program to demonstrate user defined header to find 37


cube of a number
40 Program to demonstrate user defined header add and 38
multiply two numbers
41 Program to demonstrate make utility 39

42 Program to demonstrate multifile program. 39

43 Program to find Length of a given string 40

44 Program to Compare two strings 41

45 Program to concatenate two strings 42

46 Program to Reverse the given string 42

47 Program to find given string is palindrome or not 43

Department of CSE.BVRIT
3
Fundamentals of computer science Lab

HARDWARE AND SOFTWARE


REQUIREMENTS

HARDWARE REQUIREMENTS:

INTEL PENTIUM 915 GV

80GB HDD

512MB DDR

SOFTWARE REQUIREMENTS:

Windows/Linux OS
Turbo C Compiler
GCC compiler
Online C Editor

Course Outcomes:
COs Description

CO1 Basic Programming Skills – Algorithms & Flowcharts


CO2 Implement C programs with conditionals and loops

CO3 Demonstrate array operations and application programs.

CO4 Demonstrate modular programs and implement multifile programs


CO5 Demonstrate string operations and application programs.

Mapping of Course outcomes (COs) to Program outcomes (POs):

Department of CSE.BVRIT
4
Fundamentals of computer science Lab

Program Outcomes:
PO1. Engineering knowledge: Apply the knowledge of mathematics,
science, engineering fundamentals, and an engineering specialization
to the solution of complex engineering problems.

PO2. Problem analysis: Identify, formulate, review research literature, and


analyze complex engineering problems reaching substantiated
conclusions using first principles of mathematics, natural sciences,
and engineering sciences.

PO3. Design/development of solutions: Design solutions for complex


engineering problems and design system components or processes
that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental
considerations.

PO4. Conduct investigations of complex problems: Use research-based


knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis of the information
to provide valid conclusions.

PO5. Modern tool usage: Create, select, and apply appropriate techniques,
resources, and modern engineering and IT tools including prediction
and modeling to complex engineering activities with an understanding
of the limitations.

PO6. The engineer and society: Apply reasoning informed by the


contextual knowledge to assess societal, health, safety, legal and
cultural issues and the consequent responsibilities relevant to the
professional engineering practice.

PO7. Environment and sustainability: Understand the impact of the


professional engineering solutions in societal and environmental
contexts, and demonstrate the knowledge of, and need for sustainable
development.

PO8. Ethics: Apply ethical principles and commit to professional ethics


and responsibilities and norms of the engineering practice.

PO9. Individual and team work: Function effectively as an individual, and


as a member or leader in diverse teams, and in multidisciplinary
settings.
PO10. Communication: Communicate effectively on complex engineering
activities with the engineering community and with society at large,
such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and
receive clear instructions.

Department of CSE.BVRIT
5
Fundamentals of computer science Lab

PO11. Project management and finance: Demonstrate knowledge and


understanding of the engineering and management principles and
apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.

PO12. Life-long learning: Recognize the need for, and have the preparation
and ability to engage in independent and life-long learning in the
broadest context of technological change.

Program Specific Outcomes:

PSO1. Foundations of Computer Science: Ability to understand the


principles of computer science. Student can possess professional skills
and knowledge of software design process.

PSO2. Applications of Computing and Innovation: Ability to use


theoretical and practical concepts in interdisciplinary domains to
provide solution to new ideas and innovations

Assessment Method:
S Lab Evaluation Criteria for Internal 30 Marks Total
.No

1 1.Attendance 2.Every Lab 3.Lab Internal Exam 30M


( 5M) program writing,
i.Program Writing(5M)
execution and
Viva(Day To Day ii.ProgramExecution(5M)
Evaluation Week
Wise)( 10M) iii.Viva(5M).

Lab Evaluation Criteria for External 70 Marks

2 External External Record(20M) Viva Voce(10M) 70M


Program Program
Writing(20M) Execution(20M)

100M

Department of CSE.BVRIT
6
Fundamentals of computer science Lab

1. Algorithm to add two numbers entered by the user.

Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop

2. Algorithm to Find the largest number among three different


numbers

Step 1:
Start
Step 2:
Declare variables a,b and c.
Step 3:
Read variables a,b and c.
Step 4:
If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

Department of CSE.BVRIT
7
Fundamentals of computer science Lab

3. Algorithm to Roots of a quadratic equation ax2 + bx + c = 0

Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
r1 ← rp+j(ip)
r2 ← rp-j(ip)
Step 5: Display r1 and r2 as roots.
Step 6: Stop

Department of CSE.BVRIT
8
Fundamentals of computer science Lab

4. Algorithm to find Factorial of a number entered by the user.

Step 1: Start
Step 2: Declare variables n, fact and i.
Step 3: Initialize variables
fact ← 1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: fact ← fact*i
5.2: i ← i+1
Step 6: Display fact
Step 7: Stop

5. Algorithm to Check whether a number is a prime number or not


Step 1:
Start
Step 2:
Declare variables N,i.
Step 3:
Read N from the user.
Step 4:
Initialize variables
i←2
Step 5: Repeat the steps until N%i !=0
5.1 i ← i+1
Step 6: If N! = i
Display not prime
else
Display prime
Step 7: Stop

Department of CSE.BVRIT
9
Fundamentals of computer science Lab

6. Algorithm to Find the Fibonacci series till term ≤ 1000.


Step 1: Start
Step 2: Declare variables fterm,sterm and temp.
Step 3: Initialize variables fterm ← 0 sterm ← 1
Step 4: Repeat the steps until sterm ≤ 1000
4.1: Display sterm
4.2: temp ← sterm
4.3: second_term ← sterm + fterm
4.4: fterm ← temp
Step 6: Stop

Department of CSE.BVRIT
10
Fundamentals of computer science Lab

7. Algorithm to Find the GCD of two numbers.

Step 1: Start
Step 2: Declare variables a b and c.
Step 3: Read variables a and b.
Step 4: Repeat the steps until b !=0
4.1: c =b
4.2 b = a%b
4.3 a =c
Step 5: Display a

8. Program to find largest of n numbers

int main()
{
int a,max,i,n;
printf("\nenter limit :");
scanf("%d",&n);
printf("\nenter one number : ");
scanf("%d",&max);
for(i=1;i<n;i++)
{
printf("\nenter number : ");
scanf("%d",&a);
if(a>max)
max = a;
}
printf("\nmaximum of n numbers = %d",max);
return 0;
}

Department of CSE.BVRIT
11
Fundamentals of computer science Lab

enter limit : 8
enter one number :43
enter number : 4
enter number : 76
enter number : 9
enter number : 32
enter number : 66
enter number : 5
enter number : 8

maximum of n numbers = 76

9. Program to find LCM of two numbers

int main()
{
int a,b,x;
printf("\nenter a and b values :");
scanf("%d%d",&a,&b);
x = a>b?a:b;
while(x<=a*b)
{
if(x%a == 0 && x%b ==0)
break;
x++;
}
printf("\n\nLCM = %d",x);
return 0;
}

enter a and b values : 40 70

LCM = 280

Department of CSE.BVRIT
12
Fundamentals of computer science Lab

10. Program to check given number is Armstrong number or not

int main()
{
int n,sum,rem,m;
printf("\nenter n :");
scanf("%d",&n);
m = n;
sum = 0;
while(n>0)
{
rem = n%10;
rem = rem*rem*rem;
sum = sum+rem;
n = n/10;
}
if(sum == m)
printf("\n%d is Armstrong number \n",m);
else
printf("\n%d is not an Armstrong\n",m);
return 0;
}

enter n : 153

153 is Armstrong number

Department of CSE.BVRIT
13
Fundamentals of computer science Lab

11. Program to print Armstrong numbers up to given number

int main()
{
int n,sum,rem,i,j;
printf("\nenter n : ");
scanf("%d",&n);
printf("\nthe Armstrong series numbers are\n");
for(i=1;i<=n;i++)
{
j = i;
sum = 0;
while(j>0)
{
rem = j%10;
rem = rem*rem*rem;
sum = sum + rem;
j = j/10;
}
if(i==sum)
printf("\n%d",i);
}
return 0;
}

enter n : 850

the Armstrong series numbers are

1
153
370
371
407

Department of CSE.BVRIT
14
Fundamentals of computer science Lab

12. Program to check given number is magic number or not

int main()
{
int i,n,rev,sum,temp,p;
printf("\nenter n : ");
scanf("%d",&n);
p = n;
for(sum=0;n>0;sum+=n%10,n=n/10);
n = sum;
for(rev =0;n>0;rev = rev*10+n%10,n=n/10);
temp = sum*rev;
if(temp == p)
printf("%d is magic\n",p);
else
printf("\n not " );
return 0;
}

enter n : 1729

1729 is magic

13. Program to print magic numbers up to given number

int main()
{
int i,n,temp,sum,rev,j,prod;
printf("\nenter n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j =i;
for(sum=0;j>0;sum+=j%10,j/=10);
temp = sum;
for(rev=0;temp>0;rev = rev*10+temp%10,temp/=10);
prod = sum*rev;
if(prod == i)
printf("\n%d",i);
}
return 0;
}
enter n : 2000
1
81
1458
1729

Department of CSE.BVRIT
15
Fundamentals of computer science Lab

14. Program to check given number is perfect number or not

int main()
{
int n,i,sum;
printf("\nenter n :" );
scanf("%d",&n);
sum = 0;
for(i=1;i<n;i++)
{
if(n%i==0)
sum = sum +i;
}
if(sum == n)
printf("\n%d is perfect\n",n);
else
printf("\n%d is not perfect\n",n);
return 0;
}

enter n : 6

6 is perfect

Department of CSE.BVRIT
16
Fundamentals of computer science Lab

15. Program to print perfect numbers up to given number

int main()
{
int n,i,j,sum;
printf("\nenter n :");
scanf("%d",&n);
printf("\nthe perfect numbers are\n");
for(i=1;i<n;i++)
{
sum = 0;
for(j = 1;j<i;j++)
{
if(i%j==0)
sum = sum +j;
}
if(sum == i)
printf("\n%d",i);
}
return 0;
}

enter n : 9000

the perfect numbers are

6
28
496
8128

Department of CSE.BVRIT
17
Fundamentals of computer science Lab

16. Program to check given number is prime number or not

int main()
{
int n,i;
printf("\nenter n :");
scanf("%d",&n);
for(i=2;i<n;i++)
if(n%i==0)
break;
if(i==n)
printf("\n %d is prime\n",n);
else
printf("\n not\n");
return 0;
}

enter n : 23

23 is prime

17. Program to print prime numbers up to given number

int main()
{
int n,i,j;
printf("\nenter n :");
scanf("%d",&n);
for(j=2;j<=n;j++)
{
for(i=2;i<=j;i++)
{
if(j%i==0)
break;
}
if(i==j)
printf("\n %d is prime\n",i);
}
return 0;
}

Department of CSE.BVRIT
18
Fundamentals of computer science Lab

enter n : 20

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime

18. Program to check given number is strong number or not

int main()
{
int n,sum,m,i,f,j,rem;
printf("\nenter n : ");
scanf("%d",&n);
m = n;
sum = 0;
while(n>0)
{
f = 1;
rem = n%10;
for(j=1;j<=rem;j++)
f = f*j;
sum = sum +f;
n = n/10;
}
if(m==sum)
printf("\n%d is strong number\n",m);
else
printf("\n%d is not strong\n",m);
return 0;
}

enter n : 145

145 is strong number

Department of CSE.BVRIT
19
Fundamentals of computer science Lab

19. Program to print Strong number series up to given number

int main()
{
long int n,sum,m,i,f,j,rem,k;
printf("\nenter n : ");
scanf("%ld",&n);
for(k=1;k<n;k++)
{
m = k;
sum = 0;
while(m>0)
{
f = 1;
rem = m%10;
for(j=1;j<=rem;j++)
f = f*j;
sum = sum +f;
m = m/10;
}
if(k==sum)
printf("\n%ld is strong number\n",k);
}
return 0;
}

enter n : 150

1 is strong number

2 is strong number

145 is strong number

Department of CSE.BVRIT
20
Fundamentals of computer science Lab

20. Program to convert Binary to Decimal

#include<math.h>
int main()
{
int bin,dec,rem,i;
printf("\nenter bin : ");
scanf("%d",&bin);
i=0;
dec = 0;
while(bin!=0)
{
rem = bin%10;
dec = dec + rem*pow(2,i);
bin = bin/10;
i++;
}
printf("\ndecimal = %d\n",dec);
return 0;
}

enter bin : 10010


decimal = 18

21. Program to convert Decimal to Binary

#include<math.h>
int main()
{
int dec,bin,i,rem;
printf("\nenter decimal number : ");
scanf("%d",&dec);
bin = 0;
i=1;
while(dec!=0)
{
rem = dec%2;
bin = bin +rem*i;
dec = dec/2;
i=i*10;
}
printf("\nbin = %d",bin);
return 0;
}

enter decimal number : 18


bin = 10010

Department of CSE.BVRIT
21
Fundamentals of computer science Lab

22. Program to find an element using linear search

#include<stdio.h>
int a[30],i,n,item,pos;
int srch()
{
for(i=0;i<n;i++)
{
if(a[i] == item)
return i;
}
return -1;
}
void reada()
{
printf("\nenter elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
int main()
{
printf("\nenter limit : ");
scanf("%d",&n);
reada();
printf("\nenter item to search : ");
scanf("%d",&item);
pos = srch();
if(pos != -1)
printf("\n%d found at %d",item,pos);
else
printf("\n%d not found \n\n",item);
return 0;
}

enter limit :6

enter elements
465312

enter item to search : 3

3 found at 3

Department of CSE.BVRIT
22
Fundamentals of computer science Lab

23. Program to find an element using Binary Search


#include<stdio.h>
int a[30],i,n,item,first,middle,last,pos;
int srch()
{
first =0;
last = n-1;
while(first<=last)
{
middle = (first +last)/2;
if(item == a[middle])
return middle;
if(item>a[middle])
first = middle +1;
if(item<a[middle])
last = middle -1;
}
return -1;
}
void reada()
{
printf("\nenter elements in sorted order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
int main()
{
printf("\nenter limit : ");
scanf("%d",&n);
reada();
printf("\nenter item to search : ");
scanf("%d",&item);
pos = srch();
if(pos != -1)
printf("\n%d found at %d",item,pos);
else
printf("\n%d not found \n\n",item);
return 0;
}

enter limit : 5
enter elements in sorted order
10 20 30 40 50

enter item to search : 50

50 found at 4

Department of CSE.BVRIT
23
Fundamentals of computer science Lab

24. Program to left rotate the elements of Array

int main()
{
int a[20],i,j,n,temp,count;
printf("\nenter n : ");
scanf("%d",&n);
printf("\nenter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter count : ");
scanf("%d",&count);
while(count)
{
temp = a[0];
for(i=1;i<=n;i++)
a[i-1] = a[i];
a[n-1]= temp;
count--;
}
printf("\nthe array after rotation : ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
}

enter n : 5

enter elements
12345

enter count : 2

the array after rotation : 3 4 5 1 2

Department of CSE.BVRIT
24
Fundamentals of computer science Lab

25. Program to right rotate the elements of Array

int main()
{
int a[20],i,j,n,count,temp;
printf("\nenter n : ");
scanf("%d",&n);
printf("\nenter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter count :");
scanf("%d",&count);
while(count)
{
temp = a[n-1];
for(i=n-1;i>=1;i--)
a[i] = a[i-1];
a[0] =temp;
count--;
}
printf("\nthe elements after rotation are\n");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
}

enter n : 5

enter elements
12345
enter count : 2

the elements after rotation are


45123

Department of CSE.BVRIT
25
Fundamentals of computer science Lab

26. Program to print Lower triangular matrix

int main()
{
int a[20][20],i,j,n,m,sum,grand;
printf("\nenter order of square matrix: ");
scanf("%d%d",&m,&n);
printf("\nenter elements \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nthe elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
printf(" %d",a[i][j]);
}
printf("\n");
}
return 0;
}

enter order of square matrix: 3 3

enter elements
123
456
789

the elements are

4
78

Department of CSE.BVRIT
26
Fundamentals of computer science Lab

27. Program to print Upper triangular matrix

int main()
{
int a[20][20],i,j,n,m,sum,grand;
printf("\nenter order : ");
scanf("%d%d",&m,&n);
printf("\nenter elements \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nupper diagonal elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
printf("%d",a[i][j]);
else
printf("");
}
printf("\n");
}
return 0;
}

enter order : 3 3

enter elements
123
456
789

upper diagonal elements are


23
6

Department of CSE.BVRIT
27
Fundamentals of computer science Lab

28. Program to find sum of each row and column of a Matrix

int main()
{
int a[10][10],i,j,m,n,sum,gsum;
printf("\nenter order : ");
scanf("%d%d",&m,&n);
printf("\nenter elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
gsum = 0;
for(i=0;i<m;i++)
{
for(sum =0,j=0;j<n;j++)
sum = sum + a[i][j];
gsum = gsum + sum;
a[i][n] = sum;
}
for(i=0;i<n;i++)
{
for(sum=0,j=0;j<m;j++)
sum = sum + a[j][i];
gsum = gsum + sum;
a[m][i] = sum;
}
a[m][n] = gsum;
printf("\nthe resultant matrix is\n");
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
printf(" %d",a[i][j]);
printf("\n");
}
return 0;
}
enter order :
33
enter elements
111
222
333
the resultant matrix is
1113
2226
3339
6 6 6 36

Department of CSE.BVRIT
28
Fundamentals of computer science Lab

29. Print matrix elements in diagonal pattern

int main()
{
int a[10][10],i,j,m,n,k;
printf("\nenter order : ");
scanf("%d%d",&m,&n);
printf("\nenter elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nthe result \n\n");
for(k=0;k<m;k++)
{
for(i=k,j=0;i>=0&&j<n;i--,j++)
printf(" %d",a[i][j]);
printf("\n");
}
for(k=1;k<n;k++)
{
for(i=m-1,j=k;i>=0&&j<n;i--,j++)
printf(" %d",a[i][j]);
printf("\n");
}
return 0;
}

enter order : 3 4

enter elements
1111
2222
3333

the result

1
2 1
3 21
3 21
3 2
3

Department of CSE.BVRIT
29
Fundamentals of computer science Lab

30. Print Matrix elements in reverse diagonal pattern

int main()
{
int a[10][10],i,j,m,n,k;
printf("\nenter order : ");
scanf("%d%d",&m,&n);
printf("\nenter elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nthe result \n\n");
for(k=0;k<n;k++)
{
for(i=0,j=k;i<m&&j>=0;i++,j--)
printf(" %d",a[i][j]);
printf("\n");
}
for(k=1;k<m;k++)
{
for(i=k,j=n-1;i<m&&j>=0;i++,j--)
printf(" %d",a[i][j]);
printf("\n");
}

return 0;
}

enter order : 3 4

enter elements
1111
2222
3333

the result

1
1 2
1 23
1 23
2 3
3

Department of CSE.BVRIT
30
Fundamentals of computer science Lab

31. Program to Reverse array elements

int main()
{
int a[20],n,i,temp,m;
printf("\nenter n :");
scanf("%d",&n);
printf("\nenter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
m = n/2;
for(i=0;i<m;i++)
{
temp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = temp;
}
printf("\nthe array nelements are\n");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
}

enter n : 6

enter elements
123456

the array nelements are


654321

32. Program to display given matrix elements in spiral form

int main()
{
int a[10][10],i,j,m,n,k,l,row,col;
printf("\nenter order : ");
scanf("%d%d",&m,&n);
printf("\nenter elements \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nspiral display of matrix\n");
row = m-1;
col = n-1;
k = 0;
l = 0;

Department of CSE.BVRIT
31
Fundamentals of computer science Lab

while(k<=row && l<=col)


{
for(i=l;i<=col;i++)
printf(" %d",a[k][i]);
k++;
printf("\n");
for(i=k;i<=row;i++)
printf(" %d",a[i][col]);
col--;
printf("\n");
if(k<=row)
{
for(i=col;i>=l;i--)
printf(" %d",a[row][i]);
row--;
}
printf("\n");
if(l<=col)
{
for(i=row;i>=k;i--)
printf(" %d",a[i][l]);
l++;
}
printf("\n");
}
return 0;
}

enter order : 3 3

enter elements
123
456
789

spiral display of matrix


123
69
87
4
5

Department of CSE.BVRIT
32
Fundamentals of computer science Lab

33. program to illustrate command line arguments

#include<stdio.h>
int main(int argc,char* argv[])
{
int counter;
printf("Program Name Is: %s",argv[0]);
if(argc==1)
printf("\nNo Extra Command Line Argument Passed Other Than
Program Name");
if(argc>=2)
{
printf("\nNumber Of Arguments Passed: %d",argc);
printf("\n----Following Are The Command Line Arguments Passed--");
for(counter=0;counter<argc;counter++)
printf("\nargv[%d]: %s",counter,argv[counter]);
}
return 0;
}
/* Arguments First Second Third are passed in command line */
Program Name Is: ./main.c.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed--
argv[0]: ./main.c.out
argv[1]: First
argv[2]: Second
argv[3]: Third

34. Program for finding the largest integer among three numbers
using command line arguments

#include<stdio.h>
int main(int argc, char *argv[])
{
int a, b, c;
if (argc < 4 || argc > 5)
{
printf("enter 4 arguments only eg.\"filename arg1 arg2 arg3!!\"");
return 0;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
if (a < 0 || b < 0 || c < 0)
{
printf("enter only positive values in arguments !!");
return 1;
}

Department of CSE.BVRIT
33
Fundamentals of computer science Lab

if (!(a != b && b != c && a != c))


{
printf("please enter three different value ");
return 1;
}
else
{
if (a > b && a > c)
printf("%d is largest", a);
else if (b > c && b > a)
printf ("%d is largest", b);
else if (c > a && c > b)
printf("%d is largest ",c);
}
return 0;
}
/*Arguments 4 56 18 are passed in command line */
56 is largest
35. Program to demonstrate use of variable number of arguments.

#include <stdarg.h>
#include <stdio.h>

int min(int arg_count, ...)


{
int i;
int min, a;
va_list ap;
va_start(ap, arg_count);
min = va_arg(ap, int);
for (i = 2; i <= arg_count; i++)
if ((a = va_arg(ap, int)) < min)
min = a;
va_end(ap);
return min;
}

int main()
{
int count = 5;
printf("Minimum value is %d", min(count, 12, 67, 6, 7, 100));
return 0;
}

Minimum value is 6

Department of CSE.BVRIT
34
Fundamentals of computer science Lab

36. Program to implement function which can take the variable


number of parameters and return their average

#include <stdio.h>
#include <stdarg.h>

double average(int num,...)


{

va_list valist;
double sum = 0.0;
int i;
va_start(valist, num);
for (i = 0; i < num; i++)
{
sum += va_arg(valist, int);
}
va_end(valist);

return sum/num;
}

int main() {

printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));


printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
return 0;
}

Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000

37. Program to demonstrate function pointer example

#include <stdio.h>
int fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
int (*fun_ptr)(int);
fun_ptr = &fun;
(*fun_ptr)(10);
return 0;
}

Value of a is 10

Department of CSE.BVRIT
35
Fundamentals of computer science Lab

38. Program to demonstrate function pointer example

#include <stdio.h>
int add(int a, int b)
{
printf("Addition is %d\n", a+b);
}
int subtract(int a, int b)
{
printf("Subtraction is %d\n", a-b);
}
int multiply(int a, int b)
{
printf("Multiplication is %d\n", a*b);
}

int main()
{
int (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};
int ch, a , b;
printf("\nEnter a and b values :");
scanf("%d%d",&a,&b);
printf("Enter Choice: 0 for add, 1 for subtract and 2 for multiply\n");
scanf("%d", &ch);
if (ch > 2) return 0;
(*fun_ptr_arr[ch])(a, b);
return 0;
}

Enter a and b values :


47
Enter Choice: 0 for add, 1 for subtract and 2 for multiply
2
Multiplication is 28

Department of CSE.BVRIT
36
Fundamentals of computer science Lab

39. Program to demonstrate user defined header to find cube of a


number
cube.h
int cube(int n)
{
int ans;
ans = n*n*n;
return ans;
}

#include"cube.h"
int main()
{
int n;

printf("\nenter n :");
scanf("%d",&n);
printf("\ncube(%d) = %d",n,cube(n));
return 0;
}

enter n : 5

cube(5) = 125

Department of CSE.BVRIT
37
Fundamentals of computer science Lab

40. Program to demonstrate user defined header add and multiply two
numbers

fun.h

int add(int a,int b)


{
printf("\naddition = %d",a+b);
}
int mul(int a,int b)
{
printf("\nmultiplication = %d",a*b);
}

#include"fun.h"
int main()
{
int a,b;

printf("\nenter a and b values : ");


scanf("%d%d",&a,&b);
add(a,b);
mul(a,b);
return 0;
}

enter a and b values :


12 6

addition = 18
multiplication = 72

Department of CSE.BVRIT
38
Fundamentals of computer science Lab

41. Program to demonstrate make utility


fun.c

int myFunc(int)
{
printf("\nWorld");
}

program.c
#include”fun.c”
int main()
{
printf("Hello");
myFunc();
return 0;
}

Makefile

all: #target name


gcc main.c fun.c -o program

$] make
gcc program.c fun.c -o program
$] ./program
Hello
World

42. Program to demonstrate multifile program.


set.h

int x;
int set(int i);

set.c
#include "set.h"

int set(int i)
{
x=i;
}

Department of CSE.BVRIT
39
Fundamentals of computer science Lab

program.c
#include <stdio.h>
#include "set.h"
int main()
{
set(1);
printf("x=%d\n", x);
set(5);
printf("x=%d\n",
gcc x);
set.c program.c
return 0;
}

x=1
x=5

43. Program to find Length of a given string

void main()
{
int n;
char a[10],*p;
printf("\nenter string : ");
scanf("%s",a);
p = a;
n =0;
while(*p)
{
n++;
p++;
}
printf("\nlength of string = %d ",n);
printf("\nstrlen(%s) = %d",a,strlen(a));
return 0;
}

enter string : bvrit

length of string = 5
strlen(bvrit) = 5

Department of CSE.BVRIT
40
Fundamentals of computer science Lab

44. Program to Compare two strings

int main()
{
char a[20],b[20],*p,*q;
printf("\nenter first string :");
scanf("%s",a);
printf("\nenter second string :");
scanf("%s",b);
p=a;
q =b;
while(*p)
{
if(*p!= *q)
break;
p++;
q++;
}
if(*p == *q)
printf("strings are same\n");
else
printf("strings are not same ");
return 0;
}

enter first string : bvrit

enter second string : bvrit


strings are same

Department of CSE.BVRIT
41
Fundamentals of computer science Lab

45. Program to concatenate two strings

int main()
{

char a[20],b[10],*p,*q;
printf("\nenter string1 :");
scanf("%s",a);
printf("\nenter string2 :");
scanf("%s",b);
p = a;
q = b;
while(*p)
p++;
while(*q)
{
*p++ = *q++;
}
*p = '\0';
printf("\nafter concatenation = %s",a);
return 0;
}

enter string1 : bvrit


enter string2 : college
after concatenation = bvritcollege

46. Program to Reverse the given string

void main()
{
char a[30],b[30];
int i,j;
printf("\nenter the string : ");
scanf("%s",a);
for(i=0;a[i];i++);
for(j=0,i=i-1;i>=0;i--,j++)
b[j]=a[i];
b[j] = '\0';
printf("\nthe reversed string is = %s\n\n",b);
return 0;
}

enter the string :


computer

the reversed string is = retupmoc

Department of CSE.BVRIT
42
Fundamentals of computer science Lab

47. Program to find given string is palindrome or not

#include<stdio.h>
#include<string.h>
int main()
{
char a[20];
int n,i,j,m;
printf("\nenter string :");
scanf("%s",a);
n = strlen(a);
i=0;
j =n-1;
m = n/2;
while(i <m)
{
if(a[i]!=a[j])
break;
i++;
j--;
}
if(i==m)
printf("\n%s is palindrome ",a);
else
printf("\n%s is not palindrome ",a);
return 0;
}

enter string : madam

madam is palindrome

Department of CSE.BVRIT
43

You might also like