You are on page 1of 8

Set No: 2

Program 1

Source Code

#include <stdio.h>

#include <stdlib.h>

//Creating a structure node

typedef struct LinkedList{

int data;

struct LinkedList *next;

}node;

//Declaring the functions

node* getnode();

void displayList(node *head) ;

//main method

int main()

node *head, *q,*x;

int n, i=0;

//Taking the value of n from the user

printf("Enter the value of n\n");

do{

scanf("%d",&n);

//Checking if the value of n is valid or not

if(n<=0)

printf("Invalid Entry. Enter again.\n");

Page 1
Set No: 2

}while(n<=0);

//Creating the linked list

while(i<n)

//Condition for creating the starting node of the linked list

if(i==0)

head = getnode();

q=head;

i=i+1;

//Condition for adding susequent nodes

else

x=getnode();

q->next=x;

q=q->next;

i=i+1;

//Displaying the linked list

displayList(head);

return 0;

//Creating a node dynamically

Page 2
Set No: 2

node* getnode()

int item;

node* new =(node*) malloc(sizeof(node));

printf("\nEnter the data\n");

scanf("%d",&item);

new->data = item;

new->next = NULL;

return new;

//Printing the elements of the linked list

void displayList(node *head)

node *temp;

if (head == NULL)

printf("List is empty.\n");

printf("Elements of list are : ");

temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

Page 3
Set No: 2

Output

Page 4
Set No: 2

Program 2

Source Code

#include <stdio.h>
#include <stdlib.h>

//main method
int main()
{
float *arr;
int pos, i, n;
//Taking the value of n from the user
printf("Enter number of elements in array\n");
scanf("%d", &n);
//Creating the array dynamically
arr = calloc(n, sizeof(float));
//Entering the elements into the array
printf("Enter the %d elements\n", n);
for (i = 0; i < n; i++)
scanf("%f", &arr[i]);
// Entering the deleting location
printf("Enter the location where you wish to delete element\n");
scanf("%d", &pos);
//Deletion operation
if (pos <=0||pos >= n+1 )
printf("Deletion not possible.\n");
else
{
for (i = pos-1; i<n-1; i++)
arr[i] = arr[i+1];
}
//Displaying the resultant array after deletion
printf("Resultant array:\n");
for (i = 0; i < n - 1; i++)
printf("%f ", arr[i]);

return 0;
}

Page 5
Set No: 2

Output

Page 6
Set No: 2

Program 3

Source Code

>>> def fib(n):


if n <= 1:
return n
else:
return(fib(n-1) + fib(n-2))
>>> # Taking the number of terms from the user
>>> n = int(input("How many terms? "))
How many terms? 15
>>> # Checking if the number of terms is valid or not
>>> if n <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:") # Printing the Fibonacci sequence
for i in range(n):
print(fib(i))

Output

Page 7
Set No: 2

Program 4

Source Code

>>> # Program to transpose a matrix using a nested loop

>>> X = [[12,7], [4 ,5], [3 ,8]]

>>> result = [[0,0,0], [0,0,0]]

>>> # Iterating through rows

>>> for i in range(len(X)):

# iterate through columns


for j in range(len(X[0])):
result[j][i] = X[i][j]

>>> for r in result:


print(r)

Output

Page 8

You might also like