0% found this document useful (0 votes)
3 views

PSC Lab Manual

Uploaded by

srivatsa8197
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PSC Lab Manual

Uploaded by

srivatsa8197
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Exp no: 1

Date: ​Functions

1. a)Write a C program using a pointer to demonstrate call by reference 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:
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.

● Simple and Robust: It is a straightforward implementation of the Euclidean


algorithm, and it will correctly handle most cases, including negative inputs and zeros
(though the edge case of both inputs being zero is not explicitly handled).

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.

Requirements : Visual Studio or GCC Compiler.

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.

Result: Thus the implementation of String Tokenize was executed successfully.

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.

Requirements : Visual Studio or GCC Compiler.

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.

2. Input the string from the user into s1.

3.To accept the input from the user along with the space we are taking the character set up to
newline.

4.Initialize s2 as an empty character array.

5.Iterate through each character in s1.

6.Copy the current character of s1 to s2.

7. Move to the next character in both s1 and s2.

8.Terminate s2 with a null character (\0).

9.Print s2 to display the copied string.

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:

After Copying: AllianceUniversity

Sample Input:

Hello World

Sample Output:

After Copying: Hello World

Result: Thus the implementation of copying one string to another string without using strcpy
was executed successfully.

____________________________________________________________________________

Exp-4
Date:

b)Aim:Create C program to Count Vowels and Consonants using string function.

Algorithm:

1.Start.
2. Start Input the string from the user.

3.Input the string from the user.

4. Initialize count of the vowel= 0 and count of the consonant =0.

5.They didn’t mention anything about the characters so we can consider both uppercase and
lowercase letters.

6.Iterate through each character in the string .

7.Check the current character


If the character is a vowel(a,e,i,o,u), increment the count of the vowel .
If the character is not a vowel, come to else block and increment the count of the
consonant.

8.Print the count of the vowels and consonants.

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.

Input: We are using scanf to read input from the user.

Calculation:

→Here we are using a for loop to travel the entire string.


→To check whether it is character or not we are using isalpha() function
→If we find it is an alphabet then we are checking whether the current character is a vowel or
not.

Output:

The program displays the count of vowels and consonants.

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:

a)Aim: Create a c program to perform functions with arrays as parameters.

Algorithm:

1.Start

2.Take the size of the array as an input from the user.

3.To store elements based on the size we have to create an array.

4.Iterate through each element of an array.

5. Pass size,array as a parameter to function.

6.Create a function to perform the task.

7.Initialize a variable to store the sum of the array elements sum=0.

8.Iterate each element from the entire array and add to the sum.

9.Print the sum value.

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:

Initialization: Initialize size and array to store the multiple values.

Input: Use scanf to read input from the user.

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

Output: The program displays the sum of the array elements.

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:

b)Aim:Write a C program to demonstrate Variadic function to find the largest number.

Algorithm:

1.Start

2.Declare a value to store the fixed size and declare the values.

3.Read input from the user by using scanf.

4.Declare a function and pass size and elements as a parameters.

5.To accept the number of parameters use an arbitrary function.

6.Initialize a max=0 .

7.Initialize variable argument list using va_start.

8.Iterate each element by using for loop


→when you are iterating each element by using a for loop compare it with max.
→If the element is greater than the max update the max.

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.

Input: We have to read input from the user by using scanf.

Calculation:

→The Program uses a for loop to traverse the entire list.


→Each element we are going to compare with the max variable from the list.
→If we reached to last element then we have to stop iterating

Output: The program displays the max of the list elements.

Result: Thus the implementation of max of the list elements was executed successfully.
Exp:6
Date:

a)Aim:Write a C program to demonstrate callback function by passing a function pointer


as an argument to another function

Algorithm:

1.Start.

2.Create one function sum to add two numbers.

3. Define function display(int (*ptr)(int, int)) to pass the address of a function as


a parameter.

4.Print the result.

5.End.

Program:

#include <stdio.h>
int sum(int a,int b)
{
return a+b;
}
void display(int (*ptr)(int,int))
{
ptr=&sum;
printf("%d",ptr(10,20));
}
int main()
{
display(sum);
}

Inferences:

Calculation:We are passing the address of a function to another function.

Output: The program displays the sum of 2 numbers.

Result:Thus the implementation of sum of two numbers by using callback was executed
successfully.

Output:
30
Exp:6
Date:

b)Aim:Write C program to demonstrates higher-order functions by using function


pointers to perform operations like addition and multiplication on elements of an
array

Algorithm:

1.Start.

2.Take the size of the array as an input from the user.

3.To store elements based on the size we have to create an array.

4.Iterate through each element of an array.

5.Define function add(int x, int y)

●​ Compute x + y
●​ Return the sum

6.Define function mul(int x, int y)

●​ Compute x * y
●​ Return the product

7.Define function display(int(*ptr)(int, int), int n, int a[], int(*ptr1)(int, int))

●​ Assign ptr = add and ptr1 = mul


●​ Initialize res = a[0] for addition and res1 = a[0] for multiplication.
●​ Loop from i = 0 to n:

→Each element we have to pass to function by passing an element we have to add and
multiply the values.

●​ Print res and res1

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:

a)Aim:Create a program that demonstrates direct recursion by calculating the nth


Fibonacci number using a recursive function

Algorithm:

1.Start.

2.Read Input from the user.

3.Create one function and pass the input to that function.

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:

Input: Read n value from the user by using scanf.

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:

b)Aim:Write a program to calculate the factorial of a number using tail recursion


to demonstrate its efficiency

Algorithm:

1.Start.

2.Read the input from the user.

3.Create one function to find the factorial of a number.

4.The value of 1!=1 and 0!=1 so initialize the value.

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:

Input: Read n value from the user by using sacnf.

Calculation: Until n becomes equal to one repeat the process if n is equal to 1 stop
iterating.

Output: The program displays the factorial of a number by using recursion.

Result:Thus the implementation of factorial of a number by using recursion was


executed successfully.

Sample Input:

Sample Output:

120

Sample Input:

Sample Output:

You might also like