C Program to find sum of cubic roots of individual digits of a number

Examples:

123 : 11/3 + 21/3 + 31/3

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
long int no;
float sum = 0;
clrscr();
printf(“Enter a Number : “);
scanf(“%ld”, &no);
while (no > 0)
{
sum = sum + pow(no % 10,(1/3));
no = no / 10;
}
printf(“\nSum is %f\n”, sum);
getch();
}

C program to find sum of integers between 101 to 200 whose individual digits are prime in nature

#include<stdio.h>
#include<conio.h>

void main()
{
 int i=102, j, k;
 int sum = 0, digitsum;
 int prime;
 clrscr();
 do
 {
  j = i;
  digitsum = 0;
  while (j > 0)
  {
   digitsum = digitsum + (j % 10);
   j = j / 10;
  }
  prime = 1;
  for (k=2; k<digitsum/2; k++)
  {
   if (digitsum % k == 0)
   {
    prime = 0;
    break;
   }
  }
  if (prime == 1)
  {
   sum = sum + i;
  }
  i++;
 } while (i < 200);
 printf("Sum is %d", sum);
 getch();
}

C program for list implementation using array

#include<stdio.h>
#include<conio.h>

#define MAX 20 //maximum no of elements in the list

//user defined datatypes
struct
{
 int list[MAX];
 int element;  //new element to be inserted
 int pos;  //position of the element to be inserted or deleted
 int length;  //total no of elements
}l;
enum boolean { true, false };
typedef enum boolean boolean;

//function prototypes
int menu(void); //function to display the list of operations
void create(void); //function to create initial set of elements
void insert(int, int); //function to insert the given element at specified position
void delet(int); //function to delete the element at given position
void find(int);  //function to find the position of the given element, if exists
void display(void); //function to display the elements in the list
boolean islistfull(void); //function to check whether the list is full or not
boolean islistempty(void);  //function to check whether the list is empty or not

void main()
{
 int ch;
 int element;
 int pos;

 l.length = 0;

 while(1)
 {
  ch = menu();
  switch (ch)
  {
   case 1:
    l.length = 0;
    create();
    break;
   case 2:
    if (islistfull() != true)
    {
     printf("\tEnter the New element : ");
     scanf("%d", &element);
     printf("\tEnter the Position : ");
     scanf("%d", &pos);
     insert(element, pos);
    }
    else
    {
     printf("\tList if Full. Cannot insert");
     printf("\nPress any key to continue...");
     getch();
    }
    break;
   case 3:
    if (islistempty() != true)
    {
     printf("Enter the position of element to be deleted : ");
     scanf("%d", &pos);
     delet(pos);
    }
    else
    {
     printf("List is Empty.");
     printf("\nPress any key to continue...");
     getch();
    }
    break;
   case 4:
    printf("No of elements in the list is %d", l.length);
    printf("\nPress any key to continue...");
    getch();
    break;
   case 5:
    printf("Enter the element to be searched : ");
    scanf("%d", &element);
    find(element);
    break;
   case 6:
    display();
    break;
   case 7:
    exit(0);
    break;
   default:
    printf("Invalid Choice");
    printf("\nPress any key to continue...");
    getch();
  }
 }
}

//function to display the list of elements
int menu()
{
 int ch;
 clrscr();
 printf("\n\t\t********************************************\n");
 printf("\t\t******LIST Implementation Using Arrays******\n");
 printf("\t\t********************************************\n\n");
 printf("\t1. Create\n\t2. Insert\n\t3. Delete\n\t4. Count\n\t5. Find\n\t6. Display\n\t7. Exit\n\n\tEnter your choice : ");
 scanf("%d", &ch);
 printf("\n\n");
 return ch;
}

//function to create initial set of elements
void create(void)
{
 int element;
 int flag=1;
 while(flag==1)
 {
  printf("Enter an element : ");
  scanf("%d", &element);
  l.list[l.length] = element;
  l.length++;
  printf("To insert another element press '1' : ");
  scanf("%d", &flag);
 }
}

//function to display the elements in the list
void display(void)
{
 int i;
 for (i=0; i<l.length; i++)
  printf("Element %d : %d \n", i+1, l.list[i]);
 printf("Press any key to continue...");
 getch();
}

//function to insert the given element at specified position
void insert(int element, int pos)
{
 int i;
 if (pos == 0)
 {
  printf("\n\nCannot insert at zeroth position");
  getch();
  return;
 }
 if (pos-1 > l.length)
 {
  printf("\n\nOnly %d elements exit. Cannot insert at %d postion", l.length, pos);
  printf("\nPress any key to continue...");
  getch();
 }
 else
 {
  for (i=l.length; i>=pos-1; i--)
  {
   l.list[i+1] = l.list[i];
  }
  l.list[pos-1] = element;
  l.length++;
 }
}

//function to delete the element at given position
void delet(int pos)
{
 int i;
 if(pos == 0)
 {
  printf("\n\nCannot delete at zeroth position");
  getch();
  return;
 }
 if (pos > l.length)
 {
  printf("\n\nOnly %d elements exit. Cannot delete", l.length, pos);
  printf("\nPress any key to continue...");
  getch();
  return;
 }
 for (i=pos-1; i<l.length; i++)
 {
  l.list[i] = l.list[i+1];
 }
 l.length--;
}

//function to find the position of the given element, if exists
void find(int element)
{
 int i;
 int flag = 1;
 for (i=0; i<l.length; i++)
 {
  if(l.list[i] == element)
  {
   printf ("%d exists at %d position",element, i+1);
   flag = 0;
   printf("\nPress any key to continue...");
   getch();
   break;
  }
 }
 if(flag == 1)
 {
  printf("Element not found.\nPress any key to continue...");
  getch();
 }
}

//function to check whether the list is full or not
boolean islistfull(void)
{
 if (l.length == MAX)
  return true;
 else
  return false;
}

//function to check whether the list is empty or not
boolean islistempty(void)
{
 if (l.length == 0)
  return true;
 else
  return false;
}

Sample Output:

1. Create
2. Insert
3. Delete
4. Count
5. Find
6. Display
7. Exit

Enter your Choice: 1
Enter an element: 25
To insert another element press 1: 1

Enter an element: 50
To insert another element press 1: 0

Enter your Choice: 6
Element in the List are
1. 25
2. 50

Enter your Choice: 2
Enter an element: 5
Enter the position: 1
Element in the List are
1. 25
2. 50

Enter your Choice: 3
Enter the position: 3

Enter your Choice: 6
Element in the List are
1. 5
2. 25

Enter your Choice: 4
Number of Elements: 2

Enter your Choice: 7

C program for list implementation using linked list

#include<stdio.h>
#include<stlib.h>
#include<conio.h>

struct node;
typedef struct node *ptr;
typedef ptr list;
typedef ptr position;
typedef int data;

struct node
{
 data element;
 struct node *next;
}

//function prototypes
void makeempty(void); //to make empty list
int isempty(void);  //to check list is empty or not
void create(void);  //to create initial set of elements
position findprevious(data); //to find position of previous element
void delet(data);  //to delete given element
void display(void);  //to display all the elements
void insert(data, int);  //to insert a new element
position getprevposition(int); //to find position of previous element
data getelement(int);  //to find the element at given position
int getposition(data);  //to find position of given element

//global variable declarations
position first;
position last;
position L;
int length;

//to make empty list
void makeempty(void)
{
 position tmp;
 tmp = malloc(sizeof(list));
 tmp->next = NULL;
 L = tmp;
 first = last = NULL;
}

//to check list is empty or not
int isempty(void)
{
 if (L->next = NULL)
  return 1;
 else
  return 0;
}

//to create initial set of elements
void create(void)
{
 data e;
 int n, i;
 position tmp;
 makeempty();
 printf(“Enter number of element : \ “);
 scanf(“%d”, &n);
 for (i=0; i<n; i++)
 {
  printf(“Enter an element : “);
  scanf(“%d”, &e);
  tmp = malloc(sizeof(list));
  tmp->element = e;
  tmp->next = NULL;
  if (L->next == NULL)
  {
   L->next = tmp;
   first = last = tmp;
  }
  else
  {
   last->next = tmp;
   last = tmp;
  }
 }
}

//to display all the elements
void display()
{
 position t;
 for(t=first; t!=NULL; t=t->next)
  printf(“%d --> “, t->element);
 getch();
}
//to find position of previous element
position getprevposition(int index)
{
 position tmp;
 int count = 1;
 if (index>length)
 {
  printf(“Invalid Position”);
  return NULL;
 }
 else
 {
  for (tmp=first; count<index-1; tmp=tmp->next)
   count++;
  return tmp;
 }
}

//to insert a new element
void insert(data x, int p)
{
 position pos, tmp;
 tmp = malloc(sizeof(list));
 tmp->element=x;
 if (p==1)  //first position
 {
  tmp->next = first;
  L->next = tmp;
  first = tmp;
  length++;
 }
 else if (p == length) //last position
 {
  last->next = tmp;
  last = tmp;
  tmp->next = NULL;
 }
 else   //arbitrary position
 {
  pos = getpreviousposition(p);
  if (pos == NULL)
  {
   printf(“Invalid position”);
   getch();
  }
  else
  {
   tmp->next = pos->next;
   pos->next = tmp;
   length++;
  }
 }
}

//to find position of previous element
position findprevious(data x)
{
 position p;
 p = L;
 while (p->next->element!=x && p->next!=NULL)
  p = p->next;
 return p;
}

//to delete given element
void delet(data x)
{
 position p, tmp;
 if (isempty())
 {
  printf(“List is empty”);
  getch();
 }
 else
 {
  p = findprevious(x);
  if (p->next = NULL)
  {
   printf(“Element not found”);
   getch();
  }
  else
  {
   if (p->next == last)
   {
    free (p->next);
    p->next =  NULL;
    last = p;
    length--;
    return;
   }
   if (p == L)
   {
    first = first->next;
   }
   tmp = p->next;
   p->next = tmp->next;
   free(tmp);
   length--;
  }
 }
}

int menu()
{
 int ch;
 printf(“1. Create\n2. Display\n3.Insert\n4.Get Element\n5.Get Position\n6. Delete\n7. Exit\n\n Enter your choice : “);
 scanf(“%d”, &choice);
 return choice;
}

//to find the element at given position
data getelement(int pos)
{
 position p;
 int i;
 p = L;
 if (pos > length)
  return NULL;
 else
 {
  for(i=0; i<pos; i++)
   p = p->next;
  return p->element;
 }
}

//to find position of given element
 int getposition(data e)
{
 position p;
 int i=0;
 for (p=first; p!=NULL; p=p->next)
 {
  if (p->element == e)
   return i+1;
  else
   i++;
 }
 return NULL;
}

void main()
{
 int ch;
 data n, p;
 while(1)
 {
  clrscr();
  ch = menu();
  switch (ch)
  {
   case 1:
    create();
    break;
   case 2:
    display();
    break;
   case 3:
    printf(“Enter an element : “);
    scanf(“%d”, &n);
    printf(“Enter Position : “);
    scanf(“%d”, &p);
    insert (n, p);
    break;
   case 4:
    printf(“Enter an element : “);
    scanf(“%d”, &n);
    delet (n);
    break;
   case 5:
    printf(“Enter position : “);
    scanf(“%d”, &p);
    if (p<1 || p>length)
     printf(“Invalid position”);
    else
     printf(“Element at position %d is %d”, p, getelement(p));
    getch();
    break;
   case 6:
    printf(“Enter an element : “);
    scanf(“%d”, &n);
    if (getposition(n) == NULL)
     printf(“Element doesn’t Exist”);
    else
     printf(“%d exists at position %d”, n, getposition(n));
    getch();
    break;
   default:
    printf(“Invalid Choice”);
    getch();
  }
 }
}

Sample Output:

1. Create
2. Display
3. Insert
4. Delete
5. Get element
6. Get position
7. Exit

Enter your Choice: 1

Enter number of element: 3
Enter an element: 10
Enter an element: 20
Enter an element: 30

Enter your Choice: 3

Enter element: 25
Enter Position: 3

Enter your Choice: 2

10 --> 20 --> 25 --> 30

Enter your Choice: 6

Enter an element:20
20 exists at position 2

Enter your Choice: 4

Enter an element 30

Enter your Choice: 2

10 --> 20 --> 25

Enter your Choice: 7

C program for Binary Search Tree

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

struct searchtree
{
 int element;
 struct searchtree *left,*right;
}*root;
typedef struct searchtree *node;
typedef int ElementType;

node insert(ElementType, node);
node delet(ElementType, node);
void makeempty();
node findmin(node);
node findmax(node);
node find(ElementType, node);
void display(node, int);

void main()
{
 int ch;
 ElementType a;
 node temp;
 makeempty();
 while(1)
 {
  printf("\n1. Insert\n2. Delete\n3. Find\n4. Find min\n5. Find Max\n6. Display\n7. Exit\nEnter Your Choice : ");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("Enter an element : ");
    scanf("%d", &a);
    root = insert(a, root);
    break;
   case 2:
    printf("\nEnter the element to delete : ");
    scanf("%d",&a);
    root = delet(a, root);
    break;
   case 3:
    printf("\nEnter the element to search : ");
    scanf("%d",&a);
    temp = find(a, root);
    if (temp != NULL)
     printf("Element found");
    else
     printf("Element not found");
    break;
   case 4:
    temp = findmin(root);
    if(temp==NULL)
     printf("\nEmpty tree");
    else
     printf("\nMinimum element : %d", temp->element);
    break;
   case 5:
    temp = findmax(root);
    if(temp==NULL)
     printf("\nEmpty tree");
    else
     printf("\nMaximum element : %d", temp->element);
    break;
   case 6:
    if(root==NULL)
     printf("\nEmpty tree");
    else
     display(root, 1);
    break;
   case 7:
    exit(0);
   default:
    printf("Invalid Choice");
  }
 }
}

node insert(ElementType x,node t)
{
 if(t==NULL)
 {
  t = (node)malloc(sizeof(node));
  t->element = x;
  t->left = t->right = NULL;
 }
 else
 {
  if(x < t->element)
   t->left = insert(x, t->left);
  else if(x > t->element)
   t->right = insert(x, t->right);
 }
 return t;
}

node delet(ElementType x,node t)
{
 node temp;
 if(t == NULL)
  printf("\nElement not found");
 else
 {
  if(x < t->element)
   t->left = delet(x, t->left);
  else if(x > t->element)
   t->right = delet(x, t->right);
  else
  {
   if(t->left && t->right)
   {
    temp = findmin(t->right);
    t->element = temp->element;
    t->right = delet(t->element,t->right);
   }
   else if(t->left == NULL)
    t=t->right;
   else
    t=t->left;
  }
 }
 return t;
}
void makeempty()
{
 root = NULL;
}

node findmin(node temp)
{
 if(temp == NULL || temp->left == NULL)
  return temp;
 return findmin(temp->left);
}

node findmax(node temp)
{
 if(temp==NULL || temp->right==NULL)
  return temp;
 return findmax(temp->right);
}

node find(ElementType x, node t)
{
 if(t==NULL) return NULL;
 if(x<t->element) return find(x,t->left);
 if(x>t->element) return find(x,t->right);
 return t;
}

void display(node t,int level)
{
 int i;
 if(t)
 {
  display(t->right, level+1);
  printf(“\n”);
  for(i=0;i<level;i++)
   printf(" ");
  printf("%d", t->element);
  display(t->left, level+1);
 }
}

Sample Output:

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 1
Enter an element : 10

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 1
Enter an element : 20

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 1
Enter an element : 5

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 4
The smallest Number is 5

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 3
Enter an element : 100
Element not Found

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 2
Enter an element : 20

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 6
20
10

1. Insert
2. Delete
3. Find
4. Find Min
5. Find Max
6. Display
7. Exit
Enter your Choice : 7