Welcome to Millillion Tech

Millillion Tech is not a only blog, it is a landmark of technological knowledge.

Be first in self-enrichment!

Visit our blog and enrich yourself and be all in all of your self-development.

?া?া? ?? ??্?া? ?ে?ে ?া??ে ?া?া? ?? ?্???া? ??ু?

................................................................................................................................................................................................................................

?ু?্?ী?ি ??ি?া? ??ু?, ???া?ে ?ঁ??ে ??ু?

................................................................................................................................................................................................................................

??া?ে? ?? ?ো?্? ?ে??ি?ে ???ু ?া??া?ু?

................................................................................................................................................................................................................................

Translate

Saturday, August 22, 2015

Age Calculator using C Programming

Millilliontech

#include<stdio.h>
#include<time.h>

int leapYearFeb(int year, int mon) //Leap year checking
{
    int flag = 0;
    if (year % 100 == 0)
    {
        if (year % 400 == 0)
        {
            if (mon == 2)
            {
                flag = 1;
            }
        }
    }
    else if (year % 4 == 0)
    {
        if (mon == 2)
        {
            flag = 1;
        }
    }
    return (flag);
}

int main(void)
{
    int daysInMon[] = {31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31};  //Days in month
    int days, month, year;
    char dob[100];
    time_t ts;
    struct tm *ct;

    printf("Enter Date of Birth (DD MM YY): ");
    scanf("%d%d%d", &days, &month, &year);
    printf("\nDate of Birth: %d/%d/%d", days, month, year);

    ts = time(NULL);   //Find the current month
    ct = localtime(&ts);
    printf("\nCurrent Date: %d/%d/%d",ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
    days = daysInMon[month - 1] - days + 1;

    if (leapYearFeb(year, month)) //Checking the given year Leap or not
    {
        days = days + 1;
    }

    /*Calculating the num of year, month and date*/
    days = days + ct->tm_mday;
    month = (12 - month) + (ct->tm_mon);
    year = (ct->tm_year + 1900) - year - 1;

    if (leapYearFeb((ct->tm_year + 1900), (ct->tm_mon + 1)))
    {
        if (days >= (daysInMon[ct->tm_mon] + 1))
        {
            days = days - (daysInMon[ct->tm_mon] + 1);
            month = month + 1;
        }
    }
    else if (days >= daysInMon[ct->tm_mon])
    {
        days = days - (daysInMon[ct->tm_mon]);
        month = month + 1;
    }

    if (month >= 12)
    {
        year = year + 1;
        month = month - 12;
    }

    /*Result portion*/

    printf("\nAge: %d year %d months and %d days", year, month, days);
    return 0;
  }


Output:
millilliontech

Download Source File

C Programming Binary Search Tree (BST) implementation insert, delete and recursive travarse (inorder, preorder, postorder)

millilliontech


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

    struct btnode
    {
        int value;
        struct btnode *l;
        struct btnode *r;

    }*root = NULL, *temp = NULL, *t2, *t1;

    void delete1();
    void insert();
    void delete();
    void inorder(struct btnode *t);
    void create();
    void search(struct btnode *t);
    void preorder(struct btnode *t);
    void postorder(struct btnode *t);
    void search1(struct btnode *t,int data);
    int smallest(struct btnode *t);
    int largest(struct btnode *t);

    int flag = 1;

    void main()
    {
        int ch;
        printf("\nOPERATIONS ---");
        printf("\n1 - Insert an element into tree\n");
        printf("2 - Delete an element from the tree\n");
        printf("3 - Inorder Traversal\n");
        printf("4 - Preorder Traversal\n");
        printf("5 - Postorder Traversal\n");
        printf("6 - Exit\n");

        while(1)
        {
            printf("\nEnter your choice : ");
            scanf("%d", &ch);

            switch (ch)
            {
            case 1:
                insert();
                break;
            case 2:
                delete();
                break;
            case 3:
                inorder(root);
                break;
            case 4:
                preorder(root);
                break;
            case 5:
                postorder(root);
                break;
            case 6:
                exit(0);
            default :
                printf("Wrong choice, Please enter correct choice  ");
                break;
            }
        }
    }

    /* To insert a node in the tree */

    void insert()
    {
        create();
        if (root == NULL)
            root = temp;
        else
            search(root);
    }
    /* To create a node */

    void create()
    {
        int data;
        printf("Enter data of node to be inserted : ");
        scanf("%d", &data);
        temp = (struct btnode *)malloc(1*sizeof(struct btnode));
        temp->value = data;
        temp->l = temp->r = NULL;
    }
    /* Function to search the appropriate position to insert the new node */

    void search(struct btnode *t)
    {
        if ((temp->value > t->value) && (t->r != NULL))      /* value more than root node value insert at right */
            search(t->r);
        else if ((temp->value > t->value) && (t->r == NULL))
            t->r = temp;
        else if ((temp->value < t->value) && (t->l != NULL))    /* value less than root node value insert at left */
            search(t->l);
        else if ((temp->value < t->value) && (t->l == NULL))
            t->l = temp;
    }
    /* recursive function to perform inorder traversal of tree */

    void inorder(struct btnode *t)
    {
        if (root == NULL){
            printf("No elements in a tree to display");
            return;
        }
        if (t->l != NULL)
            inorder(t->l);
        printf("%d ", t->value);
        if (t->r != NULL)
            inorder(t->r);
    }
    /* To check for the deleted node */

    void delete()
    {
        int data;
        if (root == NULL){
            printf("No elements in a tree to delete");
            return;
        }
        printf("Enter the data to be deleted : ");
        scanf("%d", &data);
        t1 = root;
        t2 = root;
        search1(root, data);
    }
    /* To find the preorder traversal */

    void preorder(struct btnode *t)
    {
        if (root == NULL){
            printf("No elements in a tree to display");
            return;
        }
        printf("%d ", t->value);
        if (t->l != NULL)
            preorder(t->l);
        if (t->r != NULL)
            preorder(t->r);
    }
    /* To find the postorder traversal */

    void postorder(struct btnode *t)
    {
        if (root == NULL){
            printf("No elements in a tree to display ");
            return;
        }
        if (t->l != NULL)
            postorder(t->l);
        if (t->r != NULL)
            postorder(t->r);
        printf("%d ", t->value);
    }

    /* Search for the appropriate position to insert the new node */

    void search1(struct btnode *t, int data)
    {
        if ((data>t->value)){
            t1 = t;
            search1(t->r, data);
        }
        else if ((data < t->value)){
            t1 = t;
            search1(t->l, data);
        }
        else if ((data==t->value)){
            delete1(t);
        }
    }
    /* To delete a node */

    void delete1(struct btnode *t)
    {
        int k;

        /* To delete leaf node */

        if ((t->l == NULL) && (t->r == NULL)){
            if (t1->l == t){
                t1->l = NULL;
            }
            else{
                t1->r = NULL;
            }
            t = NULL;
            free(t);
            return;
        }
        /* To delete node having one left hand child */

        else if ((t->r == NULL)){
            if (t1 == t){
                root = t->l;
                t1 = root;
            }
            else if (t1->l == t){
                t1->l = t->l;
            }
            else{
                t1->r = t->l;
            }
            t = NULL;
            free(t);
            return;
        }
        /* To delete node having right hand child */

        else if (t->l == NULL){
            if (t1 == t){
                root = t->r;
                t1 = root;
            }
            else if (t1->r == t)
                t1->r = t->r;
            else
                t1->l = t->r;
            t == NULL;
            free(t);
            return;
        }
        /* To delete node having two child */

        else if ((t->l != NULL) && (t->r != NULL)){
            t2 = root;
            if (t->r != NULL){
                k = smallest(t->r);
                flag = 1;
            }
            else{
                k =largest(t->l);
                flag = 2;
            }
            search1(root, k);
            t->value = k;
        }
    }

    /* To find the smallest element in the right sub tree */

    int smallest(struct btnode *t)
    {
        t2 = t;
        if (t->l != NULL){
            t2 = t;
            return(smallest(t->l));
        }
        else
            return (t->value);
    }

    /* To find the largest element in the left sub tree */

    int largest(struct btnode *t)
    {
        if (t->r != NULL){
            t2 = t;
            return(largest(t->r));
        }
        else
            return(t->value);
    }



Output:
millilliontech


Download Source File

C Programming Binary Search Tree (BST) implementation insert, search and recursive travarse (inorder, preorder, postorder)

millilliontech


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

typedef struct BST {
   int data;
   struct BST *lchild, *rchild;
} node;

void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);

void main() {
   int choice;
   char ans = 'N';
   int key;
   node *new_node, *root, *tmp, *parent;
   node *get_node();
   root = NULL;

   printf("\nProgram For Binary Search Tree ");
   do {
      printf("\n1.Create");
      printf("\n2.Search");
      printf("\n3.Recursive Traversals");
      printf("\n4.Exit");
      printf("\nEnter your choice :");
      scanf("%d", &choice);

      switch (choice) {
      case 1:
         do {
            new_node = get_node();
            printf("\nEnter The Element ");
            scanf("%d", &new_node->data);

            if (root == NULL) /* Tree is not Created */
               root = new_node;
            else
               insert(root, new_node);

            printf("\nWant To enter More Elements?(y/n)");
            ans = getch();
         } while (ans == 'y');
         break;

      case 2:
         printf("\nEnter Element to be searched :");
         scanf("%d", &key);

         tmp = search(root, key, &parent);
         printf("\nParent of node %d is %d", tmp->data, parent->data);
         break;

      case 3:
         if (root == NULL)
            printf("Tree Is Not Created");
         else {
            printf("\nThe Inorder display : ");
            inorder(root);
            printf("\nThe Preorder display : ");
            preorder(root);
            printf("\nThe Postorder display : ");
            postorder(root);
         }
         break;
      }
   } while (choice != 4);
}
/*
 Get new Node
 */
