Powered By Blogger

Wednesday, August 15, 2012

Programs For Placements

// Factorial using Recursion

#include<stdio.h>
#include<conio.h>
long int fact(long int);
void main()
{
 long int n,res;
 clrscr();
 printf("\n Enter the n value:");
 scanf("%ld",&n);
 res=fact(n);
 printf("\n The Result is %ld",res);
 getch();
}
long int fact(long int n)
{
 long int f=1,res;
 if(n==0 || n==1)
          return 1;
 else
         res=n*fact(n-1);
   return res;
}

// Factorial without Recursion

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,fact=1,i;
 clrscr();
 printf("\n Enter the n value:");
 scanf("%d",&n);
 if(n==1 || n==0)
 {
  n=1;
  printf("\n The result is : %d",n);
 }
 for(i=1;i<=n;i++)
                fact=fact*i;
 printf("The Result is =%d",fact);
getch();
}

// Program for Fibonacci series for a=0 and b=1 fixed values

#include<stdio.h>
#include<conio.h>
void main()
{
 int a=0,b=1,c,n,i;
 clrscr();
 printf("\n Enter the range of the series:");
 scanf("%d",&n);
 printf("\n The Series is %d \t %d",a,b);
 for(i=2;i<n;i++)
 {
  c=a+b;
  a=b;
  b=c;
  printf("\t %d \t",c);
 }
 getch();
}

// Program for Fibonacci Series asking a and b values from user

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c,n,i;
 clrscr();
 read:printf("\n Enter the values of a and b:");
 scanf("%d %d",&a,&b);
 if(a==0 && b==0)
 goto read;
 printf("\n Enter the range:");
 scanf("%d",&n);
 printf("The Series is %d\t%d",a,b);
 for(i=2;i<n;i++)
 {
  c=a+b;
  a=b;
  b=c;
  printf("\t%d",c);
 }
getch();
}

// Prime numbers from 2 to N

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,i,j,flag=0;
 clrscr();
 printf("\n Enter the range:");
 scanf("%d",&n);
 for(i=2;i<=n;i++)
 {
  for(j=2;j<=i;j++)
  {
   if(i%j==0)
    flag++;
  }
  if(flag==1)
  printf("\n The Prime nubers are : %d",i);
  flag=0;
 }
 getch();
}

// List of Prime Numbers Between Two Integer values.

#include<stdio.h>
#include<conio.h>
void main()
{
 int m,n,i,j,flag=0;
 clrscr();
 read: printf("\n Enter the range between M and N:");
 scanf("%d %d",&m,&n);

 if(m>n)
 goto read;
 for(i=m;i<=n;i++)
 {
  for(j=2;j<=i;j++)
  {
   if(i%j==0)
    flag++;
  }
  if(flag==1)
  printf("\n The Prime nubers are : %d",i);
  flag=0;
 }
 getch();
}

//Sum of the individual digits. [Eg: 145 – 1+4+5=10]

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,rem,i,sum=0;
 clrscr();
 printf("\n Enter the digit:");
 scanf("%d",&n);
 while(n!=0)
 {
  rem=n%10;
  n=n/10;
  sum=sum+rem;
  }
 printf("The Result is %d",sum);
 getch();
}

// swapping of 2 numbers with temporary (Call by Reference)

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);

void main()
{
 int a,b,c;
 clrscr();
 printf("\n Enter the values of a and b:");
 scanf("%d %d",&a,&b);
 printf("\n \n Before swapping the values are %d \t %d \n",a,b);
 swap(&a,&b);
 printf("\n The After swapping the values are %d \t %d \n",a,b);
 getch();
}
void swap(int *x,int *y)
{
 int t;
 t=*x;
 *x=*y;
 *y=t;
}

//Swapping without temporary variable

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b;
 clrscr();
 printf("\n Enter the values of a and b:");
 scanf("%d %d",&a,&b);
 printf("\n Before swapping %d and %d\n",a,b);
 a=a+b;
 b=a-b;
 a=a-b;
 printf("\n After swapping %d and %d",a,b);
 getch();
}

// Reversing a Digit Eg: if a = 321 the result should be 123

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int num,i,count=0,res,sum=0,mo;
 clrscr();
 printf("\n Enter the number:");
 scanf("%d",&num);
 res=num;
 while(num!=0)
 {
 num=num/10;
 count++;
 }
  for(i=count-1;res!=0;i--)
  {
  mo=res%10;
  sum = sum+(mo*pow(10,i));
  res=res/10;
  }
  printf("Final Result = %d \n \t",sum);
  getch();
 }

// Reversing the digit in another way but it is not the correct one so max avoid this program

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b;
 printf("Enter the number to be reversed:");
 scanf("%d",&a);
 if(a<0)
  a=-a;
 printf("Reverse number is");
 do
 {
  b=a%10;
  printf("%d\n \t",b);
  a=a/10;
  } while(a>0);
  getch();
 }

More Programs Yet to Come

Tuesday, May 8, 2012

Unix Commands...

  • Clear - Used to clear the screen
  • ls- Used to list all the files and folders
  • mkdir- to create a new directory (folder)
  • cat><File name> - used to create a new file
  • cat <File name>- To view file contents
  • cd <Folder name>- to Enter into the directory(folder) created
  • cd <Space Bar Key> - To come out of the directory (folder)
  • rmdir- To remove the directory(folder)
  • who- tell who all are connected to the server
  • date- displays the date and time (of the system)
  • cal- displays the present year calendar of all the months
  • cal <month(in number)> <Year(in number)>- displays the specified month and year
  • pwd- This command is provided to know the current working directory.
  • cp <Old File> <New File> - To copy the contents of old file to a new file
  • mv <Old File> <New File>- to move the data of old file into new file
  • wc <File Name>- To count the number of words lines and characters in a given file
  • bc- Calculator  to come out of it Press CTRL+d
  • who am i- Identify the user and lists the user name, Terminal line, the date and time of login.
  • Semi column(;) - to run multiple commands at a time.
  • vi editor- visual editor used to write C Programs.
  • sort <File Name>- it will sort the file according to the alphabetic order.
  • rm <File Name>- To delete the file which is already created.

How to run a C file in UNIX???

First open a file using VI editor (vi <File name.c>) after entering into it then press the key 'i' so that we can insert the data into the file which we created. after writing the program press escape key and the ':wq' to save and quit the program. After that use the command 'gcc <File Name.c>' to compile u r C program. If no errors then just give './a.out' to see the output.

 Sample Program for addition of 2 numbers:
$vi add.c
press 'i' key to type the program.
#include<stdio.h>
int main()
{
 int a,b,c;
 printf("\n Enter the values of A and B:");
 scanf("%d %d",&a,&b);
 c=a+b;
 printf("The Result is %d",c);
 return c;
}
 press the 'escape key' and ':wq'
after that compile using gcc or cc.
$gcc add.c
To view the output.
$./a.out  
finally u r result will be displayed if u don't have any errors.

Friday, April 27, 2012

Program : 19


PROGRAM - 19
Write a program to implement stack operations using:
  •  Arrays.
  • Pointers.
Program to implement stack operations using arrays:

#include<stdio.h>
#include<conio.h>
#define max 5
static stack(max);
int top;
void push(int t)
{
   if(top>=max)
   {
     printf("stack is full");
     return;
   }
   stack[top++]=t;
}
int pop()
{
   top--;
   if(top<0)
   {
   printf("stack is empty");
   }
   return(stack[top]);
}
main()
{
 int condition,element,ret;
 clrscr();
 while(1)
 {
      printf("enter-1:push\n");
      printf("enter-2:pop\n");
      printf("enter-3:exit\n");
      scanf("%d",&condition);
      switch(condition)
      {
      case 1:printf("enter the element to push");
          scanf("%d",&elements);
          push(element);
          break;
      case 2:ret=pop();
          printf("popped element is %d",ret);
          break;
      case 3:return 0;
      default:printf("\n choose & give correct option");
      }
 }
 getch();
 return();
}


Program to implement stack operations using pointers:

#include<stdio.h>
#include<conio.h>
struct stack
{
       int data;
       struct stack *next;
};
typedef struct stack node;
node *start=NULL;
node *top=NULL;
node *getnode();
int menu();
void push(node*);
void pop();
void display();
void main()
{
      node *newnode;
      int ch;
      clrscr();
      do
      {
        ch=menu();
        switch(ch)
        {
           case 1:{
                  newnode=getnode();
                  push(newnode);
                  }
                  break;
           case 2:pop();
                  break;
           case 3:display();
                  break;
           case 4:exit(1);
           default:printf("invalid choice");
        }
      }while(1);
}
int menu()
{
     int ch;
     printf("menu\n");
     printf("1.push\n");
     printf("2.pop\n");
     printf("3.display\n");
     printf("4.exit\n");
     printf("enter your choice");
     scanf("%d",&ch);
     return ch;
}
node *getnode()
{
     node *newnode;
     newnode=(node*)malloc(sizeof(node));
     printf("enter the data");
     scanf("%d",&newnode->data);
     newnode->next=NULL;
     return(newnode);
}
void push(node *newnode)
{
     node *temp;
     if(newnode==NULL)
     {
       printf("overflow");
     }
     else
     {
       if(start==NULL)
       {
          start=newnode;
          top=newnode;
       }
       else
       {
           temp=start;
           while(temp->next!=NULL)
           {
             temp=temp->next;
           }
           temp->next=newnode;
           top=newnode;
       }
     }
}
void pop()
{
     node *temp;
     if(start==NULL)
     {
      printf("underflow");
     }
     else
     {
     if(start->next==NULL)
     {
           printf("popped element is %d",top->data);
           start=NULL;
           free(top);
           top=NULL;
     }
     else
     {
           temp=start;
           while(temp->next!=top)
           {
                  temp=temp->next;
           }
           temp->next=NULL;
           printf("popped element is %d",top->data);
           free(top);
           top=temp;
     }
     }
}
void display()
{
     node *temp;
     temp=start;
     while(temp!=NULL)
     {
          printf("%d",temp->data);
          temp=temp->next;
     }
}