Professional Documents
Culture Documents
Uppala Bharath Kumar
Uppala Bharath Kumar
Uppala Bharath Kumar
PROGRAMMING IN C -LAB
RECORD
Submitted by
UPPALA BHARATH KUMAR [9920004404]
BONAFIDE CERTIFICATE
This is to certify that the Theory with Practical Component Report titled
“Programming in C ” is a bonafide record of the work done by Uppala
Bharath Kumar(9920004404) in partial fulfillment of the requirements for the
award of the degree of Bachelor of Technology in Specialization of the
Computer Science and Engineering, during the Academic year Even Semester
(2020-2021).
ACKNOWLEDGEMENT
First and foremost, I wish to thank the Almighty God for his grace and
benediction to complete this Project work successfully. I would like to convey my
special thanks from the bottom of my heart to my dear Parents and affectionate
Family members for their honest support for the completion of this Project work.
Besides my Project guide, I would like to thank the rest of Class committee
members and all faculty members and Non-Teaching staff for their insightful
comments and encouragement. Finally, but by no means least, thanks go to all my
school and college teachers, well wishers, friends for almost unbelievable support.
4
5
C-programming Lab
Result: Hence using above program we can find the simple interest.
9
{
int r;
float a;
printf("enter the value of r");
scanf("%d",&r);
a=3.14*r*r;
printf("the area of the circle is:%f",a);
}
Output:
12
Result: Hence the above program finding the area of circle is verified.
13
Algorithm:
Step-4: End.
Source code:
#include<stdio.h>
main()
int f;
float c;
scanf("%d",&f);
c=(f-32)/1.8;
Output:
16
if(b<c)
printf("%d%d%d",a,b,c);
else
printf("%d%d%d",a,c,b);
}
if(b<a && b<c)
{
if (a<c)
printf("%d%d%d",b,c,a);
else
printf("%d%d%d",b,c,a);
}
if(c<a && c<b)
{
if(a<b)
printf("%d%d%d",c,a,b);
else
printf("%d%d%d",c,b,a);
}
}
Output:
19
Source code:
#include<stdio.h>
main()
{
int n,s=0,r;
printf("enter the numbers");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("sum of digits is:%d",s);
}
21
Output:
rem=num%10;
sum=sum*10+rem;
num=num/10;
}
printf("the reversed number is:%d",sum);
}
Output:
Result: Hence the reversing the given number is verified using above
program.
24
Source code:
#include<stdio.h>
main()
int num,rem,sum=0;
scanf("%d",&num);
while(num!=0)
rem=num%10;
sum=sum*10+rem;
25
num=num/10;
if(sum=num)
else
Output:
Algorithm:
Step 1 → Take integer variable A
Step 2 → Assign value to the variable
Step 3 → From value A upto 1 multiply each digit and store
Step 4 → the final stored value is factorial of A.
Source code:
Code-1:
#include<stdio.h>
main()
{
int n,s=1,i;
printf("enter the number");
scanf("%d",&n);
while(n!=0)
{
s=s*n;
n=n-1;
}
fact=fact*i;
printf("the factorial is:%d",fact);
}
}
Output:
Source code:
#include<stdio.h>
main()
{
int d,n,sum=0,m;
printf("enter the number");
scanf("%d",&n);
n=m;
while(n>0)
{
d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if (n==sum)
printf("the number is armstrong");
else
printf("the number is not armstrong");
}
Output:
29
6 Step: SWITCH(OPERATOR)
8 Step: EXIT.
Source code:
#include<stdio.h>
main()
{
int a,b,c,ch;
printf("enter the value");
scanf("%d%d",&a,&b);
printf("enter the choice 1.add 2.sub.3.mul 4.div");
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("%d",c);
break;
case 2:
c=a-b;
31
printf("%d",c);
break;
case 3:
c=a*b;
printf("%d",c);
break;
case 4:
c=a/b;
printf("%d",c);
break;
default:
printf("invalid");
}
}
Output:
Step-2: For loop will resist the user, not to enter more than
two values by using the condition i<n.
Source code:
#include<stdio.h>
main()
float avg,sum=0;
int a[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
33
scanf("%d",&a[i]);
sum=sum+a[i];
avg=sum/n;
printf("sum=%f",sum);
printf("avg=%f",avg);
Output:
Algorithm:
Step-1: First take the input from the user how many numbers
wish to enter.
Source code:
#include<stdio.h>
main()
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
35
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i]<a[j])
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
printf("%d",a[i]);
Output:
36
Algorithm:
Step-1: Start
Step-2: Now take the input from the user how many numbers
he wants to enter.
Step-4: Now take the input from user which element he wants
to search.
Step-5: Now using for loop check the each and every element
in the given input and add 1 to one variable using if condition
store the value.
Source code:
#include<stdio.h>
main()
int a[10],n,x,i,s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
38
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<n;i++)
if(x==a[i])
s=s+1;
Output:
Algorithm:
Step-1: Start
Step-2: Now take the input from the user how many numbers
he wants to enter.
Step-4: Now take the input from user which element he wants
to search.
Step-5: Now using for loop check the each and every element
in the given input and add 1 to one variable using if condition
store the value.
Source code:
#include<stdio.h>
main()
int a[10],n,x,i,s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<n;i++)
if(x==a[i])
s=s+1;
if(s==0)
else
Output:
41
Algorithm:
Source code:
#include<stdio.h>
main()
int a[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
43
for(i=0;i<n;i++)
if(a[i]%2==0)
else
//break;
Output:
Algorithm:
Step-1:start.
Step-2: Take the input from the user how many numbers want
to enter (“here it is only for two numbers”).
Step-6: Stop.
Source code:
#include<stdio.h>
main()
int a[2],i,j,t,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
45
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
printf("%d",a[i]);
Output:
46
Algorithm:
START
Step 1 -> Input matrix 1 and matrix 2.
Step 2 -> If the number of rows and number of columns of matrix 1
and matrix 2 are equal then execute step 3 else addition not possible
Step 3 -> for i=1 to rows[matrix 1]
for j=1 to columns [matrix 1]
Input matrix 1 [i,j]
Input matrix 2 [i,j]
matrix 3 [i,j]= matrix 1 [i,j] + matrix 2 [i,j];
step 4-> Display matrix 3 [i,j];
STOP
Source code:
#include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("enter the values of matrix a");
for(i=0;i<3;i++)
48
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the values of matrix b");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
49
}
printf("the sum is");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d",c[i][j]);
printf("\n");
}
Output:
50
Algorithm:
START
Step 1 -> Input matrix 1 and matrix 2.
Step 2 -> If the number of rows and number of columns of matrix 1
and matrix 2 are equal then execute step 3 else subtraction not
possible.
Step 3 -> for i=1 to rows[matrix 1]
for j=1 to columns [matrix 1]
Input matrix 1 [i,j]
Input matrix 2 [i,j]
matrix 3 [i,j]= matrix 1 [i,j] - matrix 2 [i,j];
step 4-> Display matrix 3 [i,j];
STOP
Source code:
#include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("enter the values of matrix a");
for(i=0;i<3;i++)
52
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the values of matrix b");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
53
c[i][j]=a[i][j]-b[i][j];
}
}
printf("the diff is");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d\n\n",c[i][j]);
printf("\n");
}
Output:
54
55
Step 2: Enter the row and column of the first (a) matrix.
Step 3: Enter the row and column of the second (b) matrix.
Step 6: Print the elements of the first (a) matrix in matrix form.
Step 7: Print the elements of the second (b) matrix in matrix form.
Step 11: Multiply the first (a) and second (b) matrix and store the element
Source code:
#include <stdio.h>
void main()
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
scanf("%d%d",&m,&n);
scanf("%d%d",&r,&s);
if(m!=r)
58
else
for(i= 0;i<m;i++)
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
59
scanf("\t%d",&b[i][j]);
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
for(i=0;i<m;i++)
printf("\n");
60
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
61
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
Output:
62
63
verified.
64
Algorithm:
step-1: start.
Step-9:stop.
Source code:
65
#include<stdio.h>
#include<string.h>
main()
int d;
char x[100],y[100],z[100];
scanf("%s",&x);
scanf("%s",y);
strcpy(z,x);
d=strlen(x);
66
strcpy(x,y);
d=strcmp(z,y);
if(d==0)
else
Output:
67
Aim: Swapping of two numbers using function call with out passing
Algorithm:
Step-1: start.
Step-3: now store the b value in temporary variable and store b value
Step-5: Stop.
Source code:
#include<stdio.h>
69
int swap();
int main()
swap();
swap()
int a,b,t;
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
70
Output:
statements is verified.
71
Algorithm:
Step-1: start.
Step-3: now store the b value in temporary variable and store b value
Step-5: Stop.
Source code:
#include<stdio.h>
72
int main()
int a,b;
scanf("%d%d",&a,&b);
swap(a,b);
swap(int a,int b)
int t;
t=a;
a=b;
73
b=t;
printf("after swapping");
printf("%d%d",a,b);
Output:
program is verified.
74
return statement.
Aim: To add the two numbers using the function call by passing
Algorithm:
Step-1: start.
Step-3: Now call the function now add the two numbers in the user
defined function .
Step-6: stop.
Source code:
75
#include<stdio.h>
int main()
int a,b,c,d;
scanf("%d%d",&a,&b);
c=add(a,b);
return 0;
add(int a,int b)
76
int c;
c=a+b;
return (c);
Output:
Aim: To find the factorial of the given number using recursion using
C-program.
Algorithm:
Step-1: start.
Step-4: Now in the user defined function write the process for
factorial number.
Step-6: Stop.
Source code:
#include<stdio.h>
78
int main()
int n;
scanf("%d",&n);
int fact(int n)
if(n>0)
return(n*fact(n-1));
79
else
return 1;
Output:
Algorithm:
Step-1: start.
Step-2: Take the input from the user does how many series count
Step-3: Now add the successive addition using the user input using
Step-5: Stop.
Source code:
#include<stdio.h>
int main()
int n,x,i;
series");
scanf("%d",&n);
for(i=1;i<=n;i++)
x=fib(i);
printf("\n%d",x);
}
82
int fib(int n)
if((n==1)||(n==0))
return 1;
else
return fib(n-1)+fib(n-2);
Output:
83
Algorithm:
Step-1: Start.
Step-5: stop.
Source code:
#include<stdio.h>
int main()
int var[]={10,100,200};
int i;
int *ptr=&var[1];
printf("*ptr-1 = %d\n",*(ptr-1));
printf("*ptr = %d \n",*ptr);
printf("*ptr+1 = %d \n",*(ptr+1));
return 0;
Output:
85
Code-2:
#include<stdio.h>
int main()
int var[]={10,100,200};
int i;
int *ptr=&var[0];
for(i=0;i<max;i++)
86
printf("\n%d",*(ptr+i));
Output:
code-3:
#include<stdio.h>
int main ()
{
87
int i, *ptr;
ptr = var;
i = 0;
ptr++;
i++;
}
88
return 0;
Output:
Code-4:
#include<stdio.h>
int main()
{
89
int var[]={10,100,200};
int i;
for(i=0;i<max;i++)
ptr[i]=&var[i];
for(i=0;i<max;i++)
printf("value of var[%d]=%d\n\n\n",i,*ptr[i]);
}
90
Output:
Code-5:
#include<stdio.h>
#include<conio.h>
int main()
{
91
//clrscr();
//system('clear');
int var[]={10,100,200};
int i;
int *ptr[max];
for(i=0;i<max;i++)
ptr[i]=&var[i];
for(i=0;i<max;i++)
{
92
printf("value of var[%d]=%d\n\n\n",i,*ptr[i]);
Output:
Result: Hence the above program Pointers using the array is verified.
93
Algorithm:
Step-1:start
Step-2: Now give the input for the user to enter the string.
Step-6:output.
Source code:
#include <stdio.h>
94
int main()
char str[100];
char *p;
p=str;
while(*p!='\0')
printf("%c",*p++);
Output:
memory allocation.
Algorithm:
Step-1: start.
Step-6: Stop.
Source code:
#include<stdio.h>
#include<string.h>
97
//#include<alloc.h>
#include<stdlib.h>
int main()
char *a;
a=(char*)malloc(6*sizeof(char));
strcpy(a,"HELLO");
a=(char*)realloc(a,13);
strcpy(a,"ARUPPUKOTTAI");
free(a);
a=NULL;
98
Output:
verified.
99
Algorithm:
Step-1:Start.
Step-4:stop.
Source code:
#include<stdio.h>
//#include<conio.h>
struct employee
{
100
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
int main ()
scanf("%d",&e1.emp_id);
scanf("%s",e1.name);
scanf("%f",&e1.salary);
scanf("%s",&e1.address);
scanf("%d",&e1.dept_no);
scanf("%d" ,&e1.age);
scanf("%d",&e2.emp_id);
102
scanf("%s",&e2.name);
scanf("%f",&e2.salary);
scanf("%s",&e2.address);
scanf("%d",&e2.dept_no);
scanf("%d",&e2.age);
Output:
Aim: To use the struct for the structure implementation to the array
in C-program.
Algorithm:
Step-1:start.
Step-6:stop.
Source code:
#include <stdio.h>
106
//#include <conio.h>
struct employee
int emp_id;
char name[20];
float salary;
107
char address[50];
int dept_no;
int age;
};
int main()
int i;
108
scanf("%d",&e[i].emp_id);
scanf ("%s",e[i].name);
scanf ("%f",&e[i].salary);
scanf ("%d",&e[i].dept_no);
scanf ("%d",&e[i].age);
//get ch();
Output:
array is verified.
111
program.
Algorithm:
Step-1:start
Step-8:stop
Source code:
112
#include <stdio.h>
struct employee
int emp_id;
char name[20];
float salary;
};
int main ()
113
scanf("%d",&e1.emp_id);
scanf("%s",e1.name);
scanf("%f",&e1.salary);
printdata(e1);
Output: