PSC Lab Manual
PSC Lab Manual
Date: Functions
Aim:
To demonstrate call by reference by passing two integers to a function and swapping
the values.
Algorithm:
1. Start.
2. Input a, b.
3.Call the swap function with &a and &b.
4.Inside swap function:
a. Print values of *a and *b.
b. Temporary variable t used to swap values.
c. Print the new values of *a and *b.
d. Print the final values of *a and *b after swapping.
5.End.
Program:
#include <stdio.h>
void swap(int *a, int *b)
{
printf("Before swapping %d %d\n",a,b); int
t= *a;
*a=*b;
*b=t;
printf("After swapping %d %d\n",*a,*b);
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
swap(&a,&b);
printf(a,b);
}
Inferences:
● Pointer Basics: The program provides a simple example of how to use pointers
to directly manipulate variables in memory.
● Swapping Mechanism: It illustrates a common approach to swapping values
using a temporary variable to store one value while the other is moved.
● By Reference vs By Value: The use of pointers allows the function to alter the
values in the calling function (main), demonstrating the concept of passing by
reference in C.
● Correctness in Handling Data: Using a temporary variable ensures that no data
is lost during the swapping process.
Result:
Thus the implementation of call by reference was executed successfully.
Input:
10 20
Output:
Before Swapping 10 20
After Swapping 20 10
20 10
--------------------------------------------------------------
b) Write a C program to demonstrate value by passing two integers to a function and
swapping the values
Aim:
To demonstrate call by reference by passing two integers to a function and swapping
the values.
Algorithm:
● Start.
● Input a, b.
● Print the original values of a and b.
● Call the swap function with a and b.
● Inside swap function:
○ Print values of a and b.
○ Temporary variable t used to swap values.
○ Print the new values of a and b.
○ Print the final values of a and b after swapping.
● End.
Program:
#include <stdio.h>
void swap(int a, int b)
{
printf("Before swapping %d %d\n",a,b); int
t= a;
a=b;
b=t;
printf("After swapping %d %d\n",a,b);
}
int main()
{
int a,b;
scanf("%d %d",&a,&b); printf("Original
values %d %d\n",a,b); swap(a,b);
printf("Swapped values %d %d\n",a,b);
}
Inferences:
Copying Arguments:
● In this case, when the swap function is called, the actual values of a and b are
copied into the local variables a and b inside the swap function.
● These are local copies of the values, and the changes made to them inside
the function do not affect the original a and b in the main function.
Changes are Local:
● When the swap function modifies its local copies of a and b, the changes (like
the swap operation) only affect the local scope of the swap function.
● No effect on the original variables in main (because we are working on copies,
not references).
Output Behavior:
● The program will show the original values of a and b unchanged in the main
function after the swap function call.
● The swapped values will be printed within the swap function, but since they
only affect the local copies, they won’t persist outside of the function.
Result:
Thus the implementation of call by value was executed successfully.
Input:
56
Output:
Original values 5
6
Before Swapping 56
After Swapping 65
Swapped values 56
Exp no: 2
Date: Functions
a) Create C program using function to calculates the LCM of three numbers Aim:
To demonstrate LCM (Lowest Common Factor) of three numbers using a function.
Algorithm:
Step 1: Define a function LCM that calculates the LCM of three integers. Take one
temporary variable to store max product of all the three values because the lcm of three
numbers are less then or equal to max product value.
Step 2: Input three integers from the user in the main function.
Step 3: Call a function to find lcm of three numbers.
Step 4: Run a for loop for iterating starting to max product and check whether it is
completely divisible by all the three numbers if it is divisible then return that value.
Step 5: Display the LCM of the three integers.
Program:
#include <stdio.h>
void lcm(int a,int b,int c)
{
int i;
int max=a*b*c;
for(i=1;i<=max;i++)
{
if( i%a==0 && i%b==0 && i%c==0)
{
break;
}
}
printf("%d",i);
}
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
lcm(a,b,c);
return 0;
}
Inferences:
Initialization:
The program initializes the integers a, b, and c with user input and calculates the LCM
sequentially.
Calculation:
The program first finds out max product and based on the product iterating a loop up to
max value and finding LCM of three numbers.
Output:
The program displays the LCM of the three integers
Result:
Thus the implementation of LCM of three numbers was executed successfully.
Input:
248
Output:
2b)Create C program using function to find Greatest Common Divisor (GCD) Using
Euclid’s Algorithm
Aim:
To demonstrate Greatest common divisor (GCD) using Eculid’s Algorithm.
Algorithm:
1. Start.
2. Input: Read the integers a and b from the user.
3. Check if a or b is negative:
4. If a < 0, set a = -a.
5. If b < 0, set b = -b.
6. Loop (Euclidean Algorithm):
7. While b != 0:
a. Compute r = a % b (remainder when a is divided by b).
b. Set a = b and b = r (update a and b).
8. Output: Once b becomes 0, print the value of a (GCD).
9. End.
Program:
#include<stdio.h> int
GCD(int a,int b)
{
a=(a<0)?-a:a;
b=(b<0)?-b:b;
while(b!=0)
{
int r=a%b;
a=b;
b=r;
}
return a;
}
int main()
{
int a,b;
scanf("%d%d",&a,&b); int
res=GCD(a,b);
printf("%d",res);
}
Inferences:
● Efficiency: The program uses the Euclidean algorithm, which is an efficient
way to compute the GCD with a time complexity of O(log(min(a, b))).
● Negative Handling: The program ensures that negative inputs are
converted to positive values before proceeding with the GCD calculation.
Result:
Thus the implementation of Greatest Common Divisor Using Euclid’s Algorithm was
executed successfully.
Input:
84
Output:
4
Exp no: 3
Date: Functions
a)Aim: Create C program tokenizes a string based on a specified delimiter using function.
Algorithm :
1. Start
2. Input the string from the user.
3. Input the delimiter from the user.
4. Remove the newline character (if any) from both the string and the delimiter.
5. Initialize the tokenization process:
6. Use strtok() to extract the first token from the string.
7. Iterate through the string:
8. While there are more tokens:
9. Print the current token.
10. Get the next token using strtok().
11. End
Program:
#include <stdio.h>
#include <string.h>
void deli(char s[15],char d[10])
{
char *token=strtok(s,d);
while(token!=NULL)
{
printf("%s\n",token);
token=strtok(NULL,d);
}
}
int main()
{
char s[15];
scanf("%s",s);
char d[10];
scanf("%s",d);
deli(s,d);
return 0;
}
Inferences:
Initialization: Variables are initialized for the input string (str) and the delimiter (delimiter).
Another variable (token) is used to store each extracted part of the string during tokenization.
Input: The user provides an input string and specifies a delimiter. These inputs determine how
the string will be split into tokens.
Calculation: The strtok function is used to process the string. It iteratively splits the string into
smaller parts (tokens) based on the specified delimiter, updating the token variable with each
part.
Output: Each token extracted from the string is displayed to the user, clearly showing the
results of the tokenization process.
Sample Input:
Alli+ance+Univ+ersity
+
Sample Output:
Alli
ance
Univ
ersity
Sample Input:
Alliance-University
-
Sample Output:
Alliance
University
____________________________________________________________________________
EXP-3
Date:
b)Aim: Create C program removes duplicate characters from a function.
Algorithm:
1. Start
2. Input the string from the user.
3. Iterate through each character in the string:
4. For each character, initialize a flag = 1 (assume it is unique).
5. Compare the character with all other characters in the string:
6. If the same character is found at a different position, set flag = 0 and break the loop. Check
the if flag == 1, print the character (it is unique).
7. End
Program:
#include <string.h>
#include <stdio.h>
void removeduplicate(char s[20])
{
int n=strlen(s);
for(int i=0;i<n;i++)
{
int flag=1;
for(int j=0;j<i;j++)
{
if(s[i]==s[j])
{
flag=0;
break;
}
}
if(flag==1)
{
printf("%c",s[i]);
}
}
}
int main()
{
char s[20];
scanf("%s",s);
removeduplicate(s);
return 0;
}
Inferences:
Initialization: The input string and variables used for tracking unique characters are initialized
to process the input efficiently.
Input: The program takes a string as input, ensuring that it handles spaces and special
characters appropriately.
Calculation: Duplicate characters are identified and skipped, ensuring that only unique
characters remain in the resulting string.
Output: The resulting string, free of duplicate characters, is displayed to the user in a clear
format.
Sample Input:
Alliance
Sample Output:
Aliance
Sample Input:
ProblemSolvingUsingC
Sample Output:
PrbemSvUsC
Result: Thus the implementation of removing duplicated elements was executed successfully.
Exp-4
Date:
a) Aim: Create a C program to copy the string without strcpy function by using
pointers with the help of function.
Algorithm:
1.Start.
3.To accept the input from the user along with the space we are taking the character set up to
newline.
10. End
Program:
#include <stdio.h>
void stringcopy(char *s1,char *s2)
{
while(*s1!='\0')
{
*s2=*s1;
s1++;
s2++;
}
*s2='\0';
}
int main()
{
char s1[20];
scanf("%[^\n]",s1);
char s2[20];
stringcopy(s1,s2);
printf("After Copying: %s",s2);
}
Inferences:
Initialization: The source and destination character arrays are initialized to hold the strings.
Input: The user inputs the source string using scanf, which can include spaces.
Calculation: The stringCopy function uses pointer manipulation to copy each character from the
source string to the destination string.
Output: The copied string is printed, which is the same as the source string.
Sample Input:
AllianceUniversity
Sample Output:
Sample Input:
Hello World
Sample Output:
Result: Thus the implementation of copying one string to another string without using strcpy
was executed successfully.
____________________________________________________________________________
Exp-4
Date:
Algorithm:
1.Start.
2. Start Input the string from the user.
5.They didn’t mention anything about the characters so we can consider both uppercase and
lowercase letters.
9.End.
Program:
#include <string.h>
#include <ctype.h>
#include <stdio.h>
void countvowelsconsonants(char s[20])
{
int n=strlen(s);
int vowels=0,consonants=0;
for(int i=0;i<n;i++)
{
if(isalpha(s[i]))
{
if(s[i]=='a'||s[i]=='e'||s[i]=='o'||s[i]=='i'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='O'||s[i]=='I'||s[i]=='U')
{
vowels++;
}
else
{
consonanst++;
}
}
}
printf("Vowels: %d\nConsonants: %d",vowels,consonants);
}
int main()
{
char s[20];
scanf("%s",s);
count vowelsconsonants(s);
return 0;
}
Inferences:
Initialization:
char s[] is initialized to store input string from the user.
To count the vowels and consonants we initialize vowels=0 and consonants=0.
Calculation:
Output:
Result:
Thus the implementation of count vowels and consonants was executed successfully.
Sample Input:
AllianceUniversity
Sample Output:
Vowels: 8
Consonants: 8
Sample Input:
HelloWorld1
Sample Output:
Vowels: 3
Consonants: 7
Exp:5
Date:
Algorithm:
1.Start
8.Iterate each element from the entire array and add to the sum.
10.End.
Program:
#include<stdio.h>
void sumofarrayelements(int arr[], int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
printf("%d",sum);
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sumofarrayelements(a,n);
}
Inferences:
Calculaction:
→The Program uses a for loop to traverse the entire array.
→Each element we are adding to the sum variable from the array.
→If we reached to last element then we have to stop iterating
Result: Thus the implementation of sum of the array elements was executed successfully.
Sample Input:
5
12345
Sample Output:
15
Sample Input:
5
13679
Sample Output:
26
____________________________________________________________________________
Exp:5
Date:
Algorithm:
1.Start
2.Declare a value to store the fixed size and declare the values.
6.Initialize a max=0 .
9.Return max.
10.End.
Program:
#include <stdarg.h>
#include <stdio.h>
int maxele(int n,...)
{
int f,max=0;
va_list arr;
va_start(arr,n);
for(int i=0;i<n;i++)
{
f=va_arg(arr,int);
if(max<f)
{
max=f;
}
}
return max;
va_end(arr);
}
int main()
{
int n;
scanf("%d",&n);
int a,b,c,d,e;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("%d",maxele(n,a,b,c,d,e));
}
Inferences:
Initialization:Initialize a max=0.
Calculation:
Result: Thus the implementation of max of the list elements was executed successfully.
Exp:6
Date:
Algorithm:
1.Start.
5.End.
Program:
#include <stdio.h>
int sum(int a,int b)
{
return a+b;
}
void display(int (*ptr)(int,int))
{
ptr=∑
printf("%d",ptr(10,20));
}
int main()
{
display(sum);
}
Inferences:
Result:Thus the implementation of sum of two numbers by using callback was executed
successfully.
Output:
30
Exp:6
Date:
Algorithm:
1.Start.
● Compute x + y
● Return the sum
● Compute x * y
● Return the product
→Each element we have to pass to function by passing an element we have to add and
multiply the values.
8.End
Program:
#include <stdio.h>
int add(int x,int y)
{
return x+y;
}
int mul(int x,int y)
{
return x*y;
}
void display(int(*ptr)(int,int),int n,int a[],int (*ptr1)(int,int))
{
ptr=add;
ptr1=mul;
int res=a[0],res1=a[0];
for(int i=1;i<n;i++)
{
res=add(res,a[i]);
res1=mul(res1,a[i]);
}
printf("%d\n%d",res,res1);
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
display(add,n,a,mul);
return 0;
}
Inferences:
Initialization:Initial the res=0 and res1=0 for adding and multiplying the values.
Input: Read the size of the array and elements from the user.
Calculation:
→The Program uses a for loop to traverse the entire array.
→Each element we are adding to the res and res1 variable from the array.
→If we reached to last element then we have to stop iterating
Output: The program displays the sum and multiplication of the array elements.
Result: Thus the implementation of sum and multiplication of the array elements was
executed successfully.
Sample Input:
5
12345
Sample Output:
15
120
Sample Input:
3
136
Sample Output:
10
18
__________________________________________________________________________
Exp:7
Date:
Algorithm:
1.Start.
4.Check the condition if n==0 return 0 ,if n==1 return 1 or add previous two values
fib(n-1)+fib(n-2).
5.If n==0 stop the process and return the value to where you are calling the function.
6.End.
Program:
#include <stdio.h>
int fib(int n)
{
if(n==0 )
{
return 0;
}
else if(n==1)
{
return 1;
}
else
return fib(n-1)+fib(n-2);
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",fib(n));
return 0;
}
Inferences:
Calculation:
→Check the condition in a function until the base condition satisfies the recursive
condition will work.
→If the base condition satisfies it stops the program and returns the value as output.
Output: The program displays the fibonacci value of the nth term.
Result:Thus the implementation of the nth term of fibonacci number was implemented
successfully.
Sample Input:
Sample Output:
5
Sample Input:
Sample Output:
21
______________________________________________________________________
Exp:7
Date:
Algorithm:
1.Start.
5.If the condition is not satisfied it will come to a recursive statement up to n become
equal to 0 or 1.
6.End.
Program:
#include <stdio.h>
int fact(int n)
{
if(n==0 || n==1 )
{
return 1;
}
else
return n*fact(n-1);
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",fact(n));
return 0;
}
Inferences:
Calculation: Until n becomes equal to one repeat the process if n is equal to 1 stop
iterating.
Sample Input:
Sample Output:
120
Sample Input:
Sample Output: