Saturday 28 November 2015

Write C Program to print pattern14

1:  //*****  
2:  // ****   
3:  // ***   
4:  //  **   
5:  //  *  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:   int i, j, k, row;  
11:   clrscr();  
12:   printf("Enter the number of rows you want:\n");  
13:   scanf("%d",&row);  
14:   for(i=row;i>=1;i--)  
15:   {  
16:   for(j=row;j>i;j--)  
17:   {  
18:    printf(" ");  
19:   }  
20:   for(k=1;k<=i;k++)  
21:   {  
22:    printf("*");  
23:   }  
24:   printf("\n");  
25:   }  
26:   getch();  
27:  }  

Write a C Program to print pattern13


1:  //A  
2:  //BA  
3:  //CBA  
4:  //DCBA  
5:  //EDCBA  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:    int i,j,n;  
11:    clrscr();  
12:    printf("Enter the number of rows you want:\n");  
13:    scanf("%d",&n);  
14:    for(i=1;i<=n;i++)  
15:    {  
16:      for(j=i;j>=1;j--)  
17:      {  
18:        printf("%c",'A' + j-1);  
19:      }  
20:      printf("\n");  
21:    }  
22:    getch();  
23:  }  

Write a C Program to print pattern12


1:  //5  
2:  //4 4  
3:  //3 3 3  
4:  //2 2 2 2  
5:  //1 1 1 1 1  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:    int i,j,row;  
11:    clrscr();  
12:    printf("Enter the number of rows you want:\n");  
13:    scanf("%d",&row);  
14:    for(i=row;i>=1;i--)  
15:    {  
16:       for(j=row;j>=i;j--)  
17:       {  
18:         printf("%d ",i);  
19:       }  
20:       printf("\n");  
21:    }  
22:    getch();  
23:  }  

Write a C Program to print pattern11


1:  // Write a to print following pattern  
2:  //*****  
3:  //****  
4:  //***  
5:  //**  
6:  //*  
7:  #include<stdio.h>  
8:  #include<conio.h>  
9:  void main()  
10:  {  
11:   int row,i,j;  
12:   clrscr();  
13:   printf("Enter the number of rows you want: \n");  
14:   scanf("%d",&row);  
15:   for(i=row;i>=1;i--)  
16:   {  
17:   for(j=1;j<=i;j++)  
18:   {  
19:    printf("* ");  
20:   }  
21:   printf("\n");  
22:   }  
23:  getch();  
24:  }  

Write a C Program To print pattern ABCDE descending order

1:  //E  
2:  //DE  
3:  //CDE  
4:  //BCDE  
5:  //ABCDE  
6:  #include <stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:    int i,j,n;  
11:    clrscr();  
12:    printf("Enter the number of rows you want:\n");  
13:    scanf("%d",&n);  
14:    for(i=n;i>=1;i--)  
15:    {  
16:      for(j=i;j<=n;j++)  
17:      {  
18:        printf("%c",'A' + j-1);  
19:      }  
20:      printf("\n");  
21:    }  
22:    getch();  
23:  }  

Write a Cprogram to print 1,22,333,4444,55555 pattern


1:  //1  
2:  //2 2  
3:  //3 3 3  
4:  //4 4 4 4  
5:  //5 5 5 5 5  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:    int i,j,row;  
11:    clrscr();  
12:    printf("Enter the number of rows you want:\n");  
13:    scanf("%d",&row);  
14:    for(i=1;i<=row;i++)  
15:    {  
16:       for(j=1;j<=i;j++)  
17:       {  
18:         printf("%d ",i);  
19:       }  
20:       printf("\n");  
21:    }  
22:   getch();  
23:  }  

Write a C Program to print Triangle pattern2

1:  //  *  
2:  //  **  
3:  // ***  
4:  // ****  
5:  //*****  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:   int i, j, k, row;  
11:   clrscr();  
12:   printf("Enter the number of rows you want:\n");  
13:   scanf("%d",&row);  
14:   for(i=row;i>=1;i--)  
15:   {  
16:   for(j=1;j<i;j++)  
17:   {  
18:    printf(" ");  
19:   }  
20:   for(k=row;k>=i;k--)  
21:   {  
22:    printf("*");  
23:   }  
24:   printf("\n");  
25:   }  
26:   getch();  
27:  }  

Write a C Program to print Floyds Triangle patten

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void main()  
4:  {  
5:   int row,i,j,c=1;  
6:   clrscr();  
7:   printf("Enter the number of rows you want: \n");  
8:   scanf("%d",&row);  
9:   for(i=1;i<=row;i++)  
10:   {  
11:   for(j=1;j<=i;j++)  
12:   {  
13:    printf("%3d ",c);  
14:    c++;  
15:   }  
16:   printf("\n");  
17:   }  
18:   getch();  
19:  }  

Write a C Program to print A,AB,ABC,ABCD,ABCDE pattern

1:  //A  
2:  //AB  
3:  //ABC  
4:  //ABCD  
5:  //ABCDE  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:    int i,j,n;  
11:    clrscr();  
12:    printf("Enter the number of rows you want:\n");  
13:    scanf("%d",&n);  
14:    for(i=1;i<=n;i++)  
15:    {  
16:      for(j=1;j<=i;j++)  
17:      {  
18:        printf("%c",'A' + j-1);  
19:      }  
20:      printf("\n");  
21:    }  
22:    getch();  
23:  }  

Write s C Program to print patter program1

1:  //*  
2:  //**  
3:  //***  
4:  //****  
5:  //*****  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:   int row,i,j;  
11:   clrscr();  
12:   printf("Enter the number of rows you want: \n");  
13:   scanf("%d",&row);  
14:   for(i=1;i<=row;i++)  
15:   {  
16:   for(j=1;j<=i;j++)  
17:   {  
18:    printf("* ");  
19:   }  
20:   printf("\n");  
21:   }  
22:   getch();  
23:  }  

Friday 9 October 2015

Write a C Program To Print Pattern4

1:  //*****  
2:  // ****   
3:  // ***   
4:  //  **   
5:  //  *  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:   int i, j, k, row;  
11:   clrscr();  
12:   printf("Enter the number of rows you want:\n");  
13:   scanf("%d",&row);  
14:   for(i=row;i>=1;i--)  
15:   {  
16:   for(j=row;j>i;j--)  
17:   {  
18:    printf(" ");  
19:   }  
20:   for(k=1;k<=i;k++)  
21:   {  
22:    printf("*");  
23:   }  
24:   printf("\n");  
25:   }  
26:   getch();  
27:  }  

Write a C Program To Print Following Pattern

1:  // WAP to print following pattern  
2:  //*****  
3:  //****  
4:  //***  
5:  //**  
6:  //*  
7:  #include<stdio.h>  
8:  #include<conio.h>  
9:  void main()  
10:  {  
11:   int row,i,j;  
12:   clrscr();  
13:   printf("Enter the number of rows you want: \n");  
14:   scanf("%d",&row);  
15:   for(i=row;i>=1;i--)  
16:   {  
17:   for(j=1;j<=i;j++)  
18:   {  
19:    printf("* ");  
20:   }  
21:   printf("\n");  
22:   }  
23:  getch();  
24:  }  

Write a C Program To Print Pattern

1:  //  *  
2:  //  **  
3:  // ***  
4:  // ****  
5:  //*****  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:   int i, j, k, row;  
11:   clrscr();  
12:   printf("Enter the number of rows you want:\n");  
13:   scanf("%d",&row);  
14:   for(i=row;i>=1;i--)  
15:   {  
16:   for(j=1;j<i;j++)  
17:   {  
18:    printf(" ");  
19:   }  
20:   for(k=row;k>=i;k--)  
21:   {  
22:    printf("*");  
23:   }  
24:   printf("\n");  
25:   }  
26:   getch();  
27:  }  

Write a C Program To Print Triangle Pattern


Output
1:  //*  
2:  //**  
3:  //***  
4:  //****  
5:  //*****  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  void main()  
9:  {  
10:   int row,i,j;  
11:   clrscr();  
12:   printf("Enter the number of rows you want: \n");  
13:   scanf("%d",&row);  
14:   for(i=1;i<=row;i++)  
15:   {  
16:   for(j=1;j<=i;j++)  
17:   {  
18:    printf("* ");  
19:   }  
20:   printf("\n");  
21:   }  
22:   getch();  
23:  }  

Wednesday 7 October 2015

Write a CPP Program To Perform Contructor And Destructor

Write a CPP Program To Perform Contructor And Destructor
1:  #include<iostream.h>  
2:  #include<stdio.h>  
3:  class sum  
4:  {  
5:    int a,b;  
6:  public:  
7:    sum()  
8:    {  
9:      cout<< "Enter 2 numbers" << endl;  
10:      cin>>a>>b;  
11:      cout<< "Sum = "<<a+b;  
12:    }  
13:    ~sum()  
14:    {  
15:      cout<< "\nDestructor invoked"<<endl;  
16:    }  
17:  };  
18:  int main()  
19:  {  
20:    sum t;  
21:  }  

Write a CPP Program To Check Number is Armstrong or Not

Write a CPP Program To Check Number is Armstrong or Not
1:  #include<iostream.h>  
2:  #include<stdio.h>  
3:  int main()  
4:  {  
5:    int num,rem,c,result=0;  
6:    cout<< "Enter number" <<endl;  
7:    cin>>num;  
8:    c=num;  
9:    do  
10:    {  
11:      rem=num%10;  
12:      result=result+(rem*rem*rem);  
13:      num=num/10;  
14:    }  
15:    while(num>0);  
16:    if(result==c)  
17:      cout<< "Armstrong"<<endl;  
18:    else  
19:      cout<< "Not armstrong"<<endl;  
20:  }  

Write a CPP Program To Swap Two Numbers

Write a CPP Program To Swap Two Numbers
1:  #include<iostream.h>  
2:  #include<stdio.h>  
3:  int main()  
4:  {  
5:    int a,b,c;  
6:    cout << "Enter 2 numbers" << endl;  
7:    cin>>a>>b;  
8:    cout<< "Initially"<<endl;  
9:    cout<< "a = "<<a<<endl;;  
10:    cout<< "b = "<<b<<endl;  
11:    c=a;  
12:    a=b;  
13:    b=c;  
14:    cout<< "Swapped"<<endl;  
15:    cout<< "a = "<<a<<endl;;  
16:    cout<< "b = "<<b<<endl;  
17:  }  

Write a CPP Program To Find Inputed Number is Positive Or Not

Write a CPP Program To Find Inputed Number is Positive Or Not
1:  #include<iostream.h>  
2:  #include<stdio.h>  
3:  int main()  
4:  {  
5:    int a;  
6:    cout << "Enter a number" << endl;  
7:    cin>>a;  
8:    if (a>=0)  
9:      cout << "Positive : " << a<<endl;  
10:    else  
11:      cout << "Negative : " << -a<<endl;  
12:  }  

Write a CPP Program To Find Remainder Of Input Number

Write a CPP Program To Find Remainder Of Input Number
1:  #include<iostream.h>  
2:  #include<stdio.h>  
3:  int main()  
4:  {  
5:    int n,rem;  
6:    cout<< "Enter number"<<endl;  
7:    cin>>n;  
8:    for(;n>0;n=n/10)  
9:    {  
10:      rem=n%10;  
11:      cout<< rem;  
12:    }  
13:  }  

Write a CPP Program To Find Factorial Of Input Number

Write a CPP Program To Find Factorial Of Input Number
1:  #include<iostream.h>  
2:  #include<stdio.h>  
3:  int main()  
4:  {  
5:    int n,fact=1;  
6:    cout<< "Enter number"<<endl;  
7:    cin>>n;  
8:    for(;n>0;n--)  
9:    {  
10:      fact=fact*n;  
11:    }  
12:    cout<< "Factorial = "<<fact<<endl;  
13:  }  

Tuesday 29 September 2015

Write a CPP Program To Print Hello World

1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main()  
5:  {  
6:    cout << "Hello World" << endl;  
7:       getch();  
8:  }  

Write a CPP Program To Find Greater Number From Inputed Two Numbers

1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main()  
5:  {  
6:    int a,b;  
7:    cout << "Enter 2 numbers" << endl;  
8:    cin>>a>>b;  
9:    if (a>b)  
10:      cout << "Greater is : " << a<<endl;  
11:    else  
12:      cout << "Greater is : " << b<<endl;  
13:       getch();  
14:  }  

Write a CPP Program To Perform Addition,Subtraction,Multiliation And Division using Float Data type

1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main()  
5:  {  
6:    float a,b;  
7:    cout << "Enter 2 numbers" << endl;  
8:    cin >> a>>b;  
9:    int sum=a+b;  
10:    int diff=a-b;  
11:    int mul=a*b;  
12:    float div=a/b;  
13:    cout << "Sum = " << sum <<endl;  
14:    cout << "Difference = " << diff <<endl;  
15:    cout << "Multiplication = " << mul <<endl;  
16:    cout << "Division = " << div <<endl;  
17:       getch();  
18:  }  

Write a C rogram To Perform Radix Sort in Data Structure

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void radix_sort(int arr[], int n);  
4:  void main()  
5:  {  
6:   int a[20],n,i;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   printf("\nArray elements before Radix Sort:\n");  
16:   for(i=0;i<n;i++)  
17:   {  
18:   printf("%d\t",a[i]);  
19:   }  
20:   radix_sort(a,n);  
21:   printf("\nArray elemants after Radix Sort:\n");  
22:   for(i=0;i<n;i++)  
23:   {  
24:   printf("%d\t",a[i]);  
25:   }  
26:   getch();  
27:  }  
28:  void radix_sort(int arr[],int n)  
29:  {  
30:   int bucket[10][5],buck[10],b[10];  
31:   int i,j,k,l,num,div,large,passes;  
32:   div=1;  
33:   num=0;  
34:   large=arr[0];  
35:   for(i=0;i<n;i++)  
36:   {  
37:   if(arr[i]>large)  
38:   {  
39:    large=arr[i];  
40:   }  
41:   while(large>0)  
42:   {  
43:    num++;  
44:    large=large/10;  
45:   }  
46:   for(passes=0;passes<num;passes++)  
47:   {  
48:    for(k=0;k<10;k++)  
49:    {  
50:    buck[k]=0;  
51:    }  
52:    for(i=0;i<n;i++)  
53:    {  
54:    l=((arr[i]/div)%10);  
55:    bucket[l][buck[l]++]=arr[i];  
56:    }  
57:    i=0;  
58:    for(k=0;k<10;k++)  
59:    {  
60:    for(j=0;j<buck[k];j++)  
61:    {  
62:     arr[i++]=bucket[k][j];  
63:    }  
64:    }  
65:    div*=10;  
66:   }  
67:   }  
68:  }  

Write a C Program To Perform Shell Sort

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void main()  
4:  {  
5:   int a[20];  
6:   int i,j,k,temp,n;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   for(i=n/2;i>0;i=i/2)  
16:   {  
17:   for(j=i;j<n;j++)  
18:   {  
19:    for(k=j-i;k>=0;k=k-i)  
20:    {  
21:    if(a[k+i]>=a[k])  
22:     break;  
23:    else  
24:    {  
25:     temp=a[k];  
26:     a[k]=a[k+i];  
27:     a[k+i]=temp;  
28:    }  
29:    }  
30:   }  
31:   }  
32:   printf("\nArray elements after shell sorting are:\n");  
33:   for(i=0;i<n;i++)  
34:   {  
35:   printf("%d\t",a[i]);  
36:   }  
37:   getch();  
38:  }  

Write a C Program To Perform Quick Sorting Technique

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void quicksort(int b[10],int first,int last);  
4:  void main()  
5:  {  
6:   int a[20],n,i;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   printf("\nArray elements before quick sort:\n");  
16:   for(i=0;i<n;i++)  
17:   {  
18:   printf("%d\t",a[i]);  
19:   }  
20:   quicksort(a,0,n-1);  
21:   printf("\n\nArray elements after quick sort:\n");  
22:   for(i=0;i<n;i++)  
23:   {  
24:   printf("%d\t",a[i]);  
25:   }  
26:   getch();  
27:  }  
28:  void quicksort(int b[10],int first,int last)  
29:  {  
30:   int pivot,j,temp,i;  
31:   if(first<last)  
32:   {  
33:   pivot=first;  
34:   i=first;  
35:   j=last;  
36:   while(i<j)  
37:   {  
38:    while(b[i]<=b[pivot] && i<last)  
39:    i++;  
40:    while(b[j]>b[pivot])  
41:    j--;  
42:    if(i<j)  
43:    {  
44:    temp=b[i];  
45:    b[i]=b[j];  
46:    b[j]=temp;  
47:    }  
48:   }  
49:   temp=b[pivot];  
50:   b[pivot]=b[j];  
51:   b[j]=temp;  
52:   quicksort(b,first,j-1);  
53:   quicksort(b,j+1,last);  
54:   }  
55:  }  

Write a C Program That performing Merge Sort(Sorting Techniues)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define MAX 20  
4:  void mergesort(int a[],int low,int high);  
5:  void merge(int a[],int beg,int mid,int end);  
6:  void display(int a[],int num);  
7:  void main()  
8:  {  
9:   int n,i,a[MAX];  
10:   clrscr();  
11:   printf("Enter the number of elements (<MAX )\n");  
12:   scanf("%d",&n);  
13:   printf("Enter the elements\n");  
14:   for(i=0;i<n;i++)  
15:   {  
16:   scanf("%d",&a[i]);  
17:   }  
18:   printf("\nArray elements before sorting are:\n");  
19:   display(a,n);  
20:   mergesort(a,0,n-1);  
21:   printf("\nArray elements after sorting are:\n");  
22:   display(a,n);  
23:   getch();  
24:  }  
25:  void mergesort(int a[],int low,int high)  
26:  {  
27:   int mid;  
28:   if(low<high)  
29:   {  
30:   mid=(low+high)/2;  
31:   mergesort(a,low,mid);  
32:   mergesort(a,mid+1,high);  
33:   merge(a,low,mid,high);  
34:   }  
35:  }  
36:  void merge(int a[],int beg, int mid,int end)  
37:  {  
38:   int temp,b[MAX],left,right,i;  
39:   left=beg;  
40:   right=mid+1;  
41:   temp=beg;  
42:   while((left<=mid) && (right<=end))  
43:   {  
44:   if(a[left]<a[right])  
45:   {  
46:    b[temp]=a[left];  
47:    left=left+1;  
48:   }  
49:   else  
50:   {  
51:    b[temp]=a[right];  
52:    right=right+1;  
53:   }  
54:   temp=temp+1;  
55:   }  
56:   while(left<=mid)  
57:   {  
58:   b[temp]=a[left];  
59:   left=left+1;  
60:   temp=temp+1;  
61:   }  
62:   while(right<=end)  
63:   {  
64:   b[temp]=a[right];  
65:   right=right+1;  
66:   temp=temp+1;  
67:   }  
68:   for(i=beg;i<=end;i++)  
69:   {  
70:   a[i]=b[i];  
71:   }  
72:  }  
73:  void display(int a[],int num)  
74:  {  
75:   int i;  
76:   for(i=0;i<num;i++)  
77:   {  
78:   printf("%d\t",a[i]);  
79:   }  
80:  }  

Write a C Program To perform Heap Sort in Data Structure(Sorting Techniques)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void heapsort(int a[50], int n);  
4:  void heapify(int[], int);  
5:  void adjust(int[], int);  
6:  void main()  
7:  {  
8:   int a[50],n,i;  
9:   clrscr();  
10:   printf("Enter the no. of elements to be sorted:\n");  
11:   scanf("%d",&n);  
12:   printf("Enter the elements:\n");  
13:   for(i=0;i<n;i++)  
14:   {  
15:   scanf("%d",&a[i]);  
16:   }  
17:   printf("\nArray elements before Heapsort:\n");  
18:   for(i=0;i<n;i++)  
19:   {  
20:   printf("%d\t",a[i]);  
21:   }  
22:   heapsort(a,n);  
23:   printf("\nArray elements after Heapsort:\n");  
24:   for(i=0;i<n;i++)  
25:   {  
26:   printf("%d\t",a[i]);  
27:   }  
28:   getch();  
29:  }  
30:  void heapsort(int a[50], int n)  
31:  {  
32:   int i,t;  
33:   heapify(a,n);  
34:   for(i=n-1;i>0;i--)  
35:   {  
36:   t=a[0];  
37:   a[0]=a[i];  
38:   a[i]=t;  
39:   adjust(a,i);  
40:   }  
41:  }  
42:  void heapify(int a[50], int n)  
43:  {  
44:   int item,i,j,k;  
45:   for(k=1;k<n;k++)  
46:   {  
47:   item=a[k];  
48:   i=k;  
49:   j=(i-1)/2;  
50:   while((i>0) && (item>a[j]))  
51:   {  
52:    a[i] = a[j];  
53:    i=j;  
54:    j=(i-1)/2;  
55:   }  
56:   a[i]=item;  
57:   }  
58:  }  
59:  void adjust(int a[50], int n)  
60:  {  
61:   int item,i,j;  
62:   j=0;  
63:   item=a[j];  
64:   i=2*j+1;  
65:   while(i<=n-1)  
66:   {  
67:   if(i+1<=n-1)  
68:   if(a[i]<a[i+1])  
69:   i++;  
70:   if(item<a[i])  
71:   {  
72:    a[j]=a[i];  
73:    j=i;  
74:    i=2*j+1;  
75:   }  
76:   else  
77:   break;  
78:   }  
79:   a[j]=item;  
80:  }  

Write a C rogram To Perform Bubble Sort using Insertion Of Numbers

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<stdlib.h>  
4:  #define MAX 5  
5:  void main()  
6:  {  
7:   int i,hold,pass,a[MAX];  
8:   clrscr();  
9:   printf("Enter the numbers:\n");  
10:   for(i=0;i<=MAX-1;i++)  
11:   {  
12:   scanf("%d",&a[i]);  
13:   }  
14:   printf("\n");  
15:   printf("Array in sorted order is:\n");  
16:   for (pass=1;pass<MAX;pass++)  
17:   {  
18:   for (i=0;i<=MAX-pass;i++)  
19:   {  
20:    if (a[i]>a[i+1])   
21:    {   
22:    hold=a[i];  
23:    a[i]=a[i+1];  
24:    a[i+1]=hold;  
25:    }  
26:   }  
27:   }  
28:   for(i=0;i<=MAX-1;i++)  
29:   {  
30:   printf("%d\t",a[i]);  
31:   }  
32:   getch();  
33:  }  

Thursday 24 September 2015

Write a C Program To Find Predecessor And Successor Of An Element

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void pd(struct tree *root,int value,int option);  
13:  void main()  
14:  {  
15:  int i,x,a,data;  
16:  char ch;  
17:  struct tree *s;  
18:  clrscr();  
19:  root=NULL;  
20:  do  
21:  {  
22:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
23:  printf("3.Find predecessor and successor in Inorder traversal\n");  
24:  scanf("%d",&x);  
25:  if(x==1)  
26:  {  
27:  printf("Enter the element\n");  
28:  scanf("%d",&i);  
29:  insert(i);  
30:  }  
31:  else if(x==2)  
32:  {  
33:  clrscr();  
34:  printf("1.Inorder notation\n2.Preorder notation\n4.Postorder notation\n");  
35:  scanf("%d",&a);  
36:  display(root,a);  
37:  }  
38:  else if(x==3)  
39:  {  
40:  clrscr();  
41:  printf("Enter the element\n");  
42:  scanf("%d",&data);  
43:  printf("1.Inorder notation\n2.Preorder notation\n3.Postorder notation\n");  
44:  scanf("%d",&a);  
45:  pd(root,data,a);  
46:  }  
47:  printf("\nDo you want to continue> y/n\n");  
48:  scanf(" %c",&ch);  
49:  clrscr();  
50:  }  
51:  while(ch=='y');  
52:  getch();  
53:  }  
54:  void insert(int element)  
55:  {  
56:  struct tree *newptr,*ptr,*parent;  
57:  newptr=(struct tree *)malloc(sizeof(struct tree));  
58:  newptr->info=element;  
59:  newptr->left=newptr->right=NULL;  
60:  if(root==NULL)  
61:  {  
62:  root=newptr;  
63:  }  
64:  else  
65:  {  
66:  parent=NULL;  
67:  ptr=root;  
68:  while(ptr!=NULL)  
69:  {  
70:  parent=ptr;  
71:  if(element<ptr->info)  
72:  {  
73:  ptr=ptr->left;  
74:  }  
75:  else  
76:  {  
77:  ptr=ptr->right;  
78:  }  
79:  }  
80:  if(element<parent->info)  
81:  {  
82:  parent->left=newptr;  
83:  }  
84:  else  
85:  {  
86:  parent->right=newptr;  
87:  }  
88:  }  
89:  }  
90:  void display(struct tree *root,int choice)  
91:  {  
92:  if(choice==1)  
93:  {  
94:  if(root!=NULL)  
95:  {  
96:  display(root->left,choice);  
97:  printf("%d\t",root->info);  
98:  display(root->right,choice);  
99:  }  
100:  }  
101:  else if(choice==2)  
102:  {  
103:  if(root!=NULL)  
104:  {  
105:  printf("%d\t",root->info);  
106:  display(root->left,choice);  
107:  display(root->right,choice);  
108:  }  
109:  }  
110:  else if(choice==3)  
111:  {  
112:  if(root!=NULL)  
113:  {  
114:  display(root->left,choice);  
115:  display(root->right,choice);  
116:  printf("%d\t",root->info);  
117:  }  
118:  }  
119:  }  
120:  void pd(struct tree *root,int data,int option)  
121:  {  
122:  int array[20];  
123:  int i=0,k=0,a=0;  
124:  if(option==1)  
125:  {  
126:  if(root!=NULL)  
127:  {  
128:  pd(root->left,data,option);  
129:  array[i]=root->info;  
130:  i++;  
131:  pd(root->right,data,option);  
132:  }  
133:  }  
134:  else if(option==2)  
135:  {  
136:  if(root!=NULL)  
137:  {  
138:  array[i]=root->info;  
139:  i++;  
140:  pd(root->left,data,option);  
141:  pd(root->right,data,option);  
142:  }  
143:  }  
144:  else if(option==3)  
145:  {  
146:  if(root!=NULL)  
147:  {  
148:  pd(root->left,data,option);  
149:  pd(root->right,data,option);  
150:  array[i]=root->info;  
151:  i++;  
152:  }  
153:  }  
154:  for(i=0;i<19;i++)  
155:  {  
156:  if(array[i]==data)  
157:  {  
158:  k=i;  
159:  }  
160:  else  
161:  i++;  
162:  }  
163:  if(k==0)  
164:  {  
165:  printf("No Predecessor :-(\n");  
166:  }  
167:  else  
168:  printf("Predecessor = %d",array[k-1]);  
169:  printf("Successor = %d",array[k+1]);  
170:  }  

Write a C Program To Traverse Binary Search Tree (INORDER , PREORDER and POSTORDER)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void main()  
13:  {  
14:  int i,x,a;  
15:  char ch;  
16:  clrscr();  
17:  root=NULL;  
18:  do  
19:  {  
20:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
21:  scanf("%d",&x);  
22:  if(x==1)  
23:  {  
24:  printf("Enter the element\n");  
25:  scanf("%d",&i);  
26:  insert(i);  
27:  }  
28:  else if(x==2)  
29:  {  
30:  clrscr();  
31:  printf("1.Infix notation\n2.Prefix notation\n4.Postfix notation\n");  
32:  scanf("%d",&a);  
33:  display(root,a);  
34:  }  
35:  printf("\nDo you want to continue> y/n\n");  
36:  scanf(" %c",&ch);  
37:  clrscr();  
38:  }  
39:  while(ch=='y');  
40:  getch();  
41:  }  
42:  void insert(int element)  
43:  {  
44:  struct tree *newptr,*ptr,*parent;  
45:  newptr=(struct tree *)malloc(sizeof(struct tree));  
46:  newptr->info=element;  
47:  newptr->left=newptr->right=NULL;  
48:  if(root==NULL)  
49:  {  
50:  root=newptr;  
51:  }  
52:  else  
53:  {  
54:  parent=NULL;  
55:  ptr=root;  
56:  while(ptr!=NULL)  
57:  {  
58:  parent=ptr;  
59:  if(element<ptr->info)  
60:  {  
61:  ptr=ptr->left;  
62:  }  
63:  else  
64:  {  
65:  ptr=ptr->right;  
66:  }  
67:  }  
68:  if(element<parent->info)  
69:  {  
70:  parent->left=newptr;  
71:  }  
72:  else  
73:  {  
74:  parent->right=newptr;  
75:  }  
76:  }  
77:  }  
78:  void display(struct tree *root,int choice)  
79:  {  
80:  if(choice==1)  
81:  {  
82:  if(root!=NULL)  
83:  {  
84:  display(root->left,choice);  
85:  printf("%d\t",root->info);  
86:  display(root->right,choice);  
87:  }  
88:  }  
89:  else if(choice==2)  
90:  {  
91:  if(root!=NULL)  
92:  {  
93:  printf("%d\t",root->info);  
94:  display(root->left,choice);  
95:  display(root->right,choice);  
96:  }  
97:  }  
98:  else if(choice==3)  
99:  {  
100:  if(root!=NULL)  
101:  {  
102:  display(root->left,choice);  
103:  display(root->right,choice);  
104:  printf("%d\t",root->info);  
105:  }  
106:  }  
107:  }  

