You are on page 1of 74

COMPUTER PROGRAMMING LAB

(18CPL17/27)

C PROGRAMMING WITH LINUX

Open the terminal window:

Create a directory:
To create a directory type the following commands and press enter.
Syntax: mkdir <directoryname>

Use the created directory:


To use the created directory use the change directory command.
Syntax:cd <directoryname>

Use the text editors:


There are several text editors already installed in LINUX OS like Gedit, Leafpad, Gvim etc

CSE OF DEPT, SIR MVIT 1


COMPUTER PROGRAMMING LAB
(18CPL17/27)

To use Gedit text editor type the following commands


Syntax:gedit <filename.c>
As we are programming with C, the extension will be '.c

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.

Compiling the saved program:


To compile the saved program, go to the terminal window and type the following
command. Syntax: cc filename.c

CSE OF DEPT, SIR MVIT 2


COMPUTER PROGRAMMING LAB
(18CPL17/27)

Get the Output:


To get the output use the following command in the terminal
window. Syntax: ./a.out

Some important commands in Linux.

•To return to the root directory.


Syntax: cd /
•To delete an existing directory.
Syntax: rm -r <directoryname>
•To clear terminal window.
Syntax: clear

NOTE:
When using the 'math.h' header file in the C program, use the following command to
compile. Syntax: cc -lm filename.c

CSE OF DEPT, SIR MVIT 3


COMPUTER PROGRAMMING LAB (18CPL17/27)

Lab Programs

1. Familiarization with computer hardware and programming environment, concept of


naming the program files, storing, execution and debugging. Taking any simple C-code.

CSE OF DEPT, SIR MVIT 4


COMPUTER PROGRAMMING LAB (18CPL17/27)

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]

Step 2: [Read two values]

Input num1, num2

Step 3: [display the menu-simple calculation]

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus

Step 4: [Enter choice]


Read choice
Step 5: [Switch case]
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;
Step 6: [print the result]
Output result
Step 7: [Stop]

CSE OF DEPT, SIR MVIT 5


COMPUTER PROGRAMMING LAB (18CPL17/27)

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

Invalid choice Print result

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;
}

CSE OF DEPT, SIR MVIT 7


COMPUTER PROGRAMMING LAB (18CPL17/27)

Ouptut:

CSE OF DEPT, SIR MVIT 8


COMPUTER PROGRAMMING LAB (18CPL17/27)

3. Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.

Algorithm

Step 1: [start]

Step 2: [take input]

Accept a,b,c

Step 3: [Find discriminate]

d=b*b-4*a*c

Step 4: [Check the nature]

If (d==0)

Print ‘roots are equal & equal’


r1 = (-b) / (2*a)

CSE OF DEPT, SIR MVIT 9


COMPUTER PROGRAMMING LAB (18CPL17/27)

r2 = (-b) / (2*a)
Print r1, r2
End if

Else if (d>0)

Print ‘roots are equal & distinct’


r1 = (-b+sqrt(d))/(2*a)
r2= (-b-sqrt(d))/(2*a)
Print r1, r2

End if

Else

Print ‘roots are imaginary’

Step 5: [Finished]
Stop

Flow Chart

START

Read a,b,c

d=b*b-4*a*c

T F
If
d==0

CSE OF DEPT, SIR MVIT 10


COMPUTER PROGRAMMING LAB (18CPL17/27)

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);

CSE OF DEPT, SIR MVIT 11


COMPUTER PROGRAMMING LAB (18CPL17/27)

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:

CSE OF DEPT, SIR MVIT 12


COMPUTER PROGRAMMING LAB (18CPL17/27)

4. Develop a program to find the reverse of positive integer and check for palindrome or
not Display appropriate messages.
Algorithm
Step 1: [Start]

Step 2: [Read no]

Read n

Step 3: [assign reverse to 0 and n to m]

rev=0, m=n

CSE OF DEPT, SIR MVIT 13


COMPUTER PROGRAMMING LAB (18CPL17/27)

Step 4: [reverse the number]

while (n!=0) BEGIN LOOP

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)

Print “number is a palindrome”


Else
Print “number is not a palindrome”
end if

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

CSE OF DEPT, SIR MVIT 15


COMPUTER PROGRAMMING LAB (18CPL17/27)

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");
}

CSE OF DEPT, SIR MVIT 16


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 17


COMPUTER PROGRAMMING LAB (18CPL17/27)

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

CSE OF DEPT, SIR MVIT 18


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart

START

Read consumer name

Read number of units


consumed

Meter charge = 100.0

T
Units F
Units
consume consume
d d
>=0&&< >=200&
200 &<300

Charge=units Charge=units consumed*0.90


consumed*0.80 surcharge=0.0
surcharge=0.0
Charge=units-consumed+1+meter charge
surcharge=unit-consumed*0.15

Print total charge Print total charge Print total


=charge+surcharge =charge+surcharge charge=charge+surc
+metercharge +metercharge harge

CSE OF DEPT, SIR MVIT 19


COMPUTER PROGRAMMING LAB (18CPL17/27)

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)
{

CSE OF DEPT, SIR MVIT 20


COMPUTER PROGRAMMING LAB (18CPL17/27)

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);
}

CSE OF DEPT, SIR MVIT 21


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 22


COMPUTER PROGRAMMING LAB (18CPL17/27)

6. Introduce 1D Array manipulation and implement Binary search


Algorithm

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

CSE OF DEPT, SIR MVIT 23


COMPUTER PROGRAMMING LAB (18CPL17/27)

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)

mid = (low + high)/2

F
if (strcmp if (strcmp
F
(a[mid],key (a[mid],ke
)==0 y )>0)

T
T

Print successful and


key position search

high = mid + 1 low = mid - 1

STOP Print unsuccessful search

CSE OF DEPT, SIR MVIT 24


COMPUTER PROGRAMMING LAB (18CPL17/27)

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");

CSE OF DEPT, SIR MVIT 25


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 26


COMPUTER PROGRAMMING LAB (18CPL17/27)

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)
{

CSE OF DEPT, SIR MVIT 27


COMPUTER PROGRAMMING LAB (18CPL17/27)

printf("key=%d found at position %d \n",key,mid+1);


}
else
{
printf("key=%d not found \n",key);
}
}

CSE OF DEPT, SIR MVIT 28


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 29


COMPUTER PROGRAMMING LAB (18CPL17/27)

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)

CSE OF DEPT, SIR MVIT 30


COMPUTER PROGRAMMING LAB (18CPL17/27)

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)

CSE OF DEPT, SIR MVIT 31


COMPUTER PROGRAMMING LAB (18CPL17/27)

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;

CSE OF DEPT, SIR MVIT 32


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 33


COMPUTER PROGRAMMING LAB (18CPL17/27)

Part-B

8. Develop a program to introduce 2D Array manipulation and implement Matrix


multiplication and ensure the rules of multiplication are checked.
Algorithm
Step 1: [start]
Step 2: [Take input] Read m,n,p,q
Step 3: Check if (n!=p)
Print (“Multiplication not Possible”);
else
[Read matrix 1]
For (i=0; i<m; i++)
For (j=0; j<m; j++)
Read a[i][j]
[Read matrix 2]
for (j=0; j<q; j++)
for (i=0; i<p; i++)
Read b[i][j]
[Compute multiplication]
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])

Step 4: [print the result]


For (i=0; i<m; i++)
For (j=0; j<q; j++)
Print c[i][j]

Step 5: [Finished]
Stop

CSE OF DEPT, SIR MVIT 34


COMPUTER PROGRAMMING LAB (18CPL17/27)

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]

For (i=0; i<m; i++)


F
T F
For (j=0; j<q; 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++)

C[i][j] = c[i][j] + a[i][k] * b[k][j]

F
for (i=0; i<m; i++)
T
F
for (i=0; i<q; i++)

C[i][j]=0

STOP

PROGRAM

CSE OF DEPT, SIR MVIT 36


COMPUTER PROGRAMMING LAB (18CPL17/27)

#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)

CSE OF DEPT, SIR MVIT 39


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 2: [Read the degree n]

Step 3: [Initialize the value]


x = (degree * pi) /180
term = x
sum = term

Step 4: [Compute the result]


for (i=3;i<=n;i+=2)
term = ( -term * x*x) / (i * (i-1))
sum = sum + term
end for

Step 5: [Print result]


Print “USING LIBRARY FUNCTION”
degree,sum
Printf “sin(%f) = %f\n”, degree , sum

Step 6: [Finished]

Stop

CSE OF DEPT, SIR MVIT 40


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart

START

Read degree n

x = (degree * pi)/180
term = x
sum = term

F F
for (i=3;i<=n;i+=2)

term = (-term * x * x)/


(i* (i-1))
sum =sum + term

Print sum

Print sin(x) using TLIBRARY


FUNCTION are imaginary

STOP

PROGRAM

CSE OF DEPT, SIR MVIT 41


COMPUTER PROGRAMMING LAB (18CPL17/27)

#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",&deg);
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);
}

CSE OF DEPT, SIR MVIT 42


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 43


COMPUTER PROGRAMMING LAB (18CPL17/27)

10. Write function to implement string operations such as compare, concatenate,


string length. Convince the parameters passing techniques.

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

CSE OF DEPT, SIR MVIT 44


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart
START

Read choice

Result = length (str)


T
Case Length of string = %d
1
F

T Result = compare
Case
2 (str1, str2)

T If F
result
==0
T F
Strings are If
equal result
>0

str1 > str2 Str1 < str2

T Concat (str1, str2) F


Case
3 Result = %s

Int length(char str[100])

T
While(str[i]!=’\0’ F

T
i++
STOP return (1)

CSE OF DEPT, SIR MVIT 45


COMPUTER PROGRAMMING LAB (18CPL17/27)

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);

CSE OF DEPT, SIR MVIT 46


COMPUTER PROGRAMMING LAB (18CPL17/27)

printf("enter 2nd string\n");


scanf("%s",s2);
result=compare(s1,s2);
if(result==0)
printf("strings are equal \n");
else
printf("strings are not equal \n");
break;
case 3: printf("enter two strings\n");
scanf("%s%s",s1,s2);
concat(s1,s2);
printf("result=%s \n",s1);
break;
}
}
while(option<=3);
return 0;
}
int length(char str[100])
{
int i=0;
while(str[i]!='\0')
i++;
return(i);
}
int compare(char s1[100],char s2[100])
{

CSE OF DEPT, SIR MVIT 47


COMPUTER PROGRAMMING LAB (18CPL17/27)

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';
}

CSE OF DEPT, SIR MVIT 48


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 49


COMPUTER PROGRAMMING LAB (18CPL17/27)

11. Develop a program to sort the given set of N number using Bubble sort

Algorithm

Step 1: [start]

Step 2: [Read elements of an array]


Read n

Step 3: [Complie the number of passes required : n=1]


for (i=0; i<n; i++)
Read a[i]
To acess element in each pass
for (i=0; i<n; i++)
Print a[i]
End for
for (i=1; i<n; i++)
for (j=0; j<n-i; j++)
if a[j] > = a[i++]
exchange if out of order
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp

Step 4: [Print sorted array]


for (i=0; i<n; i++)
Print array a[i]

Step 5: [Finished]
Stop

CSE OF DEPT, SIR MVIT 50


COMPUTER PROGRAMMING LAB (18CPL17/27)

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

Print sorted list a[i]

CSE OF DEPT, SIR MVIT STOP 51


COMPUTER PROGRAMMING LAB (18CPL17/27)

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]);
}

CSE OF DEPT, SIR MVIT 52


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 53


COMPUTER PROGRAMMING LAB (18CPL17/27)

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]

Enter the value of n

Step 2: [Initialize]

sqrt = n/2
temp = 0

Step 3: [check condition]


BEGIN LOOP
repeat: While (sqrt! = temp)
till condition temp = sqrt
fails sqrt = (n/sqrt + sqrt)/2

END LOOP

Step 4: [Print the square root]


Print sqrt

Step 5: [Finished]

Stop

CSE OF DEPT, SIR MVIT 54


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart

START

Enter the value of n

sqrt = n/2
temp = 0

while
temp!=sqrt
F

temp = sqrt
sqrt = (n/sqrt +Tsqrt)/2

Print sqrt
Palindrome
aimaginary

STOP

CSE OF DEPT, SIR MVIT 55


COMPUTER PROGRAMMING LAB (18CPL17/27)

(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);

CSE OF DEPT, SIR MVIT 56


COMPUTER PROGRAMMING LAB (18CPL17/27)

Algorithm
Step 1: [Start]

Step 2: [Input the number n]

Read n

Step 3: [Finding square root]

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 4: [Output square root]


print sqrt

Step 5: [Finished]
Stop

CSE OF DEPT, SIR MVIT 57


COMPUTER PROGRAMMING LAB (18CPL17/27)

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

CSE OF DEPT, SIR MVIT 58


COMPUTER PROGRAMMING LAB (18CPL17/27)

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);

CSE OF DEPT, SIR MVIT 59


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 60


COMPUTER PROGRAMMING LAB (18CPL17/27)

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

CSE OF DEPT, SIR MVIT 61


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart

START

Read n

Enter the student details

For (i=0;i<n;i++)
temp!=sqrt

Enter the student details

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;

CSE OF DEPT, SIR MVIT 63


COMPUTER PROGRAMMING LAB (18CPL17/27)

}
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");
}
}

CSE OF DEPT, SIR MVIT 64


COMPUTER PROGRAMMING LAB (18CPL17/27)

Ouptut:

CSE OF DEPT, SIR MVIT 65


COMPUTER PROGRAMMING LAB (18CPL17/27)

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

CSE OF DEPT, SIR MVIT 66


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart
START

sum = 0, sumstd = 0

Read n

for (i=0; i<n; i++)


F

T
Read a[i]

ptr = a

F
for (i=0; i<=n; i++)

T
sum = sum + *ptr ptr++

mean = sum/n
ptr = a

for (i=0; i<n; i++) F

sumstd = sumstd +pow((*ptr –mean),2)


ptr++

var = sumstd/n
std = sqrt(var)

Print sum, mean, variance, standard


deviation

STOP

CSE OF DEPT, SIR MVIT 67


COMPUTER PROGRAMMING LAB (18CPL17/27)

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);
}

CSE OF DEPT, SIR MVIT 68


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 69


COMPUTER PROGRAMMING LAB (18CPL17/27)

15. Implement Recursive functions for binary to Decimal Conversion.

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

Step 1: Receive the value from actual


Parameter to formal parameter
Step 2: If (! (Binary num/10))
return (binary num)
return (binary num%10+binary to decimal(binary num/10)*2);
Step 3: End if

CSE OF DEPT, SIR MVIT 70


COMPUTER PROGRAMMING LAB (18CPL17/27)

Flow Chart

START

Read Binary num

Result = binary to decimal (binary num)

Print “equivalent decimal num is


%d”

STOP

Result = binary to decimal(binary num)

T If (! F
(binary
/10))

STOP

Return (binary num) return(binary


num%10)binary to decimal(binary
num/10)*2)

CSE OF DEPT, SIR MVIT 71


COMPUTER PROGRAMMING LAB (18CPL17/27)

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;
}

CSE OF DEPT, SIR MVIT 72


COMPUTER PROGRAMMING LAB (18CPL17/27)

Output:

CSE OF DEPT, SIR MVIT 73


COMPUTER PROGRAMMING LAB (18CPL17/27)

CSE OF DEPT, SIR MVIT 74

You might also like