node *get_node() {
   node *temp;
   temp = (node *) malloc(sizeof(node));
   temp->lchild = NULL;
   temp->rchild = NULL;
   return temp;
}
/*
 This function is for creating a binary search tree
 */
void insert(node *root, node *new_node) {
   if (new_node->data < root->data) {
      if (root->lchild == NULL)
         root->lchild = new_node;
      else
         insert(root->lchild, new_node);
   }

   if (new_node->data > root->data) {
      if (root->rchild == NULL)
         root->rchild = new_node;
      else
         insert(root->rchild, new_node);
   }
}
/*
 This function is for searching the node from
 binary Search Tree
 */
node *search(node *root, int key, node **parent) {
   node *temp;
   temp = root;
   while (temp != NULL) {
      if (temp->data == key) {
         printf("\nThe %d Element is Present", temp->data);
         return temp;
      }
      *parent = temp;

      if (temp->data > key)
         temp = temp->lchild;
      else
         temp = temp->rchild;
   }
   return NULL;
}
/*
 This function displays the tree in inorder fashion
 */
void inorder(node *temp) {
   if (temp != NULL) {
      inorder(temp->lchild);
      printf("%d ", temp->data);
      inorder(temp->rchild);
   }
}
/*
 This function displays the tree in preorder fashion
 */
void preorder(node *temp) {
   if (temp != NULL) {
      printf("%d ", temp->data);
      preorder(temp->lchild);
      preorder(temp->rchild);
   }
}

/*
 This function displays the tree in postorder fashion
 */
void postorder(node *temp) {
   if (temp != NULL) {
      postorder(temp->lchild);
      postorder(temp->rchild);
      printf("%d ", temp->data);
   }
}


Output:
millilliontech

Download Source File

C Programming Binary Search Tree (BST) implementation insert and display it inorder, preorder and postorder

millilliontech

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

typedef struct binary_tree
{
    struct binary_tree *left_sub_tree;
    int val;
    struct binary_tree *right_sub_tree;
} tree;

tree *root;

tree *insert(tree *r, tree *n)
{
    if(r == NULL){
        r = n;
        r->left_sub_tree = NULL;
        r->right_sub_tree = NULL;
        return;
    }

    else if(n->val < r->val)
        r->left_sub_tree = insert(r->left_sub_tree,n);
    else if(n->val > r->val)
        r->right_sub_tree = insert(r->right_sub_tree,n);

    else printf("\n\nError!!!");
    return r;
}

tree **search(tree *r, tree **prev, int *s)
{
    if(r){
        if(s < r->val){
            prev = &r->left_sub_tree;
            r = *search(r->left_sub_tree, prev, s);
        }
    }

    else if(s > r->val){
        prev = &r->right_sub_tree;
        r = *search(r->right_sub_tree, prev, s);
    }

    else if(s == r->val)
        return prev;

    return 0;
}

del()
{
    tree **p, *q, **prev;
    int d_key;
    p = (struct binary_tree **)malloc(sizeof(struct binary_tree));
    printf("\n\nEnter element to be deleted: ");
    scanf("%d", &d_key);
    prev = &root;
    p = search(root, prev, d_key);

    if(*p == NULL){
        printf("\n\nCannot delete empty node.");
        return 0;
    }

    else if((*p)->right_sub_tree == NULL){
        q = *p;
        (*p) = (*p)->left_sub_tree;
    }

    else if((*p)->left_sub_tree == NULL){
        q = (*p);
        (*p) = (*p)->right_sub_tree;
    }

    else {
        for(q = (*p)->right_sub_tree ; q->left_sub_tree ; q = q->left_sub_tree);
        q->left_sub_tree = (*p)->left_sub_tree;
        q = (*p);
        (*p) = (*p)->right_sub_tree;
    }

    printf("%d is deleted", d_key);
    free(q);
    return 0;

}

tree *inorder(tree *r)
{
    if(r){
        inorder(r->left_sub_tree);
        printf("\n\t %d ", r->val);
        inorder(r->right_sub_tree);
    }
    return 0;
}

tree *preorder(tree *r)
{
    if(r){
        printf("\n\t %d ", r->val);
        preorder(r->left_sub_tree);
        preorder(r->right_sub_tree);
    }
    return 0;
}

tree *postorder(tree *r)
{
    if(r){
        preorder(r->left_sub_tree);
        preorder(r->right_sub_tree);
        printf("\n\t %d ", r->val);
    }
    return 0;
}


void display()
{
    printf("\n\n\tThis is inorder display\n");
    inorder(root);
    getch();

    printf("\n\n\tThis is preorder display\n");
    preorder(root);
    getch();

    printf("\n\n\tThis is postrder display\n");
    postorder(root);
    getch();

}

void main()
{
    int ch, s_key, d_key;
    tree *ptr;

    for(;;){
        printf("\n1.Insert\n2.Display\n");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                ptr = (struct binary_tree*)malloc(sizeof(struct binary_tree));
                printf("Enter : ");
                scanf("%d", &ptr->val);
                ptr->left_sub_tree = ptr->right_sub_tree = NULL;
                root = insert(root,ptr);
                break;

            case 2:
                display();
                break;

            case 3:
                exit(0);


        }
    }
}



Output:
Millilliontech


Download Source File

C Programming Infix To Pretfix Conversation

millilliontech