Write a C Program To Traverse Binary Tree (Creation , Insertion And Traversal

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void pd(struct tree *root,int value,int option);  
13:  void main()  
14:  {  
15:  int i,x,a,data;  
16:  char ch;  
17:  struct tree *s;  
18:  clrscr();  
19:  root=NULL;  
20:  do  
21:  {  
22:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
23:  printf("3.Find predecessor and successor in Inorder traversal\n");  
24:  scanf("%d",&x);  
25:  if(x==1)  
26:  {  
27:  printf("Enter the element\n");  
28:  scanf("%d",&i);  
29:  insert(i);  
30:  }  
31:  else if(x==2)  
32:  {  
33:  clrscr();  
34:  printf("1.Inorder notation\n2.Preorder notation\n4.Postorder notation\n");  
35:  scanf("%d",&a);  
36:  display(root,a);  
37:  }  
38:  else if(x==3)  
39:  {  
40:  clrscr();  
41:  printf("Enter the element\n");  
42:  scanf("%d",&data);  
43:  printf("1.Inorder notation\n2.Preorder notation\n3.Postorder notation\n");  
44:  scanf("%d",&a);  
45:  pd(root,data,a);  
46:  }  
47:  printf("\nDo you want to continue> y/n\n");  
48:  scanf(" %c",&ch);  
49:  clrscr();  
50:  }  
51:  while(ch=='y');  
52:  getch();  
53:  }  
54:  void insert(int element)  
55:  {  
56:  struct tree *newptr,*ptr,*parent;  
57:  newptr=(struct tree *)malloc(sizeof(struct tree));  
58:  newptr->info=element;  
59:  newptr->left=newptr->right=NULL;  
60:  if(root==NULL)  
61:  {  
62:  root=newptr;  
63:  }  
64:  else  
65:  {  
66:  parent=NULL;  
67:  ptr=root;  
68:  while(ptr!=NULL)  
69:  {  
70:  parent=ptr;  
71:  if(element<ptr->info)  
72:  {  
73:  ptr=ptr->left;  
74:  }  
75:  else  
76:  {  
77:  ptr=ptr->right;  
78:  }  
79:  }  
80:  if(element<parent->info)  
81:  {  
82:  parent->left=newptr;  
83:  }  
84:  else  
85:  {  
86:  parent->right=newptr;  
87:  }  
88:  }  
89:  }  
90:  void display(struct tree *root,int choice)  
91:  {  
92:  if(choice==1)  
93:  {  
94:  if(root!=NULL)  
95:  {  
96:  display(root->left,choice);  
97:  printf("%d\t",root->info);  
98:  display(root->right,choice);  
99:  }  
100:  }  
101:  else if(choice==2)  
102:  {  
103:  if(root!=NULL)  
104:  {  
105:  printf("%d\t",root->info);  
106:  display(root->left,choice);  
107:  display(root->right,choice);  
108:  }  
109:  }  
110:  else if(choice==3)  
111:  {  
112:  if(root!=NULL)  
113:  {  
114:  display(root->left,choice);  
115:  display(root->right,choice);  
116:  printf("%d\t",root->info);  
117:  }  
118:  }  
119:  }  
120:  void pd(struct tree *root,int data,int option)  
121:  {  
122:  int array[20];  
123:  int i=0,k=0,a=0;  
124:  if(option==1)  
125:  {  
126:  if(root!=NULL)  
127:  {  
128:  pd(root->left,data,option);  
129:  array[i]=root->info;  
130:  i++;  
131:  pd(root->right,data,option);  
132:  }  
133:  }  
134:  else if(option==2)  
135:  {  
136:  if(root!=NULL)  
137:  {  
138:  array[i]=root->info;  
139:  i++;  
140:  pd(root->left,data,option);  
141:  pd(root->right,data,option);  
142:  }  
143:  }  
144:  else if(option==3)  
145:  {  
146:  if(root!=NULL)  
147:  {  
148:  pd(root->left,data,option);  
149:  pd(root->right,data,option);  
150:  array[i]=root->info;  
151:  i++;  
152:  }  
153:  }  
154:  for(i=0;i<19;i++)  
155:  {  
156:  if(array[i]==data)  
157:  {  
158:  k=i;  
159:  }  
160:  else  
161:  i++;  
162:  }  
163:  if(k==0)  
164:  {  
165:  printf("No Predecessor :-(\n");  
166:  }  
167:  else  
168:  printf("Predecessor = %d",array[k-1]);  
169:  printf("Successor = %d",array[k+1]);  
170:  }  

Write a C Program to Evaluate Postfix Expression using Stack

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<string.h>  
4:  #define MAX 50  
5:  int stack[MAX];  
6:  char post[MAX];  
7:  int top=-1;  
8:  void pushstack(int tmp);  
9:  void calculator(char c);  
10:  void main()  
11:  {  
12:   int i;  
13:   clrscr();  
14:   printf("Insert a postfix notation :: ");  
15:   gets(post);  
16:   for(i=0;i<strlen(post);i++)  
17:   {  
18:   if(post[i]>='0' && post[i]<='9')  
19:   {  
20:    pushstack(i);  
21:   }  
22:   if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^')  
23:   {  
24:    calculator(post[i]);  
25:   }  
26:   }  
27:   printf("\n\nResult :: %d",stack[top]);  
28:   getch();  
29:  }  
30:  void pushstack(int tmp)  
31:  {  
32:   top++;  
33:   stack[top]=(int)(post[tmp]-48);  
34:  }  
35:  void calculator(char c)  
36:  {  
37:   int a,b,ans;  
38:   a=stack[top];  
39:   stack[top]='\0';  
40:   top--;  
41:   b=stack[top];  
42:   stack[top]='\0';  
43:   top--;  
44:   switch(c)  
45:   {  
46:   case '+':  
47:   ans=b+a;  
48:   break;  
49:   case '-':  
50:   ans=b-a;  
51:   break;  
52:   case '*':  
53:   ans=b*a;  
54:   break;  
55:   case '/':  
56:   ans=b/a;  
57:   break;  
58:   case '^':  
59:   ans=b^a;  
60:   break;  
61:   default:  
62:   ans=0;  
63:   }  
64:   top++;  
65:   stack[top]=ans;  
66:  }  

Write a C Program To Convert Infix to Postfix Using Stack

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define SIZE 100  
4:  int top = -1;  
5:  char stack[SIZE];  
6:  void push(char item);  
7:  char pop();  
8:  int is_operator(char symbol);  
9:  int precedence(char symbol);  
10:  void main()  
11:  {  
12:   int i, j;  
13:   char infix_exp[SIZE], postfix_exp[SIZE];  
14:   char item, x;  
15:   clrscr();  
16:   printf("\nEnter the arithmetic expression in Infix notation enclosed in parentheses: \n");  
17:   gets(infix_exp);  
18:   i=0;  
19:   j=0;  
20:   item=infix_exp[i++];  
21:   while(item != '\0')  
22:   {  
23:   if(item == '(')  
24:   {  
25:    push(item);  
26:   }  
27:   else if((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z'))  
28:   {  
29:    postfix_exp[j++] = item;  
30:   }  
31:   else if(is_operator(item) == 1)  
32:   {  
33:    x=pop();  
34:    while(is_operator(x) == 1 && precedence(x) >= precedence(item))  
35:    {  
36:    postfix_exp[j++] = x;  
37:    x = pop();  
38:    }  
39:    push(x);  
40:    push(item);    
41:   }  
42:   else if(item == ')')  
43:   {  
44:    x = pop();  
45:    while(x != '(')  
46:    {  
47:    postfix_exp[j++] = x;  
48:    x = pop();  
49:    }  
50:   }  
51:   else  
52:   {  
53:    printf("\nInvalid Arithmetic Expression.\n");  
54:    getch();  
55:    exit(0);  
56:   }  
57:   item = infix_exp[i++];  
58:   }  
59:   postfix_exp[j++] = '\0';  
60:   printf("\nArithmetic expression in Postfix notation: ");  
61:   puts(postfix_exp);  
62:   getch();  
63:  }  
64:  void push(char item)  
65:  {  
66:   if(top >= SIZE-1)  
67:   {  
68:   printf("\nStack Overflow. Push not possible.\n");  
69:   }  
70:   else  
71:   {  
72:   top = top+1;  
73:   stack[top] = item;  
74:   }  
75:  }  
76:  char pop()  
77:  {  
78:   char item = NULL;  
79:   if(top <= -1)  
80:   {  
81:   printf("\nStack Underflow. Pop not possible.\n");  
82:   }  
83:   else  
84:   {  
85:   item = stack[top];  
86:   stack[top] = NULL;  
87:   top = top-1;  
88:   }  
89:   return(item);  
90:  }  
91:  int is_operator(char symbol)  
92:  {  
93:   if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-')  
94:   {  
95:   return 1;  
96:   }  
97:   else  
98:   {  
99:   return 0;  
100:   }  
101:  }  
102:  int precedence(char symbol)  
103:  {  
104:   if(symbol == '^')  
105:   {  
106:   return(3);  
107:   }  
108:   else if(symbol == '*' || symbol == '/')  
109:   {  
110:   return(2);  
111:   }  
112:   else if(symbol == '+' || symbol == '-')  
113:   {  
114:   return(1);  
115:   }  
116:   else  
117:   {  
118:   return(0);  
119:   }  
120:  }  

Tuesday 22 September 2015

Write a C program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen.

Write a C program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<process.h>  
4:  struct stud  
5:  {  
6:        int rno;  
7:        char *nm;  
8:  };  
9:  void main()  
10:  {  
11:        struct stud *s;  
12:        int n,i;  
13:        FILE *fp,*fp1;  
14:        clrscr();  
15:        printf("Enter how many record you want to input : ");  
16:        scanf("%d",&n);  
17:        s=(struct stud *)malloc(n*sizeof(struct stud));  
18:        fp=fopen("STUD.txt","w");  
19:        for(i=0;i<n;i++)  
20:        {  
21:              printf("\n\tInformation for student : %d\n",i+1);  
22:              printf("Enter Roll No : ");  
23:              scanf("%d",&s[i].rno);  
24:              printf("Enter Name : ");  
25:              fflush(stdin);  
26:              gets(s[i].nm);  
27:              fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);  
28:        }  
29:        fclose(fp);  
30:        fp=fopen("STUD.txt","r");  
31:        fp1=fopen("DATA.txt","w");  
32:        printf("\nContent of the STUD.txt file is\n");  
33:        printf("Roll No  Name\n");  
34:        printf("---------------------------\n");  
35:        while(!feof(fp))  
36:        {  
37:              fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);  
38:              fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);  
39:              printf("%7d  %-20s\n",s[i].rno,s[i].nm);    }  
40:        fcloseall();  
41:        getch();  
42:  }  
43:  Output:  
44:  Enter how many record you want to input : 3  
45:        Information for student : 1  
46:  Enter Roll No : 1  
47:  Enter Name : Kartik  
48:        Information for student : 2  
49:  Enter Roll No : 2  
50:  Enter Name : Umesh  
51:        Information for student : 3  
52:  Enter Roll No : 3  
53:  Enter Name : Mehul  
54:  Content of the STUD.txt file is  
55:  Roll No  Name  
56:  ---------------------------  
57:     1  Kartik  
58:     2  Umesh  
59:     3  Mehul  

Write a C program to create linear linked list interactively and print out the list and total number of items in the list.

Write a C program to create linear linked list interactively and print out the list and total number of items in the list.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<process.h>  
4:  #include<stdlib.h>  
5:  struct node  
6:  {  
7:        int info;  
8:        struct node *next;  
9:  }*head,*last,*temp,*n;  
10:  void menu();  // to select the choise  
11:  void create(); // insert at last  
12:  void display(); // to print the link list  
13:  int i;  
14:  void main()  
15:  {  
16:        i=0;  
17:        clrscr();  
18:        menu();  
19:        getch();  
20:  }  
21:  void menu()  
22:  {  
23:        int ch;  
24:        printf("1. Create\n");  
25:        printf("2. Display\n");  
26:        printf("3. Exit\n");  
27:        printf("Enter choise : ");  
28:        scanf("%d",&ch);  
29:        switch(ch)  
30:        {  
31:              case 1 : create();  
32:              case 2 : display();  
33:              case 3 : printf("You have choose to exit\n");  
34:                     getch();  exit(0);  
35:              default : printf("Invalid choise\n"); menu();  
36:        }  
37:  }  
38:  void create()  
39:  {  
40:        n= new node;  
41:        printf("Enter the information : ");  
42:        scanf("%d",&n->info);  
43:        if(i==0)  
44:        {  
45:              head=n;  
46:              i++;  
47:        }  
48:        else  
49:        {  
50:              last->next=n;  
51:              i++;  
52:        }  
53:        last=n;  
54:        last->next=NULL;  
55:        menu();  
56:  }  
57:  void display()  
58:  {  
59:        if(head==NULL)  
60:        {  
61:              printf("No information in list\n");  
62:              menu();  
63:        }  
64:        temp=head;  
65:        while(temp!=NULL)  
66:        {  
67:              printf("%d->",temp->info);  
68:              temp=temp->next;  
69:        }  
70:        printf("\n");  
71:        printf("Total number of elements in the link list are : %d\n",i);  
72:        menu();  
73:  }  
74:  Output:  
75:  1. Create  
76:  2. Display  
77:  3. Exit  
78:  Enter choise : 1  
79:  Enter the information : 12  
80:  1. Create  
81:  2. Display  
82:  3. Exit  
83:  Enter choise : 1  
84:  Enter the information : 23  
85:  1. Create  
86:  2. Display  
87:  3. Exit  
88:  Enter choise : 1  
89:  Enter the information : 45  
90:  1. Create  
91:  2. Display  
92:  3. Exit  
93:  Enter choise : 2  
94:  12->23->45->  
95:  Total number of elements in the link list are : 3  
96:  1. Create  
97:  2. Display  
98:  3. Exit  
99:  Enter choise : 3  
100:  You have choose to exit  

Sunday 30 August 2015

Write a C function using pointers to add two matrices and to return the resultant matrix to the calling function

Write a C function using pointers to add two matrices and to return the resultant matrix to the calling function
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int a[5][5],b[5][5],row,col;  
4:  void add(int(*)[5]);  
5:  int main()  
6:  {  
7:        int c[5][5],i,j;  
8:        clrscr();  
9:        printf("Enter row : ");  
10:        scanf("%d",&row);  
11:        printf("Enter column : ");  
12:        scanf("%d",&col);  
13:        printf("Enter matrix A :\n");  
14:        for(i=0;i<row;i++)  
15:        {  
16:              for(j=0;j<col;j++)  
17:              {  
18:                    scanf("%d",&a[i][j]);  
19:              }  
20:        }  
21:        printf("Enter matrix B :\n");  
22:        for(i=0;i<row;i++)  
23:        {  
24:              for(j=0;j<col;j++)  
25:              {  
26:                    scanf("%d",&b[i][j]);  
27:              }  
28:        }  
29:        add(c);  
30:        printf("Addition :\n");  
31:        for(i=0;i<row;i++)  
32:        {  
33:              for(j=0;j<col;j++)  
34:              {  
35:                    printf("%d\t",c[i][j]);  
36:              }  
37:              printf("\n");  
38:        }  
39:        getch();  
40:        return 0;  
41:  }  
42:  void add(int c[5][5])  
43:  {  
44:        int i,j;  
45:        for(i=0;i<row;i++)  
46:        {  
47:              for(j=0;j<col;j++)  
48:              {  
49:                    c[i][j]=a[i][j]+b[i][j];  
50:              }  
51:        }  
52:  }  

Write a C function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function

Write a C function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function Write a C function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void solve(int*,float*);  
4:  int main()  
5:  {  
6:        int a[3];  
7:        float *root;  
8:        clrscr();  
9:        printf("Enter the value of A : ");  
10:        scanf("%d",&a[0]);  
11:        printf("Enter the value of B : ");  
12:        scanf("%d",&a[1]);  
13:        printf("Enter the value of C : ");  
14:        scanf("%d",&a[2]);  
15:        solve(a,root);  
16:        if(root==NULL)  
17:              printf("The root is not possible\n");  
18:        else if(root[0]==root[1])  
19:              printf("The root is %.2f\n",root[0]);  
20:        else  
21:              printf("The roots are %.2f & %.2f\n",root[0],root[1]);  
22:        getch();  
23:        return 0;  
24:  }  
25:  void solve(int *a,float *r)  
26:  {  
27:        float d;  
28:        d=a[1]*a[1]-4*a[0]*a[2];  
29:        if(d<0)  
30:        {  
31:              r=NULL;  
32:        }  
33:        else if(d==0)  
34:        {  
35:              r[0]=r[1]=-a[1]/(2*a[0]);  
36:        }  
37:        else  
38:        {  
39:              r[0]=-a[1]/(2*a[0]);  
40:              r[1]=-a[1]/(2*a[0]);  
41:        }  
42:  }  

Write a C Program using pointers to read an array of integers and print its elements in reverse order

Write a C Program using pointers to read an array of integers and print its elements in reverse order
1:  #include<conio.h>  
2:  #include<stdio.h>  
3:  #include<stdlib.h>  
4:  int main()  
5:  {  
6:        int *ptr,i,n;  
7:        clrscr();  
8:        printf("Enter the no of elements:");  
9:        scanf("%d",&n);  
10:        ptr=(int *)malloc(sizeof(int)*n);  
11:        if(ptr==NULL)  
12:        {  
13:              printf("Not enough memory");  
14:              exit(1);  
15:        }  
16:        for(i=0; i<n; i++)  
17:        {  
18:              printf("Enter %d element : ",i+1);  
19:              scanf("%d",&ptr[i]);  
20:        }  
21:        printf("Array in original order\n");  
22:        for(i=0; i<n; i++)  
23:        {  
24:              printf("%d\n",ptr[i]);  
25:        }  
26:        printf("Array in reverse order\n");  
27:        for(i=n-1; i>=0; i--)  
28:        {  
29:              printf("%d\n",ptr[i]);  
30:        }  
31:        getch();  
32:        return 0;  
33:  }  

Write a C program declare following structure member: name, code, age, weight and height. Read all members of the structure for 100 persons and find list of persons with all related data whose weight > 50 and height > 40 and print the same with suitable format and title

Write a C program declare following structure member: name, code, age, weight and height. Read all members of the structure for 100 persons and find list of persons with all related data whose weight > 50 and height > 40 and print the same with suitable format and title
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define size 3  
4:  struct  
5:  {  
6:        char nm[20],cd;  
7:        int height,age,weight;  
8:  }s[size];  
9:  int main()  
10:  {  
11:        int i;  
12:        clrscr();  
13:        for(i=0;i<size;i++)  
14:        {  
15:              printf("For person %d\n",i+1);  
16:              printf("Enter the name : ");  
17:              fflush(stdin);  
18:              gets(s[i].nm);  
19:              printf("Enter the code : ");  
20:              flushall(.);  
21:              s[i].cd=getc(stdin);  
22:              printf("Enter the age : ");  
23:              fflush(stdin);  
24:              scanf("%d",&s[i].age);  
25:              printf("Enter the weight : ");  
26:              fflush(stdin);  
27:              scanf("%d",&s[i].weight);  
28:              printf("Enter the height : ");  
29:              fflush(stdin);  
30:              scanf("%d",&s[i].height);  
31:        }  
32:        printf("\n\nData having weight>50 & height>40\n");  
33:        printf("\nName   Code Age Height Weight\n");  
34:        printf("--------------------------------\n");  
35:        for(i=0;i<size;i++)  
36:        {  
37:              if(s[i].weight>50 && s[i].height>40)  
38:              printf("%-10s%-5c%3d%7d%7d\n",  
39:  s[i].nm,s[i].cd,s[i].age,s[i].height,s[i].weight);  
40:        }  
41:        getch();  
42:        return 0;  
43:  }  

Define a structure called cricket that will describe the following information: Player name Team name Batting average Using cricket, declare an array player with 50 elements and wire a program to read the information about all the 50 players and print a list

Define a structure called cricket that will describe the following information: Player name Team name Batting average Using cricket, declare an array player with 50 elements and wire a program to read the information about all the 50 players and print a list
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<string.h>  
4:  struct cricket  
5:  {  
6:        char nm[20],team[20];  
7:        int avg;  
8:  };  
9:  #define total 5  
10:  int main()  
11:  {  
12:        struct cricket player[total],temp;  
13:        int i,j;  
14:        clrscr();  
15:        for(i=0;i<total;i++)  
16:        {  
17:              printf("For player %d\n",i+1);  
18:              printf("Enter the name of player : ");  
19:              fflush(stdin);  
20:              gets(player[i].nm);  
21:              printf("Enter the team : ");  
22:              fflush(stdin);  
23:              gets(player[i].team);  
24:              printf("Enter the batting average : ");  
25:              fflush(stdin);  
26:              scanf("%d",&player[i].avg);  
27:        }  
28:        printf("\nTeam    Name    Average\n");  
29:        printf("               \n");  
30:        for(i=0;i<total;i++)  
31:        {  
32:              printf("%-10s %-10s %7d\n",player[i].team,player[i].nm,player[i].avg);  
33:        }  
34:        getch();  
35:        return 0;  
36:  }  

Write a C Program that search an item from array of string

Write a C Program that search an item from array of string
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<string.h>  
4:  int string(char[],char[]);  
5:  int main()  
6:  {  
7:        char str[100],find[20];  
8:        int i;  
9:        clrscr();  
10:        printf("Enter the string :");  
11:        gets(str);  
12:        printf("Enter the srting that you want to find : ");  
13:        gets(find);  
14:        i=string(str,find);  
15:        if(i==1)  
16:              printf("%s is found in %s",find,str);  
17:        else  
18:              printf("%s is not found in %s",find,str);  
19:        getch();  
20:        return 0;  
21:  }  
22:  int string(char str[20], char find[20])  
23:  {  
24:        int i,j,flag;  
25:        for(i=0;str[i];i++)  
26:        {  
27:              if(str[i]==find[0])  
28:              {  
29:                    flag=1;  
30:                    for(j=1;find[j];j++)  
31:                    {  
32:                          if(str[i+j]!=find[j])  
33:                          {  
34:                                flag=0;  
35:                                break;  
36:                          }  
37:                    }  
38:                    if(flag==1)  
39:                          break;  
40:              }  
41:        }  
42:        return flag;  
43:  }  

Thursday 27 August 2015

Write a C Program To Write function to upper the string

Write a C Program To Write function to Reverse the string
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void reverse(char[]);  
4:  int main()  
5:  {  
6:        char str[20];  
7:        clrscr();  
8:        printf("Enter string : ");  
9:        gets(str);  
10:        reverse(str);  
11:        getch();  
12:        return 0;  
13:  }  
14:  void reverse(char str[20])  
15:  {  
16:        int i;  
17:        printf("%s in reverse order is ",str);  
18:        for(i=strlen(str)-1;i>=0;i--)  
19:              printf("%c",str[i]);  
20:  }  

Write a C Program To Write function that will scan a character string passed as an argument and convert all lower-case character into their upper-case equivalent

Write a C Program To Write function that will scan a character string passed as an argument and convert all lower-case character into their upper-case equivalent
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void upper(char[]);  
4:  int main()  
5:  {  
6:        char str[20];  
7:        clrscr();  
8:        printf("Enter string : ");  
9:        gets(str);  
10:        upper(str);  
11:        getch();  
12:        return 0;  
13:  }  
14:  void upper(char str[20])  
15:  {  
16:        int i;  
17:        printf("%s in upper case is ",str);  
18:        for(i=0;str[i];i++)  
19:        {  
20:              if(str[i]>96 && str[i]<123)  
21:                    str[i]-=32;  
22:        }  
23:        printf("%s",str);  
24:  }  

Write a C Program To Write function which returns 1 if the given number is palindrome otherwise returns 0

Write a C Program To Write function which returns 1 if the given number is palindrome otherwise returns 0
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int pelindrome(int);  
4:  int main()  
5:  {  
6:        int n,p;  
7:        clrscr();  
8:        printf("Enter a number : ");  
9:        scanf("%d",&n);  
10:        p=pelindrome(n);  
11:        if(p==1)  
12:              printf("%d is pelindrome",n);  
13:        else  
14:              printf("%d is not pelindrome",n);  
15:        getch();  
16:        return 0;  
17:  }  
18:  int pelindrome(int n)  
19:  {  
20:        char a[6],b[6];  
21:        itoa(n,a,10);  
22:        strcpy(b,a);  
23:        strrev(b);  
24:        if(strcmp(a,b)==0)  
25:              return 1;  
26:        else  
27:              return 0;  
28:  }  

Write a C Program to add first n numbers.

Write a C Program to add first n numbers.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void sum(int);  
4:  int main()  
5:  {  
6:        int n;  
7:        clrscr();  
8:        printf("Enter a number : ");  
9:        scanf("%d",&n);  
10:        sum(n);  
11:        getch();  
12:        return 0;  
13:  }  
14:  void sum(int n)  
15:  {  
16:        int i,s=0;  
17:        for(i=1;i<=n;i++)  
18:        {  
19:              s+=i;  
20:        }  
21:        printf("Sum of first %d integer is %d\n",n,s);  
22:  }  

Write a C Program To Write function prime that returns 1 if its argument is a prime no. and returns 0 otherwise (PRIME NUMBER)

Write a C Program To Write function prime that returns 1 if its argument is a prime no. and returns 0 otherwise (PRIME NUMBER)
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int prime(int);  
4:  int main()  
5:  {  
6:        int n,p;  
7:        clrscr();  
8:        printf("Enter a number : ");  
9:        scanf("%d",&n);  
10:        p=prime(n);  
11:        if(p==1)  
12:              printf("%d is prime\n",n);  
13:        else  
14:              printf("%d is not prime\n",n);  
15:        getch();  
16:        return 0;  
17:  }  
18:  int prime(int n)  
19:  {  
20:        int i;  
21:        for(i=2;i<n;i++)  
22:        {  
23:              if(n%i==0)  
24:                    return 0;  
25:        }  
26:        return 1;  
27:  }  

