Showing posts with label Programming in C. Show all posts
Showing posts with label Programming in C. Show all posts

Problems:  Write a C program to implement a circular queue that contains a list of names.  

Data Structure Definition:

Node: A node is a basic building block of a linked list. A node may contain different segments, but basically it contains two parts which are as followed,

 

                                    Data or information part : It stores the data in the node

                                       Link part : It points to the next node or holds the address to the next node.

Source Code:

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define MAX 10

  5. //defining the basic element of the queue
  6. typedef struct Q
  7. {
  8. int R,F;
  9. char *data[MAX];
  10. }Q;

  11. //declaration of functions
  12. void initialise(Q *P);
  13. int empty(Q *P);
  14. int full(Q *P);
  15. void enqueue(Q *P,char *x);
  16. char * dequeue(Q *P);
  17. void print(Q *P);

  18. //main function
  19. int main()
  20. {
  21. //intializing a node named queue
  22. Q q;
  23. //declaration and initialization fof variables
  24.     int op;
  25.     char x[40],*y;
  26.     
  27.     //initializ the queue
  28.     initialise(&q);
  29.         
  30. do{
  31. //displaying the menu
  32. printf("\n\n 1) Enqueue\n 2) Dequeue\n 3) Print\n 4) Quit");
  33. //inputting the input choice 
  34. printf("\n Enter Your Choice : ");
  35. scanf("%d",&op);
  36. //calling functions and passing arguments through the functions as per th choice of the user
  37. switch(op)
  38. case 1: 
  39. printf("\n Enter string : ");
  40.     fflush(stdin);
  41.     scanf("%s",x);
  42.     if(!full(&q))
  43. {
  44. y=(char*)malloc(strlen(x)+1);
  45. strcpy(y,x);
  46. enqueue(&q,y);
  47. }
  48.     else
  49. printf("\n Queue is full !!!!");
  50.         break;
  51.     case 2:
  52.     if(!empty(&q))
  53.     {
  54. y=dequeue(&q);
  55. printf("\n Deleted Data = %s",y);
  56.     }
  57.     else
  58. printf("\n Queue is empty !!!!");
  59.     break;
  60.     case 3: 
  61.     print(&q);a
  62. break;
  63. }
  64.       }while(op!=4);
  65. }

  66. //function to initialize a circular queue
  67. void initialise(Q *P)
  68. {
  69. //declaration of variable
  70. int i;
  71. //initializing the queue
  72. for(i=0;i<MAX;i++)
  73. P->data[i]=NULL;
  74. P->R=-1;
  75. P->F=-1;
  76. }

  77. //function to check if the queue is empty
  78. int empty(Q *P)
  79. {
  80. //if the queue is empty, return 1 else, return 0
  81. if(P->R==-1)
  82. return(1);
  83. return(0);
  84. }

  85. //function to check if the queue is full
  86. int full(Q *P)
  87. {
  88. //return 1 if the queue is full, else return 0
  89. if((P->R+1)%MAX==P->F)
  90. return(1);
  91. return(0);
  92. }

  93. //function to enqueue and element in the queue
  94. void enqueue(Q *P,char *x)
  95. {
  96. //enqueueing the element
  97. if(P->R==-1)
  98. {
  99. P->R=P->F=0;
  100. if(P->data[P->R]!=NULL)
  101. free(P->data[P->R]);
  102. P->data[P->R]=x;
  103. }
  104. else
  105. {
  106. P->R=(P->R+1)%MAX;
  107. if(P->data[P->R]!=NULL)
  108. free(P->data[P->R]);
  109. P->data[P->R]=x;
  110. }
  111. }

  112. //function to dequeue 
  113. char * dequeue(Q *P)
  114. {
  115. //declaring a character type pointer
  116. char *x;
  117. //dequeing the element from the queue
  118. x=P->data[P->F];
  119. if(P->R==P->F)
  120. {
  121. P->R=-1;
  122. P->F=-1;
  123. }
  124. else
  125. P->F=(P->F+1)%MAX;
  126. return(x);
  127. }

  128. //function to display the elements of the queue
  129. void print(Q *P)
  130. {
  131. int i;
  132. if(!empty(P))
  133. {
  134. printf("\n");
  135. for(i=P->F;i!=P->R;i=(i+1)%MAX)
  136. printf("%s\t",P->data[i]);
  137. }
  138.        printf("%s\t",P->data[i]);
  139. }

