Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1.

TOWERS OF HANOI USING RECURSION

Aim
To write a C Program to solve towers of Hanoi using recursion.

Algorithm

Step1: Start

Step2: Declare variables

Step3: Read the Input for number of discs.

Sep4: Check the condition for each transfer of discs using recursion.

Step5: Display the output of the each move.

Step6: Stop

#include <stdio.h>

#include <conio.h>
void towerofhanoi(int n, char from, char to, char aux)
{

if (n == 1)
{
printf("\n Move disk 1 from pole %c to pole %c", from, to);

return;
}

towerofhanoi(n-1, from, aux, to);


printf("\n Move disk %d from pole %c to pole %c", n, from, to);

towerofhanoi(n-1, aux, to, from);

int main()
{
int n;
printf("Enter the number of disks : ");
scanf("%d",&n);
towerofhanoi(n, 'A', 'C', 'B');
getch();

}
Output

Enter the number of disks: 3


Move disk 1 from pole A to pole C

Move disk 2 from pole A to pole B

Move disk 1 from pole C to pole B

Move disk 3 from pole A to pole C

Move disk 1 from pole B to pole A

Move disk 2 from pole B to pole C

Move disk 1 from pole A to pole C


2. SORTING USING PASS BY REFERENCE
Aim

To write a C Program to Sort the list of numbers using pass by reference.

Algorithm

Step 1:Start

Step 2:Declare variables and create an array

Step 3:Read the Input for number of elements and each element.
Step 4:Develop a function to sort the array by passing reference
Step 5:Compare the elements in each pass till all the elements are sorted.
Step 6:Display the output of the sorted elements .
Step 7:Stop
Program

#include <stdio.h>
#include <conio.h>
void main()
{

int n,a[100],i;
void sortarray(int*,int);
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");

for(i=0;i<n;i++) printf("%d\n",a[i]);

getch();
}

void sortarray(int* arr,int num)


{
int i,j,temp;

for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i];

arr[i] = arr[j];

arr[j] = temp;
}

}
Output

Enter the Number of Elements in an array: 5


Enter the Array elements
33
67
21
45
11
After Sorting....

11
21
33
45
67
3. INTERNAL MARKS OF STUDENTS
Aim

To write a C Program to Compute internal marks of students for five different subjects
using structures and functions.
Algorithm

Step 1:Start

Step 2:Declare variables

Step 3:Read the number of students .


Step 4:Read the student mark details
Step 5:Calculate internal mark by i=total of three test marks / 3 for each subject per student.
Step 6:Display the output of the calculations for all the students .
Step 7:Stop

Program

#include<stdio.h>
#include<conio.h>
struct stud
{
char name[20];

long int rollno;


int marks[5,3];
int i[5];

}students[10];

void calcinternal(int);
int main()
{
int a,b,j,n;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=0;a<n;++a)
{
printf("\n\nEnter the details of %d student : ", a+1);

printf("\n\nEnter student %d Name : ", a);

scanf("%s", students[a].name);

printf("\n\nEnter student %d Roll Number : ", a);


scanf("%ld", &students[a].rollno);
total=0;
for(b=0;b<=4;++b)
{
for(j=0;j<=2;++j)
{
printf("\n\nEnter the test %d mark of subject-%d : ",j+1, b+1);
scanf("%d", &students[a].marks[b,j]);
}
}
}
calcinternal(n);

for(a=0;a<n;++a)

printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);

printf("\t\t\t\t Roll No : %ld", students[a].rollno);


printf("\n------------------------------------------------------------------------");
for(b=0;b<5;b++)
{

printf("\n\n\t Subject %d internal \t\t :\t %d", b+1, students[a].i[b]);


}
printf("\n\n------------------------------------------------------------------------\n");
getch();
}

return(0);
}

void calcinternal(int n)

{
int a,b,j,total;

for(a=1;a<=n;++a)

for(b=0;b<5;b++)

total=0;
for(j=0;j<=2;++j)
{
total += students[a].marks[b,j];
}
students[a].i[b]=total/3;
}
}
}

Output

How many students : 1

Enter the details of 1 student :


Enter student 1 Name : siva
Enter student 1 Roll Number : 10466

Enter the test 1mark of subject-1 : 46


Enter the test 2 mark of subject-1 : 56
Enter the test 3 mark of subject-1 : 76

Enter the test 1 mark of subject-2 : 85


Enter the test 2mark of subject-2 : 75
Enter the test 3mark of subject-2 : 75

Enter the test 1mark of subject-3 : 66


Enter the test 2 mark of subject-3 : 86
Enter the test 3 mark of subject-3 : 70

Enter the test 1 mark of subject-4 : 25


Enter the test 2mark of subject-4 : 35
Enter the test 3mark of subject-4 : 61
Enter the test 1 mark of subject-5 : 45

Enter the test 2mark of subject-5 : 75

Enter the test 3mark of subject-5 : 60

Mark Sheet
Name of Student : siva Roll No : 10466
------------------------------------------------------------------------
subject 1 internal : 59
subject 2 internal : 78
subject 3 internal : 74
subject 4 internal : 40
subject 5 internal : 60
------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy