c Programming Lab r20 Cse Final
c Programming Lab r20 Cse Final
OF
ENGINEERING AND TECHNOLOGY
(AUTONOMOUS)
NH5,BYPASS ROAD,GUDUR.
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
OF
C PROGRAMMING LAB
20CS102
AUDISANKARA COLLEGE
OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.
AUDISANKARA
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
C PROGRAMMING LAB
LAB MANUAL
INDEX
1 Programs on Expressions
2 Programs on Operators.
12 Programs on files.
C PROGRAMMING LAB MANUAL
Exercise-1
ALGORITHM:
Step1: start
Step3: s= (a*x+b)/(a*x-b)
Step4: Result s
Step 5: stop
FLOW CHART:
STOP
To evaluate algebraic exp (ax+b)/ (ax-b)
take a,b,x,S
a=5,b=4,x=3
S=(ax+b)/(ax-b)
Display S
STOP
C PROGRAMMING LAB MANUAL
PROGRAM:
To evaluate algebraic exp(ax+b)/(ax-b)
#include<stdio.h>
#include<conio.h>
void main()
int a,b,x;
float s;
clrscr();
scanf("%d%d%d",&a,&b,&x);
s=(a*x+b)/(a*x-b);
getch();
}
Input & Output:
Enter the values of a,b,x… 2 4 6
ALGORITHM:
Step1: start
Step3: s=pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2
Step4: Result s
Step 5: stop
FLOWCHART:
start
take x
x=6
S=pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2
Display S
stop
PROGRAM:
#include<stdio.h>
#include<math.h>
Void main ()
{
float x,s;
printf("enter the values of x:");
scanf("%f",&x);
s=pow(x,5)+10*pow(x,4)+8*pow(x,3)+4*x+2;
printf("the value of s=%f",s);
getch();
}
The value of s = 25
ALGORITHM:
Step1: start
Step 5: stop
FLOWCHART:
To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)))
Start
take a,b,c,s,a
a=5,b=4,c=2
s=(a+b+c)/2
area=sqrt(s-a)(s-b)(s-c)
Print Area
Stop
PROGRAM:
ALGORITHM:
Step1: start
Step3: a=a+b
Step4: b=a-b
Step 5: a=a-b
Step7: stop
FLOWCHART:
Program to swap two numbers
start
take a,b
a=1,b=10
a=a+b;b=a-b;a=a-b
Print a,b
stop
void main()
{
int a,b;
clrscr();
printf("enter the values of a,b\n");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("the values of a,b are: %d %d",a,b);
getch();
}
Input & Output:
Exercise-2
ALGORITHM:
Step1: start
Step3: c=(a>b)?a:b
Step4: Result c
Step 5: stop
FLOWCHART:
Start
Take a,b
C= (a>b)? a:b
Print c
Stop
Result:
ALGORITHM:
Step 1: start
Step 5: else
Step 6: if(b>c)
Step 8: else
Step10: stop
Start
Take a,b,c
If
(a>b)&&(a>c)
Display a
If (b>c)
Display b
Display c
Stop
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("a is greatest of %d %d %d", a,b,c);
else
if(b>c)
printf("b is greatest of %d %d %d",a,b,c);
else
printf("c is greatest of %d %d %d",a,b,c);
getch();
}
10
30
20
30 is greatest of 10 30 20
ALGORITHM:
Step1: start
Step3: switch(result)
Step10: stop
PROGRAM: Program to perform the arithmetic expression using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
clrscr();
printf(" 1.addition\n 2.subtraction\n 3.multiplication\n 4.division\n");
printf("enter the values of a & b:");
scanf("%d%d",&a,&b);
printf("enter your choice : ");
scanf("%d",&op);
switch(op)
{
case 1:printf("sum of %d and %d=%d",a,b,a+b);
break;
case 2:printf("difference of %d and %d=%d",a,b,a-b);
break;
case 3:printf("multiplication of %d and %d=%d",a,b,a*b);
break;
2. Substraction
3. Multiplication
4. Division
Sum of 10 and 20 = 30
Exercise-3
3(A).Write a Program to find the factorial of a given number
ALGORITHM:
Step1: start
Step3: fact=i=1
Step4: if(i<=n)
Step5: fact=fact*i
Step6: i=i+1
Step9: stop
FLOWCHART:
Start
Take n
Fact=i=1
If
(i<=n)
fact=fact*i; i=i+1
Display fact
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1,i=1;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial value of %d is :%d",n,fact);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,fact=1;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("Factorial value of %d is :%d",n,fact);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,fact=1;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
do
{
fact=fact*i;
i++;
}
while(i<=n);
printf("Factorial value of %d is :%d",n,fact);
getch();
}
Enter a number :5
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r;
clrscr();
printf("enter the range:");
scanf("%d",&r);
for(i=2;i<=r;i++)
{
k=1;
for(j=2;j<=i-1;j++)
{
if(i%j==0)
{
k=0;
}
}
if(k!=0)
printf("%d\n",i);
}
getch();
}
Input & Output:
Algorithm:
Step1: start
Step2: read n
Step3: i=0,sum=0
Step4: perform from step 5 to step 6 until i<=n
Step5: sum=sum+i;
Step6: i++;
Step7: write sum
Step8: stop
Read n
i=0;sum=0
F
While(i<=n)
T
Sum=sum+i;
i++
PRINT sum
stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0,sum=0;
clrscr( );
printf("Enter Limit : ");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("Sum of %d natural numbers = %d",n,sum);
getch();
}
Result:
Enter Limit : 10
Sum of 10 natural numbers = 55
OR
Sum of N natural numbers. n*(n+1)/2
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
sum=n*(n+1)/2;
printf("sum=%d",sum);
getch();
}
Result:
Input & output:
Enter the n value:5
Sum:=15
Exercise-4
4(A).Write a program to find the given number is Palindrome or not
PROGRAM // Given number is Palindrome or not
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,s=0,r;
clrscr();
printf("enter n value:");
scanf("%d",&n);
num=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==num)
{
printf("%d is a palindrome number",num);
}
else
{
printf("%d is not a palindrome number",num);
}
getch();
}
Input & Output:
Enter n value : 121
if(s==num)
{
printf("%d is an armstrong number\n",num);
}
else
{
printf("%d is not an armstrong number",num);
}
getch();
}
Input & Output:
Enter n value : 153
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float x1,x2;
clrscr();
printf("Enter a,b,c values\n");
scanf("%d %d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
printf("\nroots are imaginary");
else if(d>0)
{
printf("roots are real & un equal\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("x1=%f\nx2=%f\n",x1,x2);
}
else
{
printf("\nroots are real & equal\n");
x1=x2=-b/(2*a);
printf("x1=%f\nx2=%f\n",x1,x2);
}
getch();
}
Result:
1.Enter a,b,c values: 1 2 3
Roots are imaginary.
2.Enter a,b,c values: 1 2 1
Roots are real & equal. x1=-1.000000 x2=-1.000000
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i,t;
long int n;
clrscr();
printf("Enter n value:");
scanf("%ld",&n);
do
{
sum=0;
do
{
t=n%10;
sum=sum+t;
n=n/10;
}while(n>0);
n=SUM;
}while(n>9);
printf("Sum=%d",sum);
getch();
}
Enter n value:138
Sum=3
Exercise-5
PROGRAM
5. Write a program to find the Max and Min number by using functions.
#include<stdio.h>
#include<conio.h>
int big(int[],int);
int small(int[],int);
void main()
{
int a[10],i,n,max,min;
clrscr();
printf("Enter size of an array:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=big(a,n);
min=small(a,n);
printf("max=%d",max);
printf("min=%d",min);
getch();
}
Max=98
Min=21
Exercise-6
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int x,y;
clrscr();
printf("enter the values of x, y:\n");
scanf("%d %d",&x,&y);
swap(x,y);
printf("in the main function\n");
printf("x=%d\ny=%d\n",x,y);
getch();
}
void swap (int a,int b)
{
int t;
printf("before swapping\n");
printf("x=a=%d\n",a);
printf("y=b=%d\n",b);
t=a;
a=b;
b=t;
printf("after swapping\n");
printf("x=a=%d\n",a);
printf("y=b=%d\n",b);
}
x=a=60
y=b=50
in the main function
x=50
y=60
x=a=50
y=b=60
after swapping
x=a=60
y=b=50
in the main function
x=60
y=50
Exercise-7
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
int fact1(int);
clrscr();
printf("Enter n value:");
scanf("%d",&n);
fact=fact1(n);
printf("Factorial value is:%d",fact);
getch();
}
int fact1(int n)
{
if(n==1 || n==0)
return(1);
else
return n*fact1(n-1);
}
Enter n value:5
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
int fibo(int);
clrscr();
printf("Enter any number:");
scanf("%d",&n);
f=fibo(n);
printf("The %dth fibonoci number is:%d",n,f);
getch();
}
int fibo(int x)
{
if(x==1)
return(0);
else if(x==2)
return(1);
else
return(fibo(x-1)+fibo(x-2));
}
// Fibonacci series
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,n,c,i=2;
clrscr();
printf("enter how many fibonacci numbers u want :");
scanf("%d",&n);
printf("Fibonacci numbers are...");
printf("\n%d %d",a,b);
c=a+b;
do
{
printf("%5d",c);
a=b;
b=c;
c=a+b;
i++;
}while(i<n);
printf("\n");
getch();
}
INPUT AND OUTPUT
Enter how many fibonacci numbers u want :6
Fibonacci numbers are: 0 1 1 2 3 5
Enter how many fibonacci numbers u want :8
Fibonacci numbers are: 0 1 1 2 3 5 8 13
Result:
Exercise-8
8(A). Matrix Addition
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf("enter the 1st matrix size\n");
scanf("%d%d",&m,&n);
printf("enter the 2nd matrix size\n");
scanf("%d%d",&p,&q);
if(m!=p||n!=q)
{
printf("matrix Addition is not possible");
getch();
exit(0);
}
printf("enter 1st matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter 2nd matrix elements\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
}
printf("Matrix Addition is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%5d\t",c[i][j]);
}
}
getch();
}
INPUT AND OUTPUT
enter the 1st matrix size
2
2
enter the 2nd matrix size
2
2
enter 1st matrix elements
2
2
2
2
enter 2nd matrix elements
2
2
2
2
Matrix Addition is:
4 4
4 4
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10];
int i, j, m, n;
clrscr();
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for(i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The given matrix is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
getch();
}
Exercise-9
AIM: Program to read five cities and sort them and print sorted list of
citied in alphabetical order
ALGORITHM:
Step1:start
Step2:enter 5 city names
For(i=65;i<122;i++)
for(j=0;j<5;j++)
if(city[j][0]==i)
printf(”\n%s”,city[j]);
Step4:stop
PROGRAM:
A program to read five cities and sort them and print sorted list of citied in
alphabetical order
#include<stdio.h>
#include<conio.h>
void main()
{
char city[5][20];
int i,j;
clrscr();
printf("enter the names of cities...\n");
for(i=0;i<5;i++)
scanf("%s",&city[i]);
printf("\nsorted list of cities...\n\n");
for(i=65;i<122;i++)
{
for(j=0;j<5;j++)
{
if(city[j][0]==i)
printf("\n%s",city[j]);
}
}
getch();
}
Result:
case 6:exit(0);
default:printf("You Entered Wrong choice");
}
printf("\n Do you want to continue(y or n)");
ch=getch();
}
while(ch=='Y'|| ch=='y');
getch();
}
Exercise-10
10(A). // Print Address of a Variable
#include<stdio.h>
#include<conio.h>
void main()
{
int *m,n=20;
clrscr();
m=&n;
printf("Address of n is =%u\n",&n);
printf("value of n is =%d\n",*(&n));
printf("value of m is =%d\n",*m);
printf("address m is =%u\n",&m);
getch();
}
Exercise-11
#include<stdio.h>
#include<conio.h>
struct emp
{
int dep;
char name[50];
int basic;
}a[10];
void main()
{
float da,hra,pf,sal;
int i,n;
clrscr();
printf("enter how many employees\n");
scanf("%d",&n);
printf("enter deptcode name and basic of employee..\n");
for(i=0;i<n;i++)
{
scanf("%d%s%d",&a[i].dep,a[i].name,&a[i].basic);
}
for(i=0;i<n;i++)
{
da=a[i].basic*0.20;
hra=a[i].basic*0.5;
pf=a[i].basic*0.10;
sal=a[i].basic+da+hra-pf;
printf("\ndept\t name basic \tda \t hra\t pf\t sal\n ");
printf("%d\t%s %d %f %f %f
%f\n",a[i].dep,a[i].name,a[i].basic,da,hra,pf,sal);
}
getch();
}
#include<stdio.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
};
void Display(struct student stu);
void main()
{
struct student s1;
clrscr();
printf("Enter student's name: ");
scanf("%s",&s1.name);
printf("Enter roll number:");
scanf("%d",&s1.roll);
Display(s1); // passing structure variable s1 as argument
getch();
}
void Display(struct student stu){
printf("Output\nName: %s",stu.name);
printf("\nRoll: %d",stu.roll);
}
Exercise-12
Raja1.txt
Hai how r u.
Source Code: