Exp 1-20
Exp 1-20
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:
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
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:
START
Declare x & y
x=x+y
y=x-y
x=x-y
Print x & y
END
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;
#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;
OUTPUT:
P a g e 4 | 57
Academic Year: 2022-23 SAP ID: 60002220163
EXPERIMENT NO. 2
CODE:
#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:
P a g e 6 | 57
Academic Year: 2022-23 SAP ID: 60002220163
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
P a g e 7 | 57
IT IS NOT LEAP YEAR YES NO
Year / 4
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:
P a g e 9 | 57
Academic Year: 2022-23 SAP ID: 60002220163
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;
}
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
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");
y=((-b+sqrt((pow(b,2)-4*a*c)))/2*a);
z=((-b-sqrt((pow(b,2)-4*a*c)))/2*a);
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
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;
}
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.
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
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
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
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);
while(x!=0)
{
y=x%10;
r=(r*10) + y;
x=x/10;
s=s+y;
}
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
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
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
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
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
Compare using
functions
CODE:
#include <stdio.h>
int main()
{
P a g e 30 | 57
int x,y;
clrscr();
printf("ENTER TWO NUMBER: \n");
scanf("%d%d",&x,&y);
OUTPUT:
P a g e 31 | 57
Academic Year: 2022-23 SAP ID: 60002220163
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 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:
P a g e 33 | 57
Academic Year: 2022-23 SAP ID: 60002220163
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;
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];
}
}
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
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>
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];
}
}
}
void main()
{
int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10],s=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
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
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
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 main()
{
int i,n=0;
char a[100];
while(a[n]!='\0')
{
n++;
}
reverse(a,n);
OUTPUT:
P a g e 44 | 57
Academic Year: 2022-23 SAP ID: 60002220163
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];
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++;
}
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
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.
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>
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;
}
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>
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:
P a g e 50 | 57
Academic Year: 2022-23 SAP ID: 60002220163
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
EXPERIMENT NO. 19
CODE:
#include <stdio.h>
int main()
{
int arr[100],cpy_arr[100],size,i;
int *src_ptr=arr;
int *cpy_ptr=cpy_arr;
int *end_ptr;
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++;
}
return 0;
}
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
EXPERIMENT NO. 20
CODE:
#include <stdio.h>
int main()
{
char str1[20];
Char str2[20]
int cmp;
clrscr):
scanf("%s",str1)
scanf("%s",str2);
if(cmp 0)
getch):
return 0;
}
if(count==0)
return 0;
else
return 1;
}
OUTPUT:
P a g e 57 | 57