#Post Title #Post Title #Post Title
Wednesday, 23 September 2015

Patterns (one of their favorites)

 Patterns (one of their favorites)

If the interviewer asks you a pattern and you don’t know how to do that you are screwed big time. Often you might make a very simple mistake which the interviewer was actually looking for. Here we’ll find out how to print this pattern
?
1
2
3
4
5
A B C D E D C B A
  A B C D C B A
    A B C B A
      A B A
        A
And here is the code.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
//Printing pattern
#include
#include
int main(){
 int n,i,j;
 printf("Enter no of lines: ");
 scanf("%d",&n);
 for(i=0;i<n;i++){
 for(j=0;j<i;j++){ //for printing spaces
 printf(" ");
 }
 for(j=0;j<n-i;j++){ //for printing the left side
  printf("%c ",'A'+j); //the value of j is added to 'A'(ascii value=65)
  }
  for(j=n-i-2;j>=0;j--){ //for printing the right side
    printf("%c ",'A'+j);
  }
 printf("\n");
 }
 getch();
}
[ Read More ]
Tuesday, 15 September 2015

easiest way to sort

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

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main()
{
   int n;

   printf("Before sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) 
   {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\nAfter sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) 
   {   
      printf("%d ", values[n]);
   }
  
   return(0);
}
 
 
 
 
output
 
Before sorting the list is: 
88 56 100 2 25 
After sorting the list is: 
2 25 56 88 100 
[ Read More ]
Friday, 11 September 2015

number of ways a particular no can formed..

#include<stdio.h>


// Returns the count of ways we can sum S[0...m-1] coins to get sum n

int count( int S[], int m, int n )
{
    // If n is 0 then there is 1 solution (do not include any coin)
    if (n == 0)
        return 1;
   
    // If n is less than 0 then no solution exists
    if (n < 0)
        return 0;

    // If there are no coins and n is greater than 0, then no solution exist
    if (m <=0 && n >= 1)
        return 0;

    // count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
    return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}

// Driver program to test above function
int main()
{
    int i, j;
    int arr[] = {1, 3, 5};
    int m = sizeof(arr)/sizeof(arr[0]);
    printf("%d ", count(arr, m, 5));
    getchar();
    return 0;
}



It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for S = {1, 2, 3} and n = 5.
The function C({1}, 3) is called two times. If we draw the complete tree, then we can see that there are many subproblems being called more than once.
C() --> count()
                              C({1,2,3}, 5)                     
                           /                \
                         /                   \              
             C({1,2,3}, 2)                 C({1,2}, 5)
            /     \                        /         \
           /        \                     /           \
C({1,2,3}, -1)  C({1,2}, 2)        C({1,2}, 3)    C({1}, 5)
               /     \            /    \            /     \
             /        \          /      \          /       \
    C({1,2},0)  C({1},2)   C({1,2},1) C({1},3)    C({1}, 4)  C({}, 5)
                   / \      / \       / \        /     \    
                  /   \    /   \     /   \      /       \ 
                .      .  .     .   .     .   C({1}, 3) C({}, 4)
                                               /  \
                                              /    \  
[ Read More ]
Monday, 31 August 2015

C Program to delete a file.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 int status;
 char name[25];
 clrscr();
 printf("Enter the name of file you want to delete\n");
 gets(name);
 status=remove(name);
 if(status==0)
 {
   printf("%s file deleted successfully.\n",name);
 } 
 else
 {
   printf("Unable to delete the file\n");
 }
 getch();
}

Output
[ Read More ]

C Program to merge 2 files

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fs1,*fs2,*ft;
 char ch,file1[20],file2[20],file3[20];
 clrscr();
 printf("Enter name of first file\n");
 gets(file1);
 printf("Enter name of second file\n");
 gets(file2);
 printf("Enter name of file which'll store contents:\n");
 gets(file3);
 fs1=fopen(file1,"r");
 fs2 = fopen(file2,"r");
 if(fs1==NULL || fs2==NULL )
 {
  printf("Error opening file\n");
  getch();
  exit(1);
 }
 ft=fopen(file3,"w");
 if(ft==NULL )
 {
  printf("Error opening file\n");
  exit(1);
 }
 while((ch=fgetc(fs1))!=EOF)
  fputc(ch,ft);
 while((ch=fgetc(fs2))!=EOF)
  fputc(ch,ft);
 printf("Two files were merged into %s file \n",file3);
 fclose(fs1);
 fclose(fs2);
 fclose(ft);
 getch();
}

Output
[ Read More ]

C Program to count characters, lines, spaces & tabs in a file

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fp;
 char a[20];
 int nol=0,not=0,nob=0,noc=0;
 char c;
 clrscr();
 printf("Enter the name of File:\n");
 gets(a);
 if((fp=fopen(a,"r"))==NULL)
 {
  printf("File dosen't exist.");
 }
 else
 {
  while(1)
  {
   c=fgetc(fp);
   if(c==EOF)
      break;
   noc++;
   if(c==' ')
   nob++;
   if(c=='\n')
   nol++;
   if(c=='\t')
   not++;
  }
 }
 fclose(fp);
 printf("Number of characters = %d\n",noc);
 printf("Number of blanks = %d\n",nob);
 printf("Number of tabs = %d\n",not);
 printf("Number of lines = %d\n",nol);
 getch();
}

Output
[ Read More ]
Saturday, 29 August 2015

How to find size of integer data type without using sizeof operator in c programming language


How to find size of integer data type without using sizeof operator in c programming language  




#include<stdio.h>
int main(){
  int *ptr = 0;
  ptr++;
  printf("Size of int data type:  %d",ptr);
  return 0;
}
[ Read More ]
 
 

Labels