extra_programs
extra_programs
int i, n;
return 0;
}
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
return 0;
}
3./* Write a C program to Check Armstrong Number of three digits*/
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
#include <stdio.h>
// function prototype
long long convert(long long);
int main() {
long long n;
return 0;
}
// function definition
long long convert(long long n) {
long long dec = 0;
int i = 0, rem;
while (n != 0) {
// divide n by 10
n /= 10;
// increment i
++i;
}
return dec;
}
#include <stdio.h>
int main() {
int numbers[5]; // Declares an integer array of size 5
return 0;
}
6./* Declare a calendar as an array of 7 elements (A dynamically Created array) to
represent 7 days of a week. Each Element of the array is a structure having three
fields. The first field is the name of the Day (A dynamically allocated String), The
second field is the date of the Day (A integer), the third field is the description of the
activity for a particular day (A dynamically allocated String).b) Write functions
create( ), read( ) and display( ); to create the calendar, to read the data from the
keyboard and to print weeks activity details report on screen*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *name;
int date;
char *description;
} Day;
// Function to create a calendar
Day *create()
{
Day *calendar = (Day *)malloc(sizeof(Day) * 7);
if (calendar = = NULL)
{
printf("Error allocating memory for calendar\n");
exit(1);
}
return calendar;
}
// Function to read data from the keyboard
void read(Day *calendar)
{
for (int i = 0; i < 7; i++)
{
printf("Enter the name of day %d: ", i + 1);
char *name = (char *)malloc(sizeof(char) * 100);
if (name == NULL)
{
printf("Error allocating memory for day name\n");
exit(1);
}
scanf("%s", name);
calendar[i].name = name;
printf("Enter the date of day %d: ", i + 1);
int date;
scanf("%d", &date);
calendar[i].date = date;
printf("Enter the description of activity for day %d: ", i + 1);
char *description = (char *)malloc(sizeof(char) * 100);
if (description == NULL) {
printf("Error allocating memory for activity description\n");
exit(1);
}
scanf("%s", description);
calendar[i].description = description;
}
}
// Function to print weeks activity details report on screen
void display(Day *calendar)
{
printf("\nWeek Activity Details Report\n");
printf("----------------------------\n");
for (int i = 0; i < 7; i++)
{
printf("%s %d: %s\n", calendar[i].name, calendar[i].date, calendar[i].description);
}
}
int main( )
{
// Create a calendar
Day *calendar = create();
// Read data from the keyboard
read(calendar);
// Display weeks activity details report on screen
display(calendar);
// Free the memory allocated for the calendar
for (int i = 0; i < 7; i++)
{
free(calendar[i].name);
free(calendar[i].description);
}
free(calendar);
return 0;
}
OUTPUT:
Enter the name of day 1: Monday
Enter the date of day 1: 10
Enter the description of activity for day 1: Work from home
Enter the name of day 2: Tuesday
Enter the date of day 2: 11
Enter the description of activity for day 2: Meet with client
Enter the name of day 3: Wednesday
Enter the date of day 3: 12
Enter the description of activity for day 3: Go to gym
Enter the name of day 4: Thursday
Enter the date of day 4: 13
Enter the description of activity for day 4: Work on project
Enter the name of day 5: Friday
Enter the date of day 5: 14
#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
while(str[c] !='\0')
{
if(str[m] == pat[i])
{
i++;
m++;
if(pat[i] == '\0')
{
flag = 1;
for(k=0; rep[k]!='\0'; k++, j++)
{
ans[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
ans[j]= str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
}
void main()
{
printf("\nEnter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
stringmatch();
if(flag == 1)
printf("\nResultant string is %s", ans);
else
printf("\nPattern string is not found");
}
OUTPUT 1:
Enter the main string:Mangalore
Enter the pat string:alo
Enter the replace string:alu
Resultant string is Mangalure
OUTPUT 2:
Enter the main string:Mangalore
Enter the pat string:test
Enter the replace string:best
Pattern string is not found
8./* Develop a menu driven Program in C for the following operations on Singly Linked
List (SLL) of
Student Data with the fields: USN, Name, Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[25],name[25],branch[25];
int sem;
long int phone;
struct node *link;
};
typedef struct node * NODE;
NODE start = NULL;
int count=0;
NODE create()
{
NODE snode;
snode = (NODE)malloc(sizeof(struct node));
if(snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch, &snode->sem,
&snode-
>phone);
snode->link=NULL;
count++;
return snode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if(start == NULL)
{
return temp;
}
temp->link = start;
return temp;
}
NODE deletefront()
{
NODE temp;
if(start == NULL)
{
printf("\nLinked list is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe Student node with usn:%s is deleted ",start->usn);
count--;
free(start);
return NULL;
}
temp = start;
start = start->link;
printf("\nThe Student node with usn:%s is deleted",temp->usn);
count--;
free(temp);
return start;
}
NODE insertend()
{
NODE cur,temp;
temp = create();
if(start == NULL)
{
return temp;
}
cur = start;
while(cur->link !=NULL)
{
cur = cur->link;
}
cur->link = temp;
return start;
}
void display()
{
NODE cur;
int num=1;
if(start == NULL)
{
printf("\nNo Contents to display in SLL \n");
return;
}
printf("\nThe contents of SLL: \n");
cur = start;
while(cur!=NULL)
{
printf("\n||%d|| USN:%s| Name:%s| Branch:%s| Sem:%d| Ph:%ld|",num,cur->usn, cur-
>name,cur-
>branch, cur->sem,cur->phone);
cur = cur->link;
num++;
}
printf("\n No of student nodes is %d \n",count);
}
void stackdemo()
{
int ch;
while(1)
{
printf("\n~~~Stack Demo using SLL~~~\n");
printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");
printf("\nEnter your choice for stack demo");
scanf("%d",&ch);
switch(ch)
{
case 1: start = insertfront();
break;
case 2: start = deletefront();
break;
case 3: display();
break;
default : return;
}
}
return;
}
int main()
{
int ch,i,n;
while(1)
{
printf("\n~~~Menu~~~");
printf("\nEnter your choice for SLL operation \n");
printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of students: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
start = insertfront();
break;
case 2: display();
break;
case 3: start = insertend();
break;
case 4: start = deleteend();
break;
case 5: stackdemo();
break;
case 6: exit(0);
default: printf("\nPlease enter the valid choice");
}
}
}
9. /* Develop a menu driven Program in C for the following operations on Doubly
Linked List (DLL) of
Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
char ssn[25],name[25],dept[20],designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
NODE first = NULL;
int count=0;
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)
{
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept, enode->designation,
&enode->sal, &enode->phone);
enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
void display()
{
NODE cur;
int nodeno=1;
cur = first;
if(cur == NULL)
printf("\nNo Contents to display in DLL");
while(cur!=NULL)
{
printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone no:
%ld",
nodeno, cur->ssn, cur->name,cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d",count);
}
NODE deletefront()
{
NODE temp;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink== NULL)
{
printf("\nThe employee node with the ssn:%s is deleted", first->ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;
temp->rlink = NULL;
first->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",temp->ssn);
free(temp);
count--;
return first;
}
NODE insertend()
{
NODE cur, temp;
temp = create();
if(first == NULL)
{
return temp;
}
cur= first;
while(cur->rlink!=NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE deleteend()
{
NODE prev,cur;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink == NULL)
{
printf("\nThe employee node with the ssn:%s is deleted",first->ssn);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur = cur->rlink;
}
cur->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",cur->ssn);
free(cur);
prev->rlink = NULL;
count--;
return first;
}
void deqdemo()
{
int ch;
while(1)
{
printf("\nDemo Double Ended Queue Operation");
printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n 3:InsertQueueRear\n
4:DeleteQueueRear\n
5:DisplayStatus\n 6: Exit \n");
scanf("%d", &ch);
switch(ch)
{
case 1: first=insertfront();
break;
case 2: first=deletefront();
break;
case 3: first=insertend();
break;
case 4: first=deleteend();
break;
case 5: display();
break;
default : return;
}
}
}
void main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n7:Double Ended Queue Demo using DLL");
printf("\n8:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of Employees: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;
case 2: display();
break;
case 3: first = insertend();
break;
case 4: first = deleteend();
break;
case 5: first = insertfront();
break;
case 6: first = deletefront();
break;
case 7: deqdemo();
break;
case 8 : exit(0);
default: printf("\nPlease Enter the valid choice");
}
}
}
10 /* Develop a menu driven Program in C for the following operations on STACK of
Integers (Array
Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations */
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int s[MAX];
int top = -1;
void push(int item);
int pop();
void palindrome();
void display();
void main()
{
int choice, item;
while(1)
{
printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
printf("\n=>1.Push an Element to Stack and Overflow demo ");
printf("\n=>2.Pop an Element from Stack and Underflow demo");
printf("\n=>3.Palindrome demo ");
printf("\n=>4.Display ");
printf("\n=>5.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter an element to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2: item = pop();
if(item != -1)
printf("\nElement popped is: %d", item);
break;
case 3: palindrome();
break;
case 4: display();
break;
case 5: exit(1);
default: printf("\nPlease enter valid choice ") ;
break;
}
}
}
void push(int item)
{
if(top == MAX-1)
{
printf("\n~~~~Stack overflow~~~~");
return;
}
top = top + 1 ;
s[top] = item;
}
int pop()
{
int item;
if(top == -1)
{
printf("\n~~~~Stack underflow~~~~");
return -1;
}i
tem = s[top];
top = top - 1;
return item;
}
void display()
{
int i;
if(top == -1)
{
printf("\n~~~~Stack is empty~~~~");
return;
}
printf("\nStack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
printf("\nReverse of stack content are:\n");
for(i=0; i<=top; i++)
printf("| %d |\n", s[i]);
for(i=0; i<=top/2; i++)
{
if( s[i] != s[top-i] )
{
flag = 0;
break;
}
}
if(flag == 1)
{
printf("\nIt is palindrome number");
} else
{
printf("\nIt is not a palindrome number");
}
}
11./* Develop a Program in C for the following Stack Applications
Solving Tower of Hanoi problem with n disks */
#include <stdio.h>
void tower(int n, int source, int temp,int destination)
{
if(n == 0)
return;
tower(n-1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n-1, temp, source, destination);
}
void main()
{
int n;
printf("\nEnter the number of discs: \n");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1);
}
OUTPUT:
Enter the number of discs:
3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are: 7