0% found this document useful (0 votes)
35 views57 pages

Exp 1-20

Uploaded by

bleo91502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views57 pages

Exp 1-20

Uploaded by

bleo91502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 08/12/2022

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 1

AIM: Write a program to swap two variables values with and without using third variables.
Write algorithm and draw flowchart for the same.

ALGORITHM:

(WITHOUT USING THIRD VARIABLE)

Step 1: Start
Step 2: Declare three variables x, y, t.
Step 3: Enter values of variables x & y.
Step 4: x = x + y
Step 5: y = x - y
Step 6: x = x - y
Step 7: End

(WITH USING THIRD VARIABLE)

Step 1: Start
Step 2: Declare three variables x, y, t.
Step 3: Enter values of variables x & y.
Step 4: Swap value of x with t.
Step 5: Swap value of y with x.
Step 6: Swap value of swap value of t with y.
Step 7: End

P a g e 1 | 57
FLOWCHART:

(WITHOUT USING THIRD VARIABLE)

START

Declare x & y

x=x+y
y=x-y
x=x-y

Print x & y

END

(WITH USING THIRD VARIABLE)

START

Declare x & y

t=x
x=y
y=t

Print x & y
P a g e 2 | 57
END

CODE:
(WITHOUT USING THIRD VARIABLE)

#include <stdio.h>

int main()
{
int x=10,y=20;
clrscr();
printf("THE INITAL VALUES ARE\n");
printf("x=%d\ny=%d\n",x,y);

x = x + y;
y = x - y;
x = x - y;

printf("THE SWAPPED VALUES ARE\n");


printf("x=%d\ny=%d\n",x,y);
getch();
return 0;
}

(WITH USING THIRD VARIABLE)

#include <stdio.h>

int main()
{
int x=10,y=20,t;
clrscr();
printf("THE INITAL VALUES ARE\n");
printf("x=%d\ny=%d\n",x,y);

t=x;
x=y;
P a g e 3 | 57
y=t;

printf("THE SWAPPED VALUES ARE\n");


printf("x=%d\ny=%d\n",x,y);
getch();
return 0;
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to swap


two variables values with and without using third variables.

P a g e 4 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 13/12/2022

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 2

AIM: Write a program to check odd or even number:


(a) using modulus operator
(b) using conditional operator.

CODE:

(USING MODULUS OPERATOR)

#include <stdio.h>

int main()
{
int a;
clrscr();
printf("Enter A Number\n");
scanf("%d", &a);

if(a%2==0)
{
printf("%d is an even number",a);
}
else
{
printf("%d is an odd number",a);
}

getch();
return 0;
}

P a g e 5 | 57
(USING CONDITIONAL OPERATOR)

#include <stdio.h>

int main()
{
int num, isEven;
clrscr();
printf("Enter an Integer\n");
scanf("%d", &num);

isEven = (num%2 == 1) ? 0 : 1;

if(isEven == 1)
printf("%d is Even\n", num);
else
printf("%d is Odd\n", num);
getch();
return 0;
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to check


odd or even number:
(a) using modulus operator
(b) using conditional operator.

P a g e 6 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 15/12/2022

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 3

AIM: Design and develop a C program to read a year as an input and find whether it is leap
year or not. Also consider the end of the centuries. Write algorithm and draw flowchart for the
same.

ALGORITHM:

STEP 1: START
STEP 2: Declare an integer variable
STEP 3: Enter a year
STEP 4: If the year is divisible by 400 then it is a leap year, if not but divisible but 100 it is
not a leap year, if not but divisible by 4 then it is a leap year.
STEP 5: END

FLOWCHART:

START

ENTER A YEAR

YES NO
Year / 400

IT IS A LEAP YEAR YES NO


Year / 100

P a g e 7 | 57
IT IS NOT LEAP YEAR YES NO
Year / 4

IT IS A LEAP YEAR IT IS NOT A


LEAP YEAR

END

CODE:

#include <stdio.h>
int main()
{
int y;
clrscr();
printf("Enter a year: ");
scanf("%d);

if (y % 400 == 0)
{
printf("%d is a leap year.", y);
}

else if (y % 100 == 0)
{
printf("%d is not a leap year.", y);
}

else if (y % 4 == 0)
{
printf("%d is a leap year.", y);
}

else
{
printf("%d is not a leap year.", y);
}
getch();
P a g e 8 | 57
return 0;
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to read a


year as an input and find whether it is leap year or not.

P a g e 9 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 15/12/2022

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 4

AIM: Write a C program to find the sum of individual digits of a 3-digit number.

CODE:

#include <stdio.h>

int main()
{
int x,y,s=0;
clrsccr();
printf("Enter a three digit number: ");
scanf("%d", &x);

while(x!=0)
{
y=x%10;
s=s+y;

x=x/10;
}

printf("THE SUM OF THE DIGITS IS: %d",s);


getch();
return 0;
}

OUTPUT:

P a g e 10 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to find the
sum of individual digits of a 3-digit number.

P a g e 11 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 20/12/2022

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 5

AIM: Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and
c) of a Quadratic
equation (ax2 + bx + c=0) as input and compute all possible roots. Implement a C program for
the developed
flowchart/algorithm and execute the same to output the possible roots for a given set of
coefficients with
appropriate messages.

ALGORITHM:

STEP 1: START
STEP 2: Enter values of a & b.
±√
STEP 3: Compute the roots using x,y = .
STEP 4: Print x & y.
STEP 5: END.

FLOWCHART:
START

ENTER a, b & c

±√
x,y =

Print x & y

END
P a g e 12 | 57
CODE:

#include <stdio.h>
#include <math.h>

int main()
{
int a,b,c,x;
float y,z;
clrscr();
printf("QUADRATIC EQUATION: ax^2 + bx + c=0\n");

printf("ENTER THE VALUES OF a:\n");


scanf("%d",&a);

printf("ENTER THE VALUES OF b:\n");


scanf("%d",&b);

printf("ENTER THE VALUES OF c:\n");


scanf("%d",&c);

y=((-b+sqrt((pow(b,2)-4*a*c)))/2*a);
z=((-b-sqrt((pow(b,2)-4*a*c)))/2*a);

printf("THE ROOTS OF THE EQUATION ARE: %.2f & %.2f",y,z);


getch();
return 0;
}

OUTPUT:

P a g e 13 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to design
and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a
Quadratic
equation (ax2 + bx + c=0) as input and compute all possible roots.

P a g e 14 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 22/12/2022

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 6

AIM: Write a program to count the number of digits in a given integer. Write a program to
count the number of digits in a given integer.

CODE:

#include <stdio.h>

int main()
{
int x,y,s=0;
clrscr();
printf("Enter a number: ");
scanf("%d", &x);

while(x!=0)
{
x=x/10;
s=s+1;
}

printf("THE SUM OF THE DIGITS IS: %d",s);


getch();
return 0;
}

OUTPUT:

P a g e 15 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to count the
number of digits in a given integer. Write a program to count the number of digits in a given
integer.

Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 03/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 7

AIM: Write a menu driven program to perform simple arithmetic operations based on the
user’s choice. The user will indicate the operation to be performed using the signs e.g. + for
addition, etc. Write an algorithm and draw flowchart for same.

ALGORTHM:

STEP 1: START
STEP 2: Print the menu
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%)
STEP 3: Perform the respective function.
STEP 4: END

FLOWCHART:
START

ENTER a and b

TRUE a+b
1.Addition

P a g e 16 | 57
FALSE

TRUE a-b
2.Subtraction

FALSE

TRUE
3.Multiplication a-b
on

FALSE

4.Division TRUE a/b

FALSE

5.Modulus a%b

ERROR

END

CODE:

P a g e 17 | 57
#include <stdio.h>

int main()
{
int a,b,s;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);

printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.multiplication\n");
printf("4.Division\n");
printf("5.Modulus\n");
printf("\nSelect an opertion\n");
scanf("%d",&s);

switch(s)
{
case 1:

{
printf("The addition of the numbers is: %d\n",a+b);
break;
}
case 2:
{
printf("The subtraction of the numbers is: %d\n",a-b);
break;
}
case 3:
{
printf("The multiplication of the numbers is: %d\n",a*b);
break;
}
case 4:
{
printf("The division of the numbers is: %d\n",a/b);
break;
}
case 5:
{
P a g e 18 | 57
printf("The modulus of the numbers is: %d\n",a%b);
break;
}
default:
{
printf("ERROR\n");
}

}
getch();
return 0;
}

OUTPUT:

P a g e 19 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to perform
simple arithmetic operations based on the user’s choice.

P a g e 20 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 05/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 8

AIM: Write a program to read a number of more than one digit, reverse the number and
display the sum of digits of numbers. Write algorithm and draw flowchart for the same.

ALGORITHM:

STEP 1: START
STEP 2: Enter a number
STEP 3: Declare variables for the number, reverse number = 0, sum =0 and y.
STEP 4: Initialize while loop until the number becomes zero.
STEP 5: The number x is the modulus of x with 10, the reverse number is reversed num into
10 + the number x.
STEP 6: The number is updated after being divided buy 10.
STEP 7: The sum is the addition of sum and the modulus of the number with 10.
STEP 8: Print the values of the number, the reversed number and the sum of the digits.
STEP 9: END

FLOWCHART:

START

ENTER the number

x≠0 END
NO
YES

y=x%10

P a g e 21 | 57
r = r*10 + y

s=s+y

x = x/10

CODE:
#include <stdio.h>

int main()
{
int x,r=0,s=0,y;
clrscr();
printf("ENTER A NUMBER: ");
scanf("%d",&x);

printf("THE NUMBER ENTERED IS: %d\n",x);

while(x!=0)
{
y=x%10;
r=(r*10) + y;
x=x/10;
s=s+y;
}

printf("THE REVERSED NUMBER ENTERED IS: %d\n",r);


printf("THE SUM OF DIGITS IS: %d\n",s);
getch();
return 0;
}

OUTPUT:

P a g e 22 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to read a
number of more than one digit, reverse the number and display the sum of digits of numbers.
Write algorithm and draw flowchart for the same.

P a g e 23 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 10/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 9
AIM: Write programs to display each of the following patterns. Write algorithm and draw
flowchart for the same.

A)

ALGORITHM:

STEP 1: START
STEP 2: Declare two variables for controlling loops.
STEP 3: Initiate two for loops in increasing order for of rows and in decreasing order for no
of columns, each 5.
STEP 4: Print the values of j.
STEP 5: END

FLOWCHART:

START

Initiate two for loops first in


increasing order & second in
decreasing order

Print the
values of j

P a g e 24 | 57
END
CODE:
#include <stdio.h>

int main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
return 0;
}

OUTPUT:

P a g e 25 | 57
B)

ALGORITHM:
STEP 1: START
STEP 2: Declare two variables to control printing letters and spaces.
STEP 3: Initiate four for loops to control no. of rows , printing spaces and two for printing
letters.
STEP 4: Print the letters.
STEP 5: END

FLOWCHART:
START

Initiate four for loops to control


no. of rows , printing spaces
and two for printing letters

Print the
values of j

END
P a g e 26 | 57
CODE:
#include <stdio.h>

int main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
{
printf(" ");
}

for(j=1;j<=i;j++)
{
printf("%c",j+64);
}

for(j=i-1;j>=1;j--)
{
printf("%c",j+64);
}
printf("\n");
}

getch();
return 0;
}

OUTPUT:

P a g e 27 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to display
each of the following patterns.

P a g e 28 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 10/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 10

AIM: Write a C program to find maximum and minimum between two numbers using
functions. Write algorithm and draw flowchart for the same.

ALGORITHM:

STEP 1: START
STEP 2: Define functions for finding maximum and minimum numbers using relational
operators.
STEP 3: Enter two numbers.
STEP 4: Compare the values using functions.
STEP 5: Print the maximum and minimum numbers.
STEP 6: END

FLOWCHART:

START

Define the functions for


maximum and minimum number

ENTER the numbers

Compare using
functions

Print maximum &


minimum values
P a g e 29 | 57
END

CODE:

#include <stdio.h>

int max(int n1,int n2)


{
if(n1>n2)
{
return n1;
}
else
{
return n2;
}
}

int min(int n1,int n2)


{
if(n1<n2)
{
return n1;
}
else
{
return n2;
}
}

int main()
{

P a g e 30 | 57
int x,y;
clrscr();
printf("ENTER TWO NUMBER: \n");
scanf("%d%d",&x,&y);

printf("THE MAXIMUM NUMBER IS: %d\n",max(x,y));


printf("THE MINIMUM NUMBER IS: %d\n",min(x,y));
getch();
return 0;
}

OUTPUT:

CONCLUSION: Thus, we have successfully studied and implemented a program to find


maximum and minimum between two numbers using functions. Write algorithm and draw
flowchart for the same.

P a g e 31 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 17/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 11

AIM: Write C program to find GCD of two integers by using recursive function. Write
algorithm and draw flowchart for the same.

ALGORITHM:

main() function
STEP 1: Start
STEP 2: Input n1, n2
STEP 3: hcf=gcd(n1,n2) (Calling the recursive function)
STEP 4: If n2 is not equal to 0, then return the function with parameters (n2, n1%n2)
STEP 5: Else, return n1
STEP 6: Stop

gcd function()
Step 1: START
STEP 2: If b==0, return a, else return gcd(a%b).
STEP 3: STOP

FLOWCHART:

P a g e 32 | 57
CODE:

#include <stdio.h>
#include <conio.h>

int GCD(int a,int b)


{
if(b==0)
{
return a;
}
else
{
return GCD(b,a%b);
}
}

int main()
{
int x,y,res;
printf("ENTER THE TWO NUMBERS TO FIND GCD FOR: ");
scanf("%d%d",&x,&y);

res=GCD(x,y);
printf("THE GCD OF %d & %d IS: %d",x,y,res);

return 0;
}
OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find


GCD of two integers by using recursive function.

P a g e 33 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 17/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 12

AIM: Write a C program to find both the largest and smallest number in a list of integers.
Write algorithm and draw flowchart for the same.

ALGORITHM:

STEP 1: START
STEP 2: Input n array elements from user
STEP 3: Initialize max , min =0
STEP 4: Initialize for loops from i=0 till i<n
STEP 5: If max<a[i] then max=a[i] & if min>a[i] than min=a[i]
STEP 6: STOP

FLOWCHART:

P a g e 34 | 57
CODE:

#include <stdio.h>
#include <conio.h>

void main()
{

int n,i,a[100],l,s;

printf("Enter the number of elements:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}

l=a[0];
for(i=0;i<n;i++)
{
if(l<a[i])
{
P a g e 35 | 57
l=a[i];
}
}

s=a[0];
for(i=0;i<n;i++)
{
if(s>a[i])
{
s=a[i];
}
}

printf("The largest value of the series is: %d\n",l);


printf("The smallest value of the series is: %d\n",s);
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find both
the largest and smallest number in a list of integers

P a g e 36 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 24/01/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 13

AIM: Develop, implement and execute a C program that reads two matrices A (m x n) and B
(p x q) and Compute product of matrices A and B. Read matrix A and matrix B in row major
order and in column major order respectively. Print both the input matrices and resultant
matrix with suitable headings and output should be in matrix format only.
P a g e 37 | 57
CODE:

#include <stdio.h>
#include <conio.h>

void multiply(int a[10][10],int b[10][10],int m,int n,int p,int q)


{

int i,j,k,c[10][10];
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("The multiplication of matrices is:\n");


for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

void main()
{

int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10],s=0;

printf("Enter no of rows for 1st matrix: ");


scanf("%d",&m);

printf("Enter no of columns for 1st matrix: ");


P a g e 38 | 57
scanf("%d",&n);

printf("Enter no of rows for 2nd matrix: ");


scanf("%d",&p);

printf("Enter no of columns for 2nd matrix: ");


scanf("%d",&q);

printf("Enter %d elements for the 1st matrix:\n",m*n);

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Enter %d elements for the 2nd matrix:\n",p*q);

for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("The 1st matrix is:\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The 2nd matrix is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
P a g e 39 | 57
printf("%d\t",b[i][j]);
}
printf("\n");
}
if(n==p)
{
multiply(a,b,m,n,p,q);
}
else
{
printf("Multiplication not possible");
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find the
multiplication of two matrices.
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 31/01/2023

P a g e 40 | 57
Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 14
AIM: Write a Program for deletion of an element from the specified location from Array.

CODE:

#include <stdio.h>
#include <conio.h>

int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
{
scanf("%d", &array[c]);
}
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
{
printf("Deletion not possible.\n");
}
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
}
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
{
printf("%d\n", array[c]);
}
return 0;
}

OUTPUT:

P a g e 41 | 57
CONCLUSION: Thus we have successfully studied and implemented a program to deleta an
element from an array.

P a g e 42 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 02/02/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 15

AIM: Write a C program using user defined functions to determine whether the given string is
palindrome or not.

CODE:

#include <stdio.h>
#include <string.h>

void reverse(char a[100],int n)


{
int i,c=0;
char temp,b[10];
for(i=0;i<n;i++)
{
b[n-i-1]=a[i];
}
printf("THE REVERSE OF THE STRING IS:\n%s\n",b);
for(i=0;i<n;i++)
{
if(a[i]==b[i])
{
c++;
continue;
}
else
{
printf("IT IS NOT A PALINDROME.");
break;
}
}
P a g e 43 | 57
if(c==n)
{
printf("IT IS A PALINDROME.");
}
}

void main()
{
int i,n=0;
char a[100];

printf("Enter a string: ");


gets(a);

while(a[n]!='\0')
{
n++;
}

printf("The LENGTH OF THE STRING IS: %d\n",n);

reverse(a,n);

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find


whether a astring entered is a palindrome or not.

P a g e 44 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 07/02/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 16

AIM: Write C program to count the number of lines, words and characters in a given text.

CODE:

#include <stdio.h>
#include <string.h>

int main()
{
int i,n=0,c=0,l=1,w=1;
char a[100];

printf("Enter a string: ");


gets(a);

while(a[n]!='\0')
{
n++;
}

for(i=0;i<n;i++)
{
if(a[i]!='\0' && a[i]!=' ')
{
P a g e 45 | 57
c++;
}
else if(a[i]=='\n')
{
l++;
}
else if(a[i]==' ')
{
w++;
}

printf("THE LENGTH IS: %d\n",n);


printf("THE NUMBER OF CHARACTERS IS: %d\n",c);
printf("THE NUMBER OF LINES IS: %d\n",l);
printf("THE NUMBER OF WORDS IS: %d\n",w);

return 0;
}
OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to count the
number of lines, words and characters in a given text.

P a g e 46 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 09/02/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 17

AIM: Write a program to swap two numbers using a function. Pass the values to be swapped
to this function using the call-by-value method and call-by-reference method. Write algorithm
and draw flowchart for the same.

PART A ( call by value )

ALGORITHM:

main() function
STEP 1: START
STEP 2: Enter two numbers
STEP 3: Pass the values to swap(n1,n2)

swap() function
STEP 1: Declare temporary variable
STEP 2: t=n1,n1=n2,n2=t
STEP 3: Print values of n1 & n2

P a g e 47 | 57
FLOWCHART:

CODE:
#include<stdio.h>
#include<conio.h>

void swap(int p1,int p2)


{
int temp;
temp=p1;
p1=p2;
p2=temp;

printf("A=%d\n",*p1);
printf("B=%d",*p2);
}
int main()
{
int a,b;

printf("Enter A=");
scanf("%d",&a);
printf("Enter B=");
scanf("%d",&b);
swap(a,b);

P a g e 48 | 57
return 0;
}

PART B ( call by reference method )

ALGORITHM:

main function()
STEP 1: START
STEP 2: Enter n1 & n2
STEP 3: Pass the address’ of &n1 & &n2 to the function swap(*p1,*p2)
STEP 4: Stop

Swap function()
STEP 1: START
STEP 2: temp=*p1,*p1=*p2,*p2=temp
STEP 3: Print n1 & n2
STEP 4: STOP

FLOWCHART:

CODE:
#include<stdio.h>
#include<conio.h>

void swap(int *p1,int *p2)

P a g e 49 | 57
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;

printf("A=%d\n",*p1);
printf("B=%d",*p2);
}
int main()
{
int a,b;

printf("Enter A=");
scanf("%d",&a);
printf("Enter B=");
scanf("%d",&b);
swap(&a,&b);

return 0;
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find


swap two numbers using call by value and call by reference.

P a g e 50 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 16/02/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 18

AIM: Write a C program to find the length of the string using Pointer.

CODE:
#include<stdio.h>
P a g e 51 | 57
#include<conio.h>

int main()
{
char a[20],*p1;
int i=0;

printf("Enter a string\n");
gets(a);

p1=a;

while(*p1!='\0')
{
i++;
p1++;
}
printf("length %d",i);

return 0;
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find the
length of a string using pointers.

P a g e 52 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 21/02/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 19

AIM: Write a program to copy one array to another using pointer.

CODE:

#include <stdio.h>

void printArray(int arr[100], int size);

int main()
{

int arr[100],cpy_arr[100],size,i;

int *src_ptr=arr;
int *cpy_ptr=cpy_arr;

int *end_ptr;

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements in array: ");


for (i=0; i< size; i++)
{
scanf("%d",(src_ptr+i));
}

end_ptr=&arr[size-1];
printf("\nSource array before copying: ");
printArray(arr, size);

P a g e 53 | 57
printf("\nDestination array before copying: ");
printArray(cpy_arr, size);

while(src_ptr<=end_ptr)
{
*cpy_ptr=*src_ptr;
src_ptr++;
cpy_ptr++;
}

printf("\n\nSource array after copying: ");


printArray(arr, size);

printf("\nDestination array after copying: ");


printArray(cpy_arr, size);

return 0;
}

void printArray(int *arr, int size)


{
int i;
for (i=0; i<size; i++)
{
printf("%d.",(arr-i));
}
}

P a g e 54 | 57
OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to find copy
an array using pointers.

P a g e 55 | 57
Academic Year: 2022-23 SAP ID: 60002220163

Class & Div: E3 - 2 Name: Siddhant Sanghoi

Course Code: DJS22FEC12 Date: 23/02/2023

Course Name: Structured Programming using C Laboratory

EXPERIMENT NO. 20

AIM: Write a program to compare two strings using pointers.

CODE:

#include <stdio.h>

int str cnmp(char*a,char*b);

int main()
{
char str1[20];

Char str2[20]

int cmp;

clrscr):

printfEnter the first string

scanf("%s",str1)

printf(nEnter the second string: D

scanf("%s",str2);

cmp str cmp(strl,str2)

if(cmp 0)

printf("strings are equal");


else
P a g e 56 | 57
printf strings are not equal

getch):

return 0;
}

int str cmp(char*a,char *b)


{
int count=0,

while(*a!=’\0’ && *b!=’\0')


{
If(*a!=*b)
{
count=1;
}
a++;
b++;
}

if(count==0)

return 0;

else

return 1;
}

OUTPUT:

CONCLUSION: Thus we have successfully studied and implemented a program to compare


two stinrgs using pointers.

P a g e 57 | 57

You might also like