Write a C Program To Use recursive calls to evaluate f(x) = x – x3/3! + x5/5! – x7/7! + ……

Write a C Program To Use recursive calls to evaluate f(x) = x – x3/3! + x5/5! – x7/7! + ……
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        float series(float,int),x;  
6:        int n;  
7:        clrscr();  
8:        printf("\nEnter X:");  
9:        scanf("%f",&x);  
10:        printf("\nEnter n:");  
11:        scanf("%d",&n);  
12:        printf("\nAns %f",series(x,n));  
13:        getch();  
14:        return 0;  
15:  }  
16:  float series(float x,int n)  
17:  {  
18:        long factorial(int);  
19:        float power(float,int);  
20:        float sum=0;  
21:        int i,s=1;  
22:        for(i=1;i<=n;i+=2)  
23:        {  
24:              sum+=(power(x,i)/factorial(i))*s;  
25:              s*=-1;  
26:        }  
27:        return sum;  
28:  }  
29:  float power(float x, int y)  
30:  {  
31:        float p=1;  
32:        int i;  
33:        for(i=1;i<=y;i++)p*=x;  
34:        return p;  
35:  }  
36:  long factorial(int p)  
37:  {  
38:        long f=1;  
39:        int i;  
40:        for(i=1;i<=p;i++)f*=i;  
41:        return f;  
42:  }  

Write a C Program that finds a given word in a string.

Write a C Program that finds a given word in a string.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        char str[100],find[100];  
6:        int i,j,flag;  
7:        clrscr();  
8:        printf("Enter the string :");  
9:        gets(str);  
10:        printf("Enter the srting that you want to find : ");  
11:        gets(find);  
12:        for(i=0;str[i];i++)  
13:        {  
14:              if(str[i]==find[0])  
15:              {  
16:                    flag=1;  
17:                    for(j=1;find[j];j++)  
18:                    {  
19:                          if(str[i+j]!=find[j])  
20:                          {  
21:                                flag=0;  
22:                                break;  
23:                          }  
24:                    }  
25:                    if(flag==1)  
26:                          break;  
27:              }  
28:        }  
29:        if(flag==1)  
30:              printf("%s is found in %s at %d position",find,str,i+1);  
31:        else  
32:              printf("%s is not found in %s",find,str);  
33:        getch();  
34:        return 0;  
35:  }  

Wednesday 26 August 2015

Write a C Program that appends the one string to another string

Write a C Program that appends the one string to another string
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<string.h>  
4:  int main()  
5:  {  
6:        char str[100],app[20];  
7:        clrscr();  
8:        printf("Enter the string :");  
9:        gets(str);  
10:        printf("Enter the srting that you want to append : ");  
11:        gets(app);  
12:        printf("The string is : %s\n",str);  
13:        printf("The string you want to append is : %s\n",app);  
14:        printf("The string after append is : %s\n",strcat(str,app));  
15:        getch();  
16:        return 0;  
17:  }  

Write a C Program that will read a string and rewrite it in the alphabetical order. i.e. the word STRING should be written as GINRST

Write a C Program that will read a string and rewrite it in the alphabetical order. i.e. the word STRING should be written as GINRST
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        char str[100],temp;  
6:        int i,j;  
7:        clrscr();  
8:        printf("Enter the string :");  
9:        gets(str);  
10:        printf("%s in ascending order is -> ",str);  
11:        for(i=0;str[i];i++)  
12:        {  
13:              for(j=i+1;str[j];j++)  
14:              {  
15:                    if(str[j]<str[i])  
16:                    {  
17:                          temp=str[j];  
18:                          str[j]=str[i];  
19:                          str[i]=temp;  
20:                    }  
21:              }  
22:        }  
23:        printf("%s\n",str);  
24:        getch();  
25:        return 0;  
26:  }  

Write a C Program To FInd that will read a text and count all occurrences of a particular word.

Write a C Program To FInd that will read a text and count all occurrences of a particular word.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        char str[100],find[100];  
6:        int i,j,cnt=0,flag;  
7:        clrscr();  
8:        printf("Enter the string :");  
9:        gets(str);  
10:        printf("Enter the srting that you want to find : ");  
11:        gets(find);  
12:        for(i=0;str[i];i++)  
13:        {  
14:              flag=0;  
15:              if(str[i]==find[0])  
16:              {  
17:                    flag=1;  
18:                    for(j=1;find[j];j++)  
19:                    {  
20:                          if(str[i+j]!=find[j])  
21:                          {  
22:                                flag=0;  
23:                                break;  
24:                          }  
25:                    }  
26:                    if(flag==1)  
27:                          cnt++;  
28:              }  
29:              else  
30:              {  
31:              }  
32:        }  
33:        printf("%s occurs in %s %d times\n",find,str,cnt);  
34:        getch();  
35:        return 0;  
36:  }  

Write a C Program To Find Size of Int , Float And Double Variable

Write a C Program To Find Size of Int , Float And Double Variable
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        clrscr();  
6:        printf("Size of int is -> %d\n",sizeof(int));  
7:        printf("Size of long is -> %d\n",sizeof(float));  
8:        printf("Size of double is -> %d\n",sizeof(double));  
9:        getch();  
10:        return 0;  
11:  }  

Write a C Program to Find String Length

Write a C Program to Find String Length
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int main()  
4:  {  
5:        char str[100];  
6:        int l;  
7:        clrscr();  
8:        printf("Enter the string:");  
9:        gets(str);  
10:        for(l=0; str[l]; l++);  
11:        printf("The length of %s is -> %d",str,l);  
12:        getch();  
13:        return 0;  
14:  }  

Monday 24 August 2015

Write A C Program to Print Floyd's Triangle

#include<stdio.h>
#include<conio.h>

int main()
{
            int i,j,n,k;
            clrscr();

            printf("\n\n Enter  the number : ");
            scanf("%d",&n);

            for(i=1,k=1;i<=n;i++)
            {
                        for(j=1;j<=i;j++,k++)
                        {
                                    printf(" %d ",k);
                        }
                        printf("\n");
            }
            getch();

            return 0;
}


Write a C Program to Print Triangle Patter Program

#include<stdio.h>
#include<conio.h>

int main()
{
            int i,n,j,k;
            clrscr();

            printf("Enter  the number : ");
            scanf("%d",&n);
            printf("\n");
            for(i=0;i<n;i++)
            {
                        for(k=1;k<=i;k++)
                        {
                                    printf(" ");
                        }
                        for(j=1;j<=n-i;j++)
                        {
                                    printf(" %c",96+j);
                        }
                        printf("\n");
            }

            getch();

return 0;
}                                                                                                                    


Write a C Program that accept basic, HRA, and convergence from the user and calculate total salary

#include<stdio.h>
#include<conio.h>

int main()
{
            float basic,HRA,cnvg,totsal;
            clrscr();

            printf("\n Enter basic salary : ");
            scanf("%f",&basic);

            printf("\n Enter HRA : ");
            scanf("%f",&HRA);

            printf("\n Enter convergence : ");
            scanf("%f",&cnvg);

            HRA=(basic*HRA)/100;
            cnvg=(basic*cnvg)/100;
            totsal=basic+HRA+cnvg;

            printf("\n\n Total salary is %.2f",totsal);

            getch();

            return 0;
}


Write a C Program To find the roots of an equation ax2 + bx + c = 0

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
            float a,b,c,alf,bt,dlt;
            clrscr();

            printf("\n Enter a: ");
            scanf("%f",&a);

            printf("\n Enter b: ");
            scanf("%f",&b);

            printf("\n Enter c: ");
            scanf("%f",&c);

            dlt=b*b-4*a*c;

            if(dlt==0)
            {
                        printf("\n ALPHA=BETA=%f",-b/(2*a));
            }
            else if(dlt<0)
            {
                        printf("\n Imaginary Roots");
            }
            else
            {
                        alf=(-b+sqrt(dlt))/(2*a);
                        bt=(-b-sqrt(dlt))/(2*a);
                        printf("\n\n Alpha = %f\n Beta=%f\n",alf,bt);
            }

            getch();

            return 0;

}