Saturday, 25 April 2015

Reverse a linked list using recursion and without recursion

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

/* Link list node */
struct node
{
    int data;
    struct node* next;
};
 node * reverse1( node * ptr , node * previous) //with recursion
{
    node * temp;
    if(ptr->next == NULL) {
        ptr->next = previous;
        return ptr;
    } else {
        temp = reverse1(ptr->next, ptr);
        ptr->next = previous;
        return temp;
    }
}
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)  //without recursion
{
    struct node* prev   = NULL;
    struct node* current = *head_ref;
    struct node* next;
    while (current != NULL)
    {
        next  = current->next;
        current->next = prev;  
        prev = current;
        current = next;
    }
    *head_ref = prev;
}

/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
    /* allocate node */
    struct node* new_node =
            (struct node*) malloc(sizeof(struct node));
           
    /* put in the data  */
    new_node->data  = new_data;
               
    /* link the old list off the new node */
    new_node->next = (*head_ref);  
       
    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}

/* Function to print linked list */
void printList(struct node *head)
{
    struct node *temp = head;
    while(temp != NULL)
    {
        printf("%d  ", temp->data);  
        temp = temp->next;
    }
}  


int main()
{
    /* Start with the empty list */
    struct node* head = NULL;
 
     push(&head, 20);
     push(&head, 4);
     push(&head, 15);
     push(&head, 85);    
   
     printList(head);  
     head=reverse1(head,NULL);                    
     printf("\n Reversed Linked list \n");
     printList(head);  
 
    return 0;
}

Thursday, 23 April 2015

Prime Number

Program to check for prime number:
Solution:
#include <stdio.h> int main() { int n, i, c = 0; printf("Enter any number n:"); scanf("%d", &n); /*logic*/ for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) { printf("n is a Prime number"); } else { printf("n is not a Prime number"); } return 0; }
Prime number between two number is given by:

#include <stdio.h>
int main()
{
  int n1, n2, i, j, flag;
  printf("Enter two numbers(intevals):");
  scanf("%d %d", &n1, &n2);
  printf("Prime numbers between %d and %d are: ", n1, n2);
  for(i=n1+1; i<n2; ++i)
  {
      flag=0;
      for(j=2; j<=i/2; ++j)
      {
        if(i%j==0)
        {
          flag=1;
          break;
        }
      }
      if(flag==0)
        printf("%d ",i);
  }
  return 0;
}

To Find GCD of two number

Problem: To find GCD of two number.
Solution:

#include <iostream>

using namespace std;
int gcd_iter(int u, int v) 
{
  int t;
  while (v) 
  {
    t = u; 
    u = v; 
    v = t % v;
  }
  return u < 0 ? -u : u; 
}
int main()
{
  int n=3,m=6;
  int result=gcd_iter(n,m);
  cout<<result;
   return 0;
}

AMCAT Programming Pattern to print 2*n number of rows for input n

For input N print 2*N number of rows in following Pattern
3
44
555
6666
555
44
3



The Solution is:


#include <iostream>

using namespace std;

int main()
{
   int n=4,num=n-1;
  for(int i=1;i<=n;i++)
  {
      for(int j=1;j<=i;j++)
      cout<<num;
      num++;
      cout<<endl;
  }
  num--;
   for(int i=n;i>=1;i--)
  {
      for(int j=1;j<=i;j++)
      cout<<num;
      num--;
      cout<<endl;
  }
 
 
   return 0;

}

AMCAT PATTERN {1,3*2,4*5*6,10*9*8*7}

The most common pattern is
for n=5
we have output
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15


The Solution is:

#include <iostream>

using namespace std;

int main()
{
   int n=5,num;

   num=1;
   int l=1;
   int k=num;
   for(int i=1;i<=n;i++)
   {
     k=num-1;
     
       for(int j=1;j<=num;j++)
       {
       
           if(j%2==0)
           cout<<"*";
           else
           {
               if(i%2==0)
               {
             
               cout<<k+l-num+i;
               l++;
               k=k-2;
               }
             
              else
               cout<<l++;
           }
       }
       num=num+2;//the loop is going on as a prime number 1,3,5,7
       cout<<"\n";
   }
   return 0;
}

AMCAT Programming pattern {1112,3222,3334}

To print the pattern like
 for n=3
the program should print
1 1 1 2
3 2 2 2
3 3 3 4


The solution in c++:


#include <iostream>

using namespace std;

int main()
{
   int n=3,c=n-1;
   for(int i=1;i<=n;i++)
   {
       if(i%2==0)
       cout<<c++;
       for(int j=1;j<=n;j++)
       {
           cout<<i;
       }
       if(i%2!=0)
       cout<<c++;
       cout<<"\n";
   }
 
 
   return 0;
}

Amcat Trapezium pattern Solution

To print the trapezium pattern.
 for example , we have num=4
the output should be like



1*2*3*4*17*18*19*20
- -5*6*7*14*15*16
- - - -8*9*12*13
- - - - - -10*11


the solution in c++ is:

#include<iostream>
using namespace std;
int main(){
    int n=4,num=1,i=1,space=0,k=1,number=n;
    for(i=0;i<n;i++)
    {
        for(int j=1;j<=space;j++)
        {
         
            cout<<"-";
         
        }
        for(int m=1;m<2*n-space;m++)
        {
            if(m%2==0)
                cout<<"*";
            else
                cout<<num++;
        }
        cout<<"*";
         for(int l=1;l<2*n-space;l++)
        {
            if(l%2==0)
                cout<<"*";
            else
            {
                cout<<k+number*number;
        k++;
            }
        }
        number--;
     
     
        space=space+2;
        cout<<endl;
    }
    return 0;
}