Output:

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

 Enter string : computer

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

 Enter string : science

 

1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

Enter string : honours

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 3

 

computer        science   honours

 

1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = computer

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = science

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = honours

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

Enter Your Choice : 2

 

 Queue is empty !!!!


What is Armstrong Number?
Armstrong number is a number that is equal of sum of cubed of its digits.
as a example - 153= 13 + 53 + 33 =1+125+27=153
This a pattern... Print this using C language.
*
**
***
****
*****
****
***
**
*

Source Code: 

  1. #include<stdio.h>
  2. int main(){
  3. int row,col,n;
  4. printf("~~~~~Thanks for visit browncodeit.blogspot.com~~~~~~~~~\n");
  5. printf("Enter the numebr: ");
  6. scanf("%d",&n);
  7. for(row=1;row<=n;row++){
  8. for(col=1;col<=row;col++){
  9. printf("*");
  10. }
  11. printf("\n");
  12. }
  13. for(row=n;row>=1;row--){
  14. for(col=1;col<row;col++){
  15. printf("*");
  16. }
  17. printf("\n");
  18. }
  19. return 0;
  20. }



Output:


C program to print pattern like this in a different way just check out with Alphabet. First with row and second with column.
A
BB
CCC
DDDD
EEEEE
ABCD
ABC
AB
A

Source Code:

  1. #include<stdio.h>

  2. int main(){
  3. int row,col,n;
  4. printf("~~~~~Thanks for visit browncodeit.blogspot.com~~~~~~~~~\n");
  5. printf("Enter the numebr: ");
  6. scanf("%d",&n);
  7. for(row=1;row<=n;row++){
  8. for(col=1;col<=row;col++){
  9. printf("%c",row+64);
  10. }
  11. printf("\n");
  12. }
  13. for(row=n;row>=1;row--){
  14. for(col=1;col<row;col++){
  15. printf("%c",col+64);
  16. }
  17. printf("\n");
  18. }
  19. return 0;
  20. }

Output:


This a pattern... 
1
12
123
1234
12345
1234
123
12
1



Source Code:

  1. #include<stdio.h>

  2. int main(){
  3. int row,col,n;
  4. printf("~~~~~Thanks for visit browncodeit.blogspot.com~~~~~~~~~\n");
  5. printf("Enter the numebr: ");
  6. scanf("%d",&n);
  7. for(row=1;row<=n;row++){
  8. for(col=1;col<=row;col++){
  9. printf("%d",col);
  10. }
  11. printf("\n");
  12. }
  13. for(row=n;row>=1;row--){
  14. for(col=1;col<row;col++){
  15. printf("%d",col);
  16. }
  17. printf("\n");
  18. }
  19. return 0;
  20. }


Output:



This is a C program to print love pattern or  Heart pattern  or Leave pattern.

        *****     *****
     *******   *******
   ********* *********
   *******************
     *****************
      ***************
        *************
          ***********
            *********
              *******
                *****
                  ***
                    *

Source Code: 

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i, j, n;
  5.                  //take input from user.
  6.     printf("Enter Row number : ");
  7.     scanf("%d", &n);
  8.                    //  up two side print
  9.     for(i=n/2;i<=n;i+=2)
  10.     {
  11.         for(j=1; j<n-i; j+=2)
  12.         {
  13.             printf(" ");
  14.         }

  15.         for(j=1; j<=i; j++)
  16.         {
  17.             printf("*");
  18.         }

  19.         for(j=1; j<=n-i; j++)
  20.         {
  21.             printf(" ");
  22.         }

  23.         for(j=1; j<=i; j++)
  24.         {
  25.             printf("*");
  26.         }

  27.         printf("\n");
  28.     }
  29.            // down portion print
  30.     for(i=n; i>=1; i--)
  31.     {
  32.         for(j=i; j<n; j++)
  33.         {
  34.             printf(" ");
  35.         }

  36.         for(j=1; j<=(i*2)-1; j++)
  37.         {
  38.             printf("*");
  39.         }

  40.         printf("\n");
  41.     }

  42.     return 0;
  43. }


Output: 



Problem:  Write a c program to print Pyramid Patterns.
 
        *
        * * *
        * * * * *
        * * * * * * *
         * * * * * * * * *
Source Code:
  1.  /*
  2. * C Program to print full pyramid pattern using *
  3. */
  4. #include<stdio.h>
  5. #include<conio.h>
  6. int main()
  7.  {
  8.     int row, space, rows, star=0;
  9.     printf("Enter the number of rows in pyramid\n");
  10.     scanf("%d",&rows);
  11.  
  12.     for(row = 1;row <= rows; row++)
  13. {
  14.      /* Printing spaces */
  15.         for(space = 1; space <= rows-row; space++) 
  16. {
  17.            printf("  ");
  18.         }
  19.         /* Printing stars */
  20.         while(star != (2*row - 1)) 
  21. {
  22.             printf("* ");
  23.             star++;;
  24.         }
  25.         star=0;
  26.         printf("\n");
  27.     }
  28.     getch();
  29.     return 0;
  30. }
Outputs:
         
Problems:   Write a C program to print Patterns Inverted Pyramid
        *****
      ****
    ***
   **
 *
Source Code:
  1. #include <stdio.h>

  2. int main()
  3. {
  4.     int i, j, n;

  5.     printf("Enter N: ");
  6.     scanf("%d", &n);
  7.     for (i=n;i>=1;i--)
  8.      {
  9.      
  10.         for(j=1;j<=i;j++)
  11.           {
  12.           printf("*");
  13.           }
  14.            printf("\n");
  15.      }
  16.     
  17.     
  18.      return 0;
  19.  }


Output:


This is half pyramid different design.

Source Code:


#include <stdio.h>

int main()
{
    int i, j, n;

    printf("Enter N: ");
    scanf("%d", &n);
    for (i=1;i<=n;i++)
     {
      for(j=1;j<=(n-i);j++)
      {
      printf(" ");
        }
       for(j=1;j<=i;j++)
         {
        printf("*");
         }
     printf("\n");
 }
 }


Outputs:




This is Hello World program using C.

Source Code:

  1. #include<stdio.h>
  2. int main() 
  3. {
  4.     printf("Hello BrownCodeIt!");
  5.     return 0;
  6. }


Outputs:

Hello BrownCodeIt!
Write a C program  to print number pattern.   | Number increasing order  | Half Pyramid with Number.
                                                                   

Source Code :

  1. /**
  2.  * C program to print number pattern
  3.  */

  4. #include <stdio.h>

  5. int main()
  6. {
  7.     int i, j, N;

  8.     printf("Enter N: ");
  9.     scanf("%d", &N);

  10.     for(i=1; i<=N; i++)
  11.     {
  12.         // Logic to print numbers
  13.         for(j=1; j<=i; j++)
  14.         {
  15.             printf("%d", j);
  16.         }

  17.         printf("\n");
  18.     }

  19.     return 0;
  20. }

Outputs:


Enter N: 10
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910

Output 2:



C Program  to print inverted half pyramid. 

This is a C program which implements queue using linklist.


Source Code:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. //A structure is created for the node in queue

  5. struct queu
  6. {
  7. int info;
  8. struct queu *next;//Next node address
  9. };
  10. typedef struct queu *NODE;
  11. //This function will push an element into the queue

  12. NODE push(NODE rear)
  13. {
  14. NODE NewNode;
  15. //New node is created to push the data
  16. NewNode=(NODE)malloc(sizeof(struct queu));
  17. printf ("\nEnter the no to be pushed = ");

  18. scanf ("%d",&NewNode->info);
  19. NewNode->next=NULL;
  20. //setting the rear pointer
  21. if (rear != NULL)
  22. rear->next=NewNode;
  23. rear=NewNode;
  24. return(rear);
  25. }
  26. //This function will pop the element from the queue

  27. NODE pop(NODE f,NODE r)
  28. {
  29. //The Queue is empty when the front pointer is NULL

  30. if(f==NULL)
  31. printf ("\nThe Queue is empty");
  32. else
  33. {
  34. printf ("\nThe poped element is = %d",f->info);

  35. if(f != r)
  36. f=f->next;
  37. else
  38. f=NULL;
  39. }
  40. return(f);
  41. }
  42. //Function to display the element of the queue

  43. void traverse(NODE fr,NODE re)
  44. {
  45. //The queue is empty when the front pointer is NULL

  46. if (fr==NULL)
  47. printf ("\nThe Queue is empty");
  48. else
  49. {
  50. printf ("\nThe element(s) is/are = ");

  51. while(fr != re)
  52. {
  53. printf("%d ",fr->info);
  54. fr=fr->next;
  55. };
  56. printf ("%d",fr->info);
  57. }
  58. }
  59. int main()
  60. {
  61. int choice;
  62. char option;
  63. //declaring the front and rear pointer
  64. NODE front, rear;
  65. //Initializing the front and rear pointer to NULL

  66. front = rear = NULL;
  67. do
  68. {
  69. //clrscr();
  70. printf ("1. Push\n");
  71. printf ("2. Pop\n");
  72. printf ("3. Traverse\n");
  73. printf ("\n\nEnter your choice = ");

  74. scanf ("%d",&choice);
  75. switch(choice)
  76. {
  77. case 1:
  78. //calling the push function
  79. rear = push(rear);
  80. if (front==NULL)
  81. {
  82. front=rear;
  83. }
  84. break;
  85. case 2:
  86. //calling the pop function by passing
  87. //front and rear pointers
  88. front = pop(front,rear);
  89. if (front == NULL)
  90. rear = NULL;
  91. break;
  92. case 3:
  93. traverse(front,rear);
  94. break;
  95. }
  96. printf ("\n\nPress (Y/y) to continue = ");

  97. fflush(stdin);
  98. scanf ("%c",&option);
  99. }while(option == 'Y' || option == 'y');
  100. }

Output:

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 14

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 25

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 36

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 2

 

The poped element is = 14

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 3

 

The element(s) is/are = 25 36

 

Press (Y/y) to continue = N

 

Data structure definition:-

 The data structure Node represents a single node in the linked list]

Structure Node

        (   data to hold the relevant data value in a Node,

            next that holds the address of the next Node      

       )      




Source Code:   

  1. #include<conio.h>
  2. #include<stdio.h>
  3. #define max 5
  4. int queue[max],front=0,rear=0;
  5. int menu();
  6. void enqueue();
  7. void dequeue();
  8. void display();
  9. void main()
  10. {
  11. int ch;
  12. clrscr();
  13. printf("\nQueues using Arrays\n");
  14. do
  15. {
  16.     ch=menu();
  17.     switch(ch)
  18.     {
  19.     case 1: enqueue();
  20.     break;
  21.     case 2: dequeue();
  22.     break;
  23.     case 3: display();
  24.     break;
  25.     case 4: exit();
  26.     break;
  27.     default:printf("\n Please enter a valid choice!!");
  28.     }
  29. }while(1);
  30. }
  31.  
  32. int menu()
  33.     {
  34.     int ch;
  35.     printf("\n1.ENQUEUE \n2.DEQUEUE \n3.DISPLAY \n4.EXIT");
  36.     printf("\nEnter your Choice:");
  37.     scanf("%d",&ch);
  38.     return ch;
  39.     }
  40.  
  41. void enqueue()
  42. {
  43.     int element;
  44.     if(rear==max)
  45.     {
  46.         printf("\nOverflow!!");
  47.     }
  48.     else
  49.     {
  50.         printf("\nEnter Element:");
  51.         scanf("%d",&element);
  52.         queue[rear++]=element;
  53.         printf("\n %d Enqueued at %d",element,rear);
  54.     }
  55.  
  56. }
  57.  
  58. void dequeue()
  59. {
  60.     if(rear==front)
  61.     {
  62.         printf("\nUnderflow!!");
  63.     }
  64.     else
  65.     {
  66.         front++;
  67.         printf("\nElement is Dequeued from %d",front);
  68.    }
  69. }
  70. void display()
  71. {
  72.     int i;
  73.     if(front==rear)
  74.     {
  75.         printf("\nQueue is Empty!!!");
  76.     }
  77.     else
  78.     {
  79.         printf(" \n");
  80.         for(i=front;i<max;i++)
  81.         {
  82.             printf(" | %d ",queue[i]);
  83.         }
  84.             printf("|");
  85.     }
  86. }

Output:

Queues using Arrays
 
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:12
 
 12 Enqueued at 1
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:23
 
 23 Enqueued at 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:
23 Enqueued at 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:34
 
 34 Enqueued at 3
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:45
 
 45 Enqueued at 4
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:56
 
 56 Enqueued at 5
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:2
 
Element is Dequeued from 1
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:2
 
Element is Dequeued from 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:3
 
 | 34  | 45  | 56 |
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice: 4
 
 
 

Write a C program to implement stack using linked list

DATA STRUCTURE DEFINITION

_____________________________________________________________________

NODE: Data {To store data  in  the Node}

Next {To store link of  the next Node}

_____________________________________________________________________

Source Code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct stack
  4. {
  5.     int data;
  6.     struct node *next;
  7. };
  8. typedef struct stack stack;
  9.  
  10. void push(int data);
  11. int pop();
  12. void display();
  13.  
  14. int top=0,size;
  15. stack *head=NULL;
  16. void main()
  17. {
  18.     int no,ch,e,r;
  19.     printf("\nEnter the size of stack  ");
  20.     scanf("%d", &size);
  21.     printf("\n 1 - Push");
  22.     printf("\n 2 - Pop");
  23.     printf("\n 3 - Dipslay");
  24.     printf("\n 0 - Exit");
  25.     do
  26.     {
  27.         printf("\n Enter choice : ");
  28.         scanf("%d", &ch);
  29.  
  30.         switch (ch)
  31.         {
  32.         case 0:
  33.             break;
  34.         case 1:
  35.             printf("\nEnter data : ");
  36.             scanf("%d", &no);
  37.             push(no);
  38.             break;
  39.         case 2:
  40.             r=pop();
  41.             printf("\n%d popped",r);
  42.             break;
  43.         case 3:
  44.             display();
  45.             break;
  46.         default :
  47.             printf(" Wrong choice, Please enter correct choice  ");
  48.             break;
  49.         }
  50.     }while(ch!=0);
  51. }
  52. void push(int v)
  53. {
  54.    if(top<size){
  55.    stack *new_node;
  56.    new_node=(stack*)malloc(sizeof(stack));
  57.    new_node->data=v;
  58.    new_node->next=NULL;
  59.    if(head==NULL)
  60.    head=new_node;
  61.    else
  62.    {
  63.                new_node->next=head;
  64.                head=new_node;
  65.    }
  66.    top++;
  67. }
  68.    else
  69.    printf("\nStack is full!");
  70. }
  71. int pop()
  72. {
  73.                stack *tmp;
  74.                int v;
  75.                if(top>=1)
  76.                {
  77.                               tmp=head;
  78.                               head=head->next;
  79.                               v=tmp->data;
  80.                               free(tmp);
  81.                               top--;
  82.                }
  83.                else
  84.                v=-999;
  85.                return v;
  86. }
  87. void display()
  88. {
  89.                stack *p;             
  90.                p=head;
  91.                if(p!=NULL)
  92.                {
  93.                               printf("\nLinked List\n");
  94.                               while(p!=NULL)
  95.                               {
  96.                                              printf("%d ",p->data);      
  97.                                              p=p->next;
  98.                               }
  99.    }
  100. }

 

Output:

Enter the size of stack  8 

 1 - Push

 2 - Pop

 3 - Dipslay

 4 - Exit

 Enter choice : 1

Enter data : 33

 Enter choice : 1

Enter data : 65

 Enter choice : 1

Enter data : 22

 Enter choice : 1

Enter data : 77

 Enter choice : 2

77 popped

 Enter choice : 3

Linked List

22 65 33

 Enter choice : 4