Fcs-Lab Manual
Fcs-Lab Manual
Fcs-Lab Manual
LABORATORY MANUAL
I B.Tech. I SEM
AY-2020-21
Prepared by
Department of CSE.BVRIT
2
Fundamentals of computer science Lab
Department of CSE.BVRIT
3
Fundamentals of computer science Lab
HARDWARE REQUIREMENTS:
80GB HDD
512MB DDR
SOFTWARE REQUIREMENTS:
Windows/Linux OS
Turbo C Compiler
GCC compiler
Online C Editor
Course Outcomes:
COs Description
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.
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.
Department of CSE.BVRIT
5
Fundamentals of computer science Lab
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.
Assessment Method:
S Lab Evaluation Criteria for Internal 30 Marks Total
.No
100M
Department of CSE.BVRIT
6
Fundamentals of computer science Lab
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
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
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
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
Department of CSE.BVRIT
9
Fundamentals of computer science Lab
Department of CSE.BVRIT
10
Fundamentals of computer science Lab
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
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
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;
}
LCM = 280
Department of CSE.BVRIT
12
Fundamentals of computer science Lab
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
Department of CSE.BVRIT
13
Fundamentals of computer science Lab
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
1
153
370
371
407
Department of CSE.BVRIT
14
Fundamentals of computer science Lab
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
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
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
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
6
28
496
8128
Department of CSE.BVRIT
17
Fundamentals of computer science Lab
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
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
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
Department of CSE.BVRIT
19
Fundamentals of computer science Lab
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
Department of CSE.BVRIT
20
Fundamentals of computer science Lab
#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;
}
#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;
}
Department of CSE.BVRIT
21
Fundamentals of computer science Lab
#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
3 found at 3
Department of CSE.BVRIT
22
Fundamentals of computer science Lab
enter limit : 5
enter elements in sorted order
10 20 30 40 50
50 found at 4
Department of CSE.BVRIT
23
Fundamentals of computer science Lab
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
Department of CSE.BVRIT
24
Fundamentals of computer science Lab
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
Department of CSE.BVRIT
25
Fundamentals of computer science Lab
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 elements
123
456
789
4
78
Department of CSE.BVRIT
26
Fundamentals of computer science Lab
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
Department of CSE.BVRIT
27
Fundamentals of computer science Lab
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
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
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
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
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
enter order : 3 3
enter elements
123
456
789
Department of CSE.BVRIT
32
Fundamentals of computer science Lab
#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
#include <stdarg.h>
#include <stdio.h>
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
#include <stdio.h>
#include <stdarg.h>
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() {
Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000
#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
#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;
}
Department of CSE.BVRIT
36
Fundamentals of computer science Lab
#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
#include"fun.h"
int main()
{
int a,b;
addition = 18
multiplication = 72
Department of CSE.BVRIT
38
Fundamentals of computer science Lab
int myFunc(int)
{
printf("\nWorld");
}
program.c
#include”fun.c”
int main()
{
printf("Hello");
myFunc();
return 0;
}
Makefile
$] make
gcc program.c fun.c -o program
$] ./program
Hello
World
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
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;
}
length of string = 5
strlen(bvrit) = 5
Department of CSE.BVRIT
40
Fundamentals of computer science Lab
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;
}
Department of CSE.BVRIT
41
Fundamentals of computer science Lab
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;
}
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;
}
Department of CSE.BVRIT
42
Fundamentals of computer science Lab
#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;
}
madam is palindrome
Department of CSE.BVRIT
43