Professional Documents
Culture Documents
(18CPL17/27)
Create a directory:
To create a directory type the following commands and press enter.
Syntax: mkdir <directoryname>
This is how a Gedit text editor of file name 'example.c' would look.
Type the program in the Gedit text editor save it and then close the text editor.
NOTE:
When using the 'math.h' header file in the C program, use the following command to
compile. Syntax: cc -lm filename.c
Lab Programs
PART A
2. Develop a program to solve simple computational problems using arithmetic expressions and
use of each operator leading to simulation of a commercial calculator.(No built-in math
function)
Algorithm
Step 1: [Start]
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Flow Chart
START
Read choice
Case T
Result = num1 + num2
1
F
T
Case Result = num1 - num2
2
F T
Case Result = num1 * num2
3
F T
Case Result = num1 / num2
4
F T
Case Result = num1 % num2
5
d==0
F T
Case Print “invalid choice”
6
STOP
CSE OF DEPT, SIR MVIT 6
COMPUTER PROGRAMMING LAB (18CPL17/27)
PROGRAM
#include<stdio.h>
int main()
{
int num1,num2,choice;
int result;
printf("Enter two values\n");
scanf("%d%d",&num1,&num2);
printf("enter your choice\n");
printf("1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n 5.Modulus\n");
scanf("%d",&choice);
switch(choice)
{
case 1: result = num1 + num2;
break;
case 2: result = num1 - num2;
break;
case 3: result = num1 * num2;
break;
case 4: result = num1 / num2;
break;
case 5: result = num1 % num2;
break;
default:printf("invalid choice");
}
printf("The Result = %d",result);
return 0;
}
Ouptut:
3. Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.
Algorithm
Step 1: [start]
Accept a,b,c
d=b*b-4*a*c
If (d==0)
r2 = (-b) / (2*a)
Print r1, r2
End if
Else if (d>0)
End if
Else
Step 5: [Finished]
Stop
Flow Chart
START
Read a,b,c
d=b*b-4*a*c
T F
If
d==0
r1=-b/(2*a), r2= r1 F T
If
d>0
r1= (-b+sqrt(d))/2*a
r2= (-b-sqrt(d))/2*a
Print “The root Print “Roots are
are real and imaginary
equal”, r1, r2
Print “Roots are real
& distinct”, r1, r2
STOP
PROGRAM
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,r1,r2;
printf("Enter non-zero coefficients");
scanf("%f %f %f",&a,&b,&c);
d=(b*b)-4*a*c;
if(d==0)
{
r1=r2=(-b)/(2*a);
printf("Root are equal\n");
printf("Root1 = %f and Root2 = %f",r1,r2);
else if(d>0)
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Roots are real and distinct\n");
printf("Root1 = %f",r1);
printf("Root2 = %f",r2);
else
{
r1=(-b)/(2*a);
r2=sqrt(fabs(d))/(2*a);
printf("Roots are complex\n");
printf("Root1 = %f+i%f \n",r1,r2);
printf("Root2 = %f-i%f \n",r1,r2);
}
}
Ouptut:
4. Develop a program to find the reverse of positive integer and check for palindrome or
not Display appropriate messages.
Algorithm
Step 1: [Start]
Read n
rev=0, m=n
repeat: digit=n%10
till condition
fails n=n/10
rev=rev*10+digital
END LOOP
Step 5: [Check whether reversed and original numbers are same]
if (m==rev)
Step 6: [Finished]
Stop
Flow Chart
START
Read n
rev = 0,m=n
CSE OF DEPT, SIR MVIT 14
COMPUTER PROGRAMMING LAB (18CPL17/27)
while F
n!=0
digit =n%10
n =n/10
rev =rev * 10+digit
F
If
Number is not
m==re
Palindrome
v
aimaginary
T
Number is Palindrome
are imaginary
STOP
PROGRAM
#include<stdio.h>
int main()
{
int rev, digit, n, m;
printf("Enter four digit number");
scanf("%d" ,&n);
m = n;
rev = 0;
while (n!=0)
{
digit = n%10;
n=n/10;
rev = rev*10+digit;
}
printf("Reverse number is %d \n", rev);
if (m==rev)
printf("Number is a palindrome");
else
printf("Not a palindrome");
}
Output:
5. An Electricity board charges the following rates for the use of electricity: for the
first 200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond
300 units Rs 1 per unit. All users are changed a minimum of Rs. 100 as meter
charge. If the total amount is more than Rs 400, then an additional surcharge of
15% of total amount is charged. Write a program to read the name of the user,
number of units consumed and print out the charges.
Algorithm
Step 1: Start
Step 2: Read
Input n;
for(i=0;i<n;i++)
int a[i];
end for
Step 3: Assign ptr=a
Step 4: Interaction & Calculations
for(i=0;i<n;i++)
sum=sum+(*ptr)
ptr++
end for
Step 5: Mean = sum/n
Step 6: Assign ptr=a
Step 7: for(i=0;i<n;i++)
Sumstd=sumstd + pow((*ptr-mean),2)
Ptr++
End for
Step 8: Calculations
Var = sum std/n
Std = sqrt(var)
Step 9: output
Print sum, mean, var, std
Step 10: Finished
Stop
Flow Chart
START
T
Units F
Units
consume consume
d d
>=0&&< >=200&
200 &<300
STOP
PROGRAM
#include<stdio.h>
#include<stdlib.h>
int main()
{
char name[10];
int units;
const int mincharge=100;
const double slab1=0.80;
const double slab2=0.90;
const double slab3=1.00;
const double surcharge=0.15;
double billamt=0.0;
printf("enter the nme of consumer\n");
scanf("%s",name);
printf("enter the number of units consumed\n");
scanf("%d",&units);
billamt=billamt+mincharge;
if(units<=200)
{
billamt=billamt+(units*slab1);
}
else if(units>200&&units<=300)
{
billamt=billamt+((200*slab1)+(units-200)*slab2);
}
else
{
billamt=billamt+(200*slab1)+(100*slab2)+((units-300)*slab3);
}
if(billamt>400)
{
billamt=billamt+surcharge;
}
printf("consumer name=%s\n",name);
printf("units consumed =%d\n",units);
printf("billamount=%f rupees\n",billamt);
}
Output:
Step 1: Start
Step 2: [Read the input]
Read the key number of names
Step 3: [Read the array]
for (i=0; i<n; i++)
Read a[i]
Step 4: [Read key name]
Input key name
Step 5: [Initialize the result]
low =0
high = n-1
Step 6: [Compute result]
while (low <= high)
mid = (low +high)/2
If (strcmp (names [mid], key) ==0)
Print successful and print position
Else if (strcmp (name [mid], key >0))
high = mid-1
Else
low = mid+1
End of while
Step 7: [Print the result]
Print Unsuccessful
Step 8: [Finished]
Stop
Flow Chart
START
Read n
F
for (i=0; i<n; i++)
T
T
Read a[i]
Read key
low = 0
high = n-1
while (low<=high)
F
if (strcmp if (strcmp
F
(a[mid],key (a[mid],ke
)==0 y )>0)
T
T
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
int n,i,low,mid,high;
char a[20][20],key[20];
printf("Enter the number of names\n");
scanf("%d",&n);
printf("Enter the names\n");
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
}
printf("Enter the key\n");
scanf("%s",key); low=0;
high=n-1;
while(low<=high)
{
mid=(high+low)/2;
if(strcmp(a[mid],key)==0)
{
printf("Name found at %d\n",mid+1);
exit(0);
}
else if(strcmp(a[mid],key)>0) high=mid-1;
else low=mid+1;
}
printf("Unsuccessful search \n");
Output:
Or
PROGRAM
#include<stdio.h>
int main()
{
int a[100],i,n,found,mid,key,low,high;
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the element in ascending order \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the key elements \n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<high)
{
mid=(low+high)/2;
if(key==a[mid])
{
found=1;
break;
}
else if(key<a[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(found==1)
{
Output:
7. Implement using functions to check whether the given number is prime and display
appropriate messages(No built-in math function)
Algorithm
Step 1: [Start]
Step 2: [Input]
Read n
Step 3: [Function call]
For (i=n; i<=n2 ; i++)
If (isprime = =(i))
Print prime numbers
End if
End for
Step 4: [Finished]
Stop
IsPrime Function
Step 1: [Start]
Step 2: [computation]
for (i=2; i<=n-1; i++)
If (n%1==0)
Return (0)
End if
End if
End for
Return (1)
Flow Chart
START
Read n1,n2
for(i=n;i<n;i++) F
F If
Isprime(
i)
T
Print prime
number = 1
STOP
Isprime (num)
for(i=2;i<num-1;i++)
T
If
(num
F %i==0
)
T
Return (0) Return (1)
PROGRAM
#include<stdio.h>
#include<stdlib.h>
int Isprime(int n);
int main()
{
int n1,n2,i;
printf("Enter the range of numbers, say n1 and n2\n");
scanf("%d%d",&n1,&n2);
printf("prime number between %d to %d\n",n1,n2);
for(i=n1;i<=n2;i++)
{
if(Isprime(i))
printf("%d\n",i);
}
}
int Isprime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return(0);
}
}
return 1;
Output:
Part-B
Step 5: [Finished]
Stop
Flow Chart
START
Read m,n,p,q
F
If (n!=p) D
Multiplication is
not possiable
F
For (i=0; i<m; i++)
T F
For (j=0; j<n; j++)
T
Read a[i][j]
F
For (j=0; j<q; j++)
T F
For (i=0; i<p; i++)
T
Read b[i][j]
T
C[i][j]=0 C
B
A
CSE OF DEPT, SIR MVIT 35
COMPUTER PROGRAMMING LAB (18CPL17/27)
A B C
D
for (k=0;k<n;k++)
F
for (i=0; i<m; i++)
T
F
for (i=0; i<q; i++)
C[i][j]=0
STOP
PROGRAM
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m,n,p,q,i,j,k,a[10][10],b[10][10],c[10][10],sum;
printf("Enter the size of first matrix a\n");
scanf("%d%d",&m,&n);
printf("Enter the size of second matrix b\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matix multiplication not possible\n");
exit(0);
}
printf("Enter the first matix elements a\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The Matrix a is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the second matix elements b\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
CSE OF DEPT, SIR MVIT 37
COMPUTER PROGRAMMING LAB (18CPL17/27)
}
printf("The Matrix b is \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
C[i][j]=0
for(k=0;k<n;k++)
{
C[i][j]=C[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("Resultant matrix c\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
CSE OF DEPT, SIR MVIT 38
COMPUTER PROGRAMMING LAB (18CPL17/27)
9. Develop a program to compute Sin(x) using Taylor series approximation. Compare your
result with the built-in Library function. Print both the result with appropriate messages.
Algorithm
Step 1: [Start]
Step 6: [Finished]
Stop
Flow Chart
START
Read degree n
x = (degree * pi)/180
term = x
sum = term
F F
for (i=3;i<=n;i+=2)
Print sum
STOP
PROGRAM
#include<stdio.h>
#include<math.h>
#define PI 3.142
int main()
{
int n,i;
float deg,x,sum=0,term=0;
printf("Enter number of terms, say n\n");
scanf("%d",&n);
printf("Enter the degree\n");
scanf("%f",°);
x=(deg*PI)/180;
printf("In Radians = %f \n",x);
term=x;
sum=term;
for(i=3;i<=n;i+=2)
{
term=(-term*x*x)/(i*(i-1));
sum=sum+term;
}
printf("sin(%f)=%f/n",deg,sum);
printf("Inbuilt function Sin(%f) = %f \n",deg,sin(x));
printf("User function Sin(%f) = %f",deg,sum);
}
Output:
Algorithm:
Step 1: start
Step 2: Enter your choice
1. Length 2. Comparison 3.Concatinate
Read choice
Step 3: compute switch choice
Switch (choice)
{
Case 1: enter string; result=length (str);
Length of string =%d
Case 2: enter 2 strings;
Result = compare (str1, str2)
Case 3: enter 2 strings;
Result = Concatinate (str1, str2)
}
Step 4: stop
Function definition
Step 1: Receive value from actual parameter
Step 2: Return (1), if choice is 1
Return (1)/ (0)/ (-1), if choice is 2
Concatinate str1 & str2, if choice is 3
Step 3: Stop
Flow Chart
START
Read choice
T Result = compare
Case
2 (str1, str2)
T If F
result
==0
T F
Strings are If
equal result
>0
T
While(str[i]!=’\0’ F
T
i++
STOP return (1)
PROGRAM
#include<stdio.h>
#include<string.h>
int length(char str[100]);
int compare(char s1[100],char s2[100]);
void concat(char s1[100],char s2[100]);
int main()
{
int option,result;
char str[100],s1[100],s2[100];
do
{
printf("1.String length \n");
printf("2.string comparision \n");
printf("3.string concatenation \n");
printf("4.quit \n");
printf("enter your choice \n");
scanf("%d",&option);
switch (option)
{
case 1:printf("enter string \n");
scanf("%s",str);
result=length(str);
printf("the length of string=%d\n",result);
break;
case 2:printf("enter 1st string\n");
scanf("%s",s1);
int i=0;
while(s1[i]!='\0')
{
if(s1[i]>s2[i])
return (1);
else if(s1[i]<s2[i])
return (-1);
i++;
}
return 0;
}
void concat(char s1[100],char s2[100])
{
int i,j;
i=0;
while(s1[i]!='\0')
i++;
for(j=0;s2[j]!='\0';i++,j++)
s1[i]=s2[j];
s1[i]='\0';
}
Output:
11. Develop a program to sort the given set of N number using Bubble sort
Algorithm
Step 1: [start]
Step 5: [Finished]
Stop
Flow Chart
START
Read n
T F
for (i=0;i<n;i++)
Read a[i]
F for (i=1;i<=n;i++)
for (j=0;i<n-i;j++)
F
T
term =a[j]
a[j]=a[j+1]
a[j+1]=temp
F
for
(i=0;i<n;i++)
T
PROGRAM
#include<stdio.h>
int main()
{
int a[20],n,i,temp=0,j;
printf("Enter number of array elements\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted array is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Output:
12. Develop a program to find the square root of a given number N and execute for all
possible Input with appropriate messages. Note: Don’t use library function sqrt(n).
Algorithm
Step 1: [Start]
Step 2: [Initialize]
sqrt = n/2
temp = 0
END LOOP
Step 5: [Finished]
Stop
Flow Chart
START
sqrt = n/2
temp = 0
while
temp!=sqrt
F
temp = sqrt
sqrt = (n/sqrt +Tsqrt)/2
Print sqrt
Palindrome
aimaginary
STOP
(OR)
PROGRAM
#include<stdio.h>
int main()
{
float sqrt,temp,n;
printf("Enter a number\n");
scanf("%f",&n);
sqrt=n/2;
temp=0;
while(temp!=sqrt)
{
temp=sqrt;
sqrt=(n/sqr+sqr)/2;
}
printf("The square root of the number is %f",sqrt);
Algorithm
Step 1: [Start]
Read n
for(i=1;i*i<=n;i++);
i--;
for(j=0.001;(i+j)*(i+j)<=n;j=j+0.001);
j=j-0.001
sqrt=i+j
Step 5: [Finished]
Stop
Flow Chart
START
Read n
T
F
for(i=1;i*i<=n;i++);
i--
for(j=0.001; F
(i+j*(i+j)<=n;j+=0.001)
;
T
j = j-0.001
sqrt = i+j
Print sqrt
STOP
PROGRAM
#include<stdio.h>
int main()
{
int i,n;
float j;
printf(“enter the value of n:”);
scanf(“%d”, &n);
for (i=1;i*i<=n;i++);
i--;
for (j=0.001;(i+j)*(i+j)<=n;j=j+0.001);
j=j-0.001;
sqrt = i+j;
printf(“the square root is %d \n is %f\n”, n, sqrt);
Output:
13. Implement structures to read, write, and compute average-marks and the students
scoring above and below the average marks for a class of N students.
Algorithm
Step 1: [Start]
Step 2: Take input
Read n
Step 3: Print (“enter student details”)
For (i=0;i<n;i++)
Printf(“enter name,usn,marks”)
Average=average\n
For (i=0; i<n; i++)
Printf(“name,usn,marks”);
Step 4: if else checking
if(st[i].marks<average)
Printf(“students mark is below average”)
Else
Printf(“students marks is above average”)
Step 5: Stop
Flow Chart
START
Read n
For (i=0;i<n;i++)
temp!=sqrt
Average = Average+s[i].marks
F
Average = Average/2
T
For (i=0;i<n;i++)
T
temp!=sqrt
Print name,usn,marks
STOP
If
st[i]mark
s<averag
e
&&
Students marks is below year%10 Students marks is above
average 0! 0 average
||
year%40
0==0
CSE OF DEPT, SIR MVIT 62
COMPUTER PROGRAMMING LAB (18CPL17/27)
PROGRAM
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char name[20];
int usn;
int marks;
}
stud;
int main()
{
int i,n;
float avg=0;
stud st[50];
printf("enter the number of students\n");
scanf("%d",&n);
printf("enter the student details\n");
for(i=0;i<n;i++)
{
printf("/n name:");
scanf("%s",st[i].name);
printf("/n usn:");
scanf("%d",&st[i].usn);
prinf("\n marks:");
scanf("%d",&st[i].marks);
avg=avg+st[i].marks;
}
avg=avg/n;
for(i=0;i<n;i++)
{
printf("\n name \t %s",st[i].name);
printf("\n usn \t %d",st[i].usn);
printf("\n marks\t:%d",st[i].marks);
if(st[i].marks<avg)
printf("the student is below the average\n");
else
printf("the student is above average\n");
}
}
Ouptut:
14. Develop a program using pointers to compute the sum and standard deviation all
elements stored in an array of n real numbers.
Algorithm
Step 1: [Start]
Step 2: :[Read ]
Input n
for (i=0; i<n;i++)
Input a[i]
End for
Step 3: Assign ptr = a
Step 4:[Iteration and Calculation]
for (i=0; i<n; i++)
sum = sum + *ptr
ptr++;
End for
Step 5: mean = sum/n
Step 6: Assign ptr = a
Step 7: for (i=0; i<n; i++)
stdsum = stdsum + pow((*ptr – mean),2)
ptr++
end for
Step 8:[Calucation]
var = stdsum/n
std = sqrt(var)
Step 9: [Output]
Print sum , mean , var,std
Step 10: [Finished]
Stop
Flow Chart
START
sum = 0, sumstd = 0
Read n
T
Read a[i]
ptr = a
F
for (i=0; i<=n; i++)
T
sum = sum + *ptr ptr++
mean = sum/n
ptr = a
var = sumstd/n
std = sqrt(var)
STOP
PROGRAM
#include<stdio.h>
#include<math.h>
int main()
{
float a[10],*ptr,mean,sum=0,var,sumstd=0,std;
int n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+(*ptr);
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
var=sumstd/n;
std=sqrt(var);
printf("Sum = %f \n",sum);
printf("Mean = %f \n",mean);
printf("Variance = %f \n",var);
printf("Standard Deviation = %f",std);
}
Output:
Algorithm
Step 1: Start
Step 2: Take input
Enter binary num
Step 3: Function call
Step 4: Receiver return value
Print “equivlant decimal num=%d”
Step 5: Stop
Function definition
Flow Chart
START
STOP
T If (! F
(binary
/10))
STOP
PROGRAM
#include<stdio.h>
int binarytodecimal(int binarynum)
{
if(!(binarynum/10))
return binarynum;
return(binarynum%10 + binarytodecimal(binarynum/10)*2);
}
int main()
{
int binarynum;
printf("enter the binary number \n");
scanf("%d", &binarynum);
printf("equivalant decimal number is %d",binarytodecimal(binarynum));
return 0;
}
Output: