Subscribe

RSS Feed (xml)

Thursday, November 26, 2009

Write a C program to print the given alphabet pyramid -2


A B C D E F G F E D C B A
  A B C D E F E D C B A
    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 


#include<stdio.h>
#incldue<conio.h>
int main( )
{
  int r,c,askey,sp;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    for(sp=6; sp>=r; sp--)
        printf("  "); //2 spaces

    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    
    for(c=r-1; c>=1; c-- )
       printf("%2c", --askey);
   
   printf("\n");
  }
  getch();
  return 0;
}

Output:-
A B C D E F G F E D C B A
  A B C D E F E D C B A
    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
 

Write a C program to print the given alphabet pyramid.

A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
-----------------------------------

#include<stdio.h>
#include<conio.h>
int main( )
{
  int r,c,askey;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    askey--; 
    for(c=r; c>=1; c-- )
       printf("%2c", askey--);
   
   printf("\n");
  }
  getch();
  return 0;
}
 
Output:- 
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

Note:- 
'r' and 'c' means rows and columns .
'askey' variable is for disp. values.

Thursday, November 12, 2009

Write a C program to find the roots of a quadratic equation using Pointers and Functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
void quadratic_roots(double *,double *, double *);
int main(void)
{
double a, b, c;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
quadratic_roots(&a,&b,&c);
getch( );
return 0;
}

void quadratic_roots(double *aa, double *bb, double *cc)
{
double root1, root2;
if((*bb)*(*bb)= = 4*(*aa)*(*cc))
{
root1= -(*bb)/2*(*aa);
printf("\nRoots are equal");
printf("\nvalue : %lf", root1);

else if((*bb)*(*bb) > 4*(*aa)*(*cc))
{
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
{
printf("\nImaginary roots");
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("roots are %f+i(%f) , %f-i(%f)",r1, r2, r1, r2);
}
}

Wednesday, November 11, 2009

Write a C function that defines two strings as parameters, as follows, and checks whether the second string (str2) is a substring of the first one (str1).


int substring_finder(char* str1, char* str2);
The function should return an integer as follows:
2 if str2 is a substring of str1 and also of same length
o e.g. str1 = "Hello" and str2= "Hello"
1 if str2 is a substring of str1 and of different lengths
o e.g. str1 = "Hello World" and str2= "Hello"
0 if str2 is not a substring of str1
o e.g. str1 = "Hello" and str2= "Helo"



#include(string.h) //  note place '<' & '>' in place of '(' & ')'
#include(stdio.h)
int substring_finder(char *,char *);
int main()
{
  char *str1,*str2;
  int fl;
  clrscr();
  printf("Enter the main string:");
  gets(str1);
  printf("Enter the sub-string:");
  gets(str2);
  fl=substring_finder(str1,str2);

  if(fl==0)
    printf("\n'str2' is not a substring of 'str1'");
  else if(fl==1)
    printf("\n'str2' is a substring of 'str1'");
  else if(fl==2)
    printf("\nBoth are equal");
 

getch();
return 0;
}
int substring_finder(char *str,char *sub)
{
  int flag;
  if(strcmp(str,sub)==0)
     flag=2;
  else if(strstr(str,sub))
     flag=1;
  else
     flag=0;

  return flag;
}



Output:- Enter the main string: Hello world
Enter the sub string: world
'str2' is a substring of 'str1'

Write a C program to merge two unsorted one dimensional arrays in descending order

Write a program to merge two unsorted 1D arrays in descending order
Note: Define the sizes of each array as a constant i.e. your program should run for arrays of any size.



#include(stdio.h)  // note '<' & '>' in place of '(' & ')'
            // size of the array is 20
#define MAX 20
int main()
{
 int f[MAX],s[MAX];
 int m,n; // to read the size of two arrays
 int i,j,temp;
 clrscr();
 printf("Enter the 1st array size(1-20) :");
 scanf("%d",&m);
 printf("Enter the 2nd array size(1-20) :");
 scanf("%d",&n);
 printf("\nEnter the 1st array elements:");
 for(i=0; i< m; i++)
   scanf("%d",&f[i] );


 printf("\nEnter the 2nd array elements:");
 for(j=0; j< n; j++)
   scanf("%d", &s[j] );


 for(j=0; j< n; j++)  // to store 's' elements to 'f'
 {
    f[i]=s[ j];
    i++;
 }
 printf("\nBefore descending, the merged array is:\n");
 for(i=0; i< m+n; i++)
    printf("%5d",f[i] );


 for(i=0; i< m+n; i++)// to arrange the 'f' elements in Descending oder
 {
    for(j=i; j< (m+n)-1; j++)
    {
       if(f[i]< f[ j+1])
       {
     temp=f[i];
     f[i]=f[ j+1];
     f[ j+1]=temp;
       }
    }
 }
 printf("\nAfter descending, the merged array is:\n");
  for(i=0; i< m+n; i++)
    printf("%5d",f[i] );
getch();
 return 0;
}


Output:-


Enter the 1st array size(1-20) :4
Enter the 2nd array size(1-20) :5


Enter the 1st array elements:12
3
45
12


Enter the 2nd array elements:78
5
43
1
23


Before descending, the merged array is:
   12    3   45   12   78    5   43    1   23
After descending, the merged array is:
   78   45   43   23   12   12    5    3    1

Tuesday, November 10, 2009

Write a C program to print the Given pyramid

     1
    1 2 
   1 2 3  
  1 2 3 4
 1 2 3 4 5
  .
  .
  .
  n


#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
int r,c,n,sp;
clrscr( );
printf("Enter the no. of rows u want to print:");
scanf("%d", &n);
for(r=1; r<=n; r++)
{
for(sp=n; sp>=r; sp--)
printf(" "); // give one space in betw quotations.
for(c=1; c<=r; c++)
printf("%2d", c);
printf("\n");
}
getch( );
return 0;
}