# include <stdio.h>
# include <string.h>
# define MAX 20
void infixtoprefix(char infix[20], char prefix[20]);
void reverse(char array[30]);
char pop();
void push(char symbol);
int isOperator(char symbol);
int prcd(char symbol);
int top = -1;
char stack[MAX];

main() {
char infix[20], prefix[20], temp;
printf("Enter infix operation: ");
gets(infix);
infixtoprefix(infix, prefix);
reverse(prefix);
puts((prefix));
}
//--------------------------------------------------------
void infixtoprefix(char infix[20], char prefix[20]) {
int i, j = 0;
char symbol;
stack[++top] = '#';
reverse(infix);
for (i = 0; i < strlen(infix); i++) {
symbol = infix[i];
if (isOperator(symbol) == 0) {
  prefix[j] = symbol;
  j++;
} else {
  if (symbol == ')') {
    push(symbol);
  } else if (symbol == '(') {
    while (stack[top] != ')') {
      prefix[j] = pop();
      j++;
    }
    pop();
  } else {
    if (prcd(stack[top]) <= prcd(symbol)) {
      push(symbol);
    } else {
      while (prcd(stack[top]) >= prcd(symbol)) {
        prefix[j] = pop();
        j++;
      }
      push(symbol);
    }
  //end for else
     }
   }
  //end for else
   }
  //end for for
  while (stack[top] != '#') {
    prefix[j] = pop();
    j++;
   }
  prefix[j] = '\0';
 }
 ////--------------------------------------------------------
 void reverse(char array[30]) {
 // for reverse of the given expression
 int i, j;
 char temp[100];
 for (i = strlen(array) - 1, j = 0; i + 1 != 0; --i, ++j) {
  temp[j] = array[i];
 }
 temp[j] = '\0';
 strcpy(array, temp);//copying temp array to array
  // return array;
 }
 //--------------------------------
  char pop() {
  char a;
  a = stack[top];
  top--;
  return a;
  }
 //----------------------------------
 void push(char symbol) {
 top++;
 stack[top] = symbol;
 }
 //------------------------------------------
 int prcd(char symbol) {
 // returns the value that helps in the precedence
 switch (symbol) {
  case '+':
   case '-':
    return 2;
    break;
  case '*':
   case '/':
    return 4;
    break;
   case '$':
   case '^':
    return 6;
    break;
   case '#':
   case '(':
   case ')':
    return 1;
    break;
  }
}
//-------------------------------------------------
int isOperator(char symbol) {
switch (symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '&':
case '(':
case ')':
  return 1;
  break;
default:
  return 0;
 // returns 0 if the symbol is other than given above
  }
 }



Output:
Shohag

Download Source File

C Programming Infix To Postfix Conversation

Millilliontech


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

#define BLANK ' '
#define TAB '\t'
#define MAX 50

void push(long int symbol);
long int pop();
void infix_to_postfix();
int priority(char symbol);
int isEmpty();
int white_space(char);

char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;

int main()
{
    top=-1;
    printf("Enter infix : ");
    gets(infix);
    infix_to_postfix();
    printf("Postfix : %s\n",postfix);
    getch();
    return 1;
}

void infix_to_postfix()
{
    unsigned int i,p=0;
    char next;
    char symbol;
    for(i=0;i<strlen(infix);i++)
    {
        symbol=infix[i];
        if(!white_space(symbol))
        {
            switch(symbol)
            {
            case '(':
                push(symbol);
                break;
            case ')':
                while((next=pop())!='(')
                    postfix[p++] = next;
                break;
            case '+':
            case '-':
            case '*':
            case '/':
            case '%':
            case '^':
                while( !isEmpty( ) &&  priority(stack[top])>= priority(symbol) )
                    postfix[p++]=pop();
                push(symbol);
                break;
            default: /*if an operand comes*/
                 postfix[p++]=symbol;
            }
        }
    }
    while(!isEmpty( ))
        postfix[p++]=pop();
    postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}

/*This function returns the priority of the operator*/
int priority(char symbol)
{
    switch(symbol)
    {
    case '(':
        return 0;
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
    case '%':
        return 2;
    case '^':
        return 3;
    default :
        return 0;
    }
}

void push(long int symbol)
{
    if(top>MAX)
    {
        printf("Stack overflow\n");
        exit(1);
    }
    stack[++top]=symbol;
}

long int pop()
{
    if( isEmpty() )
    {
        printf("Stack underflow\n");
        exit(1);
    }
    return (stack[top--]);
}
int isEmpty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

int white_space(char symbol)
{
    if( symbol == BLANK || symbol == TAB )
        return 1;
    else
        return 0;
}

long int eval_post()
{
    long int a,b,temp,result;
    unsigned int i;

    for(i=0;i<strlen(postfix);i++)
    {
        if(postfix[i]<='9' && postfix[i]>='0')
            push(postfix[i]-'0');
        else
        {
            a=pop();
            b=pop();
            switch(postfix[i])
            {
            case '+':
                temp=b+a;
                break;
            case '-':
                temp=b-a;
                break;
            case '*':
                temp=b*a;
                break;
            case '/':
                temp=b/a;
                break;
            case '%':
                temp=b%a;
                break;
            case '^':
                temp=pow(b,a);
            }
            push(temp);
        }
    }
    result=pop();
    return result;
}


Output:
millillion tech

Download Source File

Wednesday, June 24, 2015

Using linked list, write a C program, that will input some students' Id, Name, Department and CGPA respectively and print them.

millillion tech


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 20


struct diu_cse
{
    int id;
    char name[SIZE];
    char department[SIZE];
    float cgpa;
    struct diu_cse *next;
};
struct diu_cse *head=NULL;
struct diu_cse *curr=NULL;

struct diu_cse* create_list(int id, char name[],char department[], float cgpa)
{
    struct diu_cse *ptr=(struct diu_cse*) malloc(sizeof(struct diu_cse));
    if(ptr==NULL)
    {
        printf("Node creation failed\n");
        return;
    }
    ptr->id = id;
    strcpy(ptr->name, name);
    strcpy(ptr->department, department);
    ptr->cgpa = cgpa;
    ptr->next=NULL;
    head=curr=ptr;
    return ptr;

}

struct diu_cse* add_to_list(int id, char name[], char department[], float cgpa)
{
    if(head==NULL)
    {
        return(create_list(id, name, department, cgpa));
    }
    struct diu_cse *ptr=( struct diu_cse*) malloc(sizeof(struct diu_cse));
    if(ptr==NULL)
    {
        printf("Node creation failed");
        return;
    }
    ptr->id = id;
    strcpy(ptr->name, name);
    strcpy(ptr->department, department);
    ptr->cgpa = cgpa;
    ptr->next=NULL;
    curr->next=ptr;
    curr=ptr;
    return ptr;
}
void print_list()
{
    struct diu_cse *ptr=head;
    while(ptr->next != NULL){
    printf("--------------------Printing List--------------------\n\n");
    printf(" %d  %s  %s  %.2f -> ", ptr->id, ptr->name, ptr->department, ptr->cgpa);
    ptr=ptr->next;
    }

    if(ptr->next == NULL)
        printf(" %d  %s  %s  %.2f ", ptr->id, ptr->name, ptr->department, ptr->cgpa);
        printf("\n\n");
}

int main()
{
    int id,i,n;
    char name[SIZE], department[SIZE];
    float cgpa;

    printf("How many node you want to create? ");
    scanf("%d", &n);
    printf("\nEnter the Id, Name, Department and CGPA of students\n");

    for(i=0;i<n;i++){
    scanf("%d", &id);
    fflush(stdin);
    gets(name);
    gets(department);
    scanf("%f", &cgpa);
    add_to_list(id, name, department, cgpa);
    }

    print_list();
    return 0;
}


millilliontech

Download source file