Tuesday, October 16, 2012

Program To check a Number is Pallindrome & Armstrong


Some Basic C program frequently asked in Interview.

/*To check a number is pallindrome or not */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
c=a;
//the number is reversed inside the while loop.
while(a>0)
{
b=a%10;
s=(s*10)+b;
a=a/10;
}
//Here the reversed number is compared with the given number.
if(s==c)
{
printf("The no. %d is a pallindrome",c);
}
else
printf("The no. %d is not a pallindrome",c);
getch();
}




/*To check a number is armstrong or not */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
c=a;
//Sum of cube of each digit of given  number is claculated inside the while loop.
while(a>0)
{
b=a%10;
s=s+b*b*b;
a=a/10;
}
//Sum is compared with the given number.
if(s==c)
{
printf("The no. %d is a armstrong no.",c);
}
else
printf("The no. %d is not a armstrong no.",c);
getch();
}

C program to replace multiple spaces with a single space


Program to replace multiple spaces between two words in a string by a single space


#include<stdio.h>

#include<conio.h>

void main()

{
char s[50];
char *str=s;
int i,j;
clrscr();
printf("Enter the String:\n");
gets(s);
for(i=0;*(str+i)!='\0';i++)
   {
   if(*(str+i)==' '&*(str+i+1)==' ')
   {

   for(j=i;*(str+j)!='\0';j++)
     {
 *(str+j)=*(str+j+1);
 }
 i--;
   }

   }
     printf("\n%s",str);
   getch();
}

Count number of words in a string


Program to count number of words in a string enter by a user 


#include<stdio.h>

#include<conio.h>

#include<string.h>
void askstring()
{
char str[50];
int i,words=0;
printf("\nEnter a string\n");
scanf("%[^\n]c",str);
//printf("\nThe String is: %s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
words++;

}
printf("\n Number of words in the stirng :%d",words+1);
}
void main()
{
clrscr();
askstring();
getch();
}

Frequently asked C programs

Some Basics C program frequently asked in interviews

1.Find factorial of a number using recursive function.


#include<stdio.h>

#include<conio.h>

int fact(int);
void main()
{
int a,b;
printf("Enter no.");
scanf("%d",&a);
clrscr();
b=fact(a);
printf("%d",b);
getch();
}
int fact(int x)
{
int r=1;
if(x==1) return 1;
else r=x*fact(x-1);
return r;
}

2. Getting a multiple word string having spaces from user using scanf function.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
printf("Enter name:\n");
scanf("%[^\n]s",&name);              /* notice the format specifier used here i.e 
%[^\n]s

*/
printf("%s",name);                          
getch();
}

Binary Search Tree


Binary Search Tree is a binary tree where left child of a node is less than the key of the Parent node and the key in the right child is greater than the parent node.We can design many kind of BST(Binary Search Tree) for some nodes or values.And there average searching time may be different.In Binary Search tree searching is easy. Time complexity of searching in case BST is nlogn.


Binary Tree
Traversal of a Binary TreeThe traversal of binary tree invloves visiting each node in the tree exactly once. Binary tree traversal is usefull in many applications.There are 3 popular method of Binary tree traversal.These methods are:-
1) Inorder Traversal.(Left,Root,Right)
2) Preorder Traversal.(Root,Left,Right)
3) Postorder Traversal.(Left,Right,Root)

 For the above tree:   Inorder Traversal is    5 , 10 , 12 , 20 , 30.
                                    Preorder Traversal is   20 , 10 , 5 , 12 , 30.           
                                    Postorder Traversal is   5 , 12 , 10 , 30 , 20.





Stack implementation using C++

stack example

First of all,what is stack? A stack is a data structure in which addition of new element or deletion of existing element always takes place at the same end. The end is often known as Top of Stack. For example a no. of books kept one above another, if we have to keep a new book there, we will kept it above all the books and suppose we have to take out a book at first we will pick the book which is at the top.



Insertion Sort algorithm in C++


Insertion Sort is a sorting algorithm used to sort an array element. From the name of the algorithm itself we can make out that here sorting is done with the help of insertion. Now suppose we are provided with a list of number e.g    18  16  14  17  8 . We have to arrange this number in a sorted order. In Insertion sort what it does that it will  pick the number present at the beginning of the given array say A, and place it in another array say B. And  then next number of the array A is picked up as a Key. Now it will compare  with the number present in array B if it is larger than that number then it will inserted in array B after the smaller number present in the array or if it is smaller than the existing number then it will be placed before the existing element of B. Similarly now the 3rd element is taken up as a Key and same comparison is done again. For rest of the element of array A same comparison is done.Have a look at this example.