Learn C# Programming tutorial 21-30/58

c# programming

C# programming language is a structure oriented programming language. Here some codes of C# programming that helps of you learn c# programming. 

  • Find the value of cos(x)
  • Find the value of sin(x)
  • Floyd Triangle
  • GCD LCM Using Euclids Algorithm
  • Heap Sort
  • Insertion Sort
  • Interpolation Search
  • Inverse of the Matrix
  • Largest Among n digit
  • LeapYear

Name Code

Find the value of cos(x)

      #include <stdio.h>
   #include <conio.h>
   #include <math.h>
   #include <stdlib.h>
   void main()
   {
     int n, x1;
     float acc, term, den, x, cosx=0, cosval;
     clrscr();
     printf("Enter the value of x (in degrees)\n");
     scanf("%f",&x);
     x1 = x;
     /* Converting degrees to radians*/
     x = x*(3.142/180.0);
     cosval = cos(x);
     printf("Enter the accuary for the result\n");
     scanf("%f", &acc);
     term = 1;
     cosx = term;
     n = 1;
     do
     {
       den = 2*n*(2*n-1);
       term = -term * x * x / den;
       cosx = cosx + term;
       n = n + 1;
       } while(acc <= fabs(cosval - cosx));
     printf("Sum of the cosine series = %f\n", cosx);
     printf("Using Library function cos(%d) = %f\n", x1,cos(x));
   } 

Find the value of sin(x)

      #include <stdio.h>
   #include <conio.h>
   #include <math.h>
   #include <stdlib.h>
   void main()
   {
     int n, x1;
     float acc, term, den, x, sinx=0, sinval;
     clrscr();
     printf("Enter the value of x (in degrees)\n");
     scanf("%f",&x);
     x1 = x;
     /* Converting degrees to radians*/
     x = x*(3.142/180.0);
     sinval = sin(x);
     printf("Enter the accuary for the result\n");
     scanf("%f", &acc);
     term = x;
     sinx = term;
     n = 1;
     do
     {
       den = 2*n*(2*n+1);
       term = -term * x * x / den;
       sinx = sinx + term;
       n = n + 1;
     } while(acc <= fabs(sinval - sinx));
     printf("Sum of the sine series = %f\n", sinx);
     printf("Using Library function sin(%d) = %f\n", x1,sin(x));
   } 

Floyd Triangle

    #include <stdio.h>
  int main()
  {
     int n, i, c, a = 1;
     printf("Enter the number of rows of Floyd\'s triangle to print:\n");
     scanf("%d", &n);
     for (i = 1; i <= n; i++)
    {
        for (c = 1; c <= i; c++)
         {
            printf("%d ",a);
            a++;
        }
         printf("\n");
    }
   return 0;
  } 

GCD LCM Using Euclids Algorithm

       #include <stdio.h>
   #include <conio.h>
   void main()
   {
     int num1, num2, gcd, lcm, remainder, numerator, denominator;
     clrscr();
     printf("Enter two numbers\n");
     scanf("%d %d", &num1,&num2);
     if (num1 > num2)
     {
       numerator = num1;
       denominator = num2;
     }
     else
     {
       numerator = num2;
       denominator = num1;
     }
     remainder = num1 % num2;
     while(remainder !=0)
     {
       numerator = denominator;
       denominator = remainder;
       remainder = numerator % denominator;
     }
     gcd = denominator;
     lcm = num1 * num2 / gcd;
     printf("GCD of %d and %d = %d \n", num1,num2,gcd);
     printf("LCM of %d and %d = %d \n", num1,num2,lcm);
   } 

Heap Sort

       #include<stdio.h>
   #include<conio.h>
   void main()
   {
     int b[10],no,i,j,c,p,temp;
     clrscr();
     printf("\n\n Enter no of elements..");
     scanf("%d",&no);
     printf(" Enter the nos..");
     for(i=0;i<no;i++)
       scanf("%d",&b[i]);
     for(i=1;i<no;i++)
     {
       c=i;
       do
       {
         p=(c-1)/2;
         if(b[p]<b[c])
         {
           temp=b[p];
           b[p]=b[c];
           b[c]=temp;
         }
         c=p;
       } while(c!=0);
     }
     for(j=no-1;j>=0;j--)
     {
       temp=b[0];
       b[0]=b[j];
       b[j]=temp;
       p=0;
       do
       {
         c=2*p+1;
         if((b[c]<b[c+1]) && c<j-1)
         c++;
         if(b[p]<b[c] && c<j)
         {
           temp=b[p];
           b[p]=b[c];
           b[c]=temp;
         }
         p=c;
       }while(c<j);
     }
     printf(" The sorted nos are..");
     for(i=0;i<no;i++)
     printf("%d",b[i]);
     getch();
   } 

Insertion Sort

       #include<stdio.h>
   void main()
   {
     int A[20], N, Temp, i, j;
     clrscr();
     printf("ENTER THE NUMBER OF TERMS...: ");
     scanf("%d", &N);
     printf("\n ENTER THE ELEMENTS OF THE ARRAY...:");
     for(i=0; i<N; i++)
     {
       gotoxy(25,11+i);
       scanf("\n\t\t%d",&A[i]);
     }
     for(i=1; i<N; i++)
     {
       Temp = A[i];
       j = i-1;
       while(Temp<A[j] && j>=0)
       {
         A[j+1] = A[j];
         j = j-1;
       }
       A[j+1] = Temp;
     }
     printf("\nTHE ASCENDING ORDER LIST IS...:\n");
     for(i=0; i<N; i++)
       printf("\n%d", A[i]);
     getch();
   } 

Interpolation Search

       #include <stdio.h>
   #include <stdlib.h>
   #define MAX 200
   int interpolation_search(int a[], int bottom, int top, int item)
   {
     int mid;
     while (bottom <= top)
     {
       mid = bottom + (top - bottom)* ((item - a[bottom]) / (a[top] - a[bottom]));
       if (item == a[mid])
         return mid + 1;
       if (item < a[mid])
         top = mid - 1;
       else
         bottom = mid + 1;
     }
     return -1;
   }
   int main()
   {
     int arr[MAX];
     int i, num;
     int item, pos;
     printf("\nEnter total elements (num < %d) : ", MAX);
     scanf("%d", &num);
     printf("Enter %d Elements : ", num);
     for (i = 0; i < num; i++)
     scanf("%d", &arr[i]);
     printf("\n ELEMENTS ARE\n: ");
     for (i = 0; i < num; i++)
     printf("%d   ", arr[i]);
     printf("\n Search For : ");
     scanf("%d", &item);
     pos = interpolation_search(&arr[0], 0, num, item);
     if (pos == -1)
       printf("\n Element %d not found \n", item);
     else
       printf("\n Element %d found at position %d \n", item, pos);
     return 0;
   } 

Inverse of the Matrix

       #include<stdio.h>
   #include<math.h>
   float detrminant(float[][], float);
   void cofactors(float[][], float);
   void trans(float[][], float[][], float);
   main()
   {
     float a[25][25], n, d;
     int i, j;
     printf("Enter the order of the matrix:\n");
     scanf("%f", &n);
     printf("Enter the elemnts into the matrix:\n");
     for (i = 0; i < n; i++)
     {
       for (j = 0; j < n; j++)
       {
         scanf("%f", &a[i][j]);
       }
     }
     d = detrminant(a, n);
     printf("\nTHE DETERMINANT IS=%2f", d);
     if (d == 0)
       printf("\nMATRIX IS NOT INVERSIBLE\n");
     else
       cofactors(a, n);
   }
   float detrminant(float a[25][25], float k)
   {
     float s = 1, det = 0, b[25][25];
     int i, j, m, n, c;
     if (k == 1)
     {
       return (a[0][0]);
     }
     else
     {
       det = 0;
       for (c = 0; c < k; c++)
       {
         m = 0;
         n = 0;
         for (i = 0; i < k; i++)
         {
           for (j = 0; j < k; j++)
           {
             b[i][j] = 0;
             if (i != 0 && j != c)
             {
               b[m][n] = a[i][j];
               if (n < (k - 2))
                 n++;
               else
               {
                 n = 0;
                 m++;
               }
             }
           }
         }
         det = det + s * (a[0][c] * detrminant(b, k - 1));
         s = -1 * s;
       }
     }
     return (det);
   }
   void cofactors(float num[25][25], float f)
   {
     float b[25][25], fac[25][25];
     int p, q, m, n, i, j;
     for (q = 0; q < f; q++)
     {
       for (p = 0; p < f; p++)
       {
         m = 0;
         n = 0;
         for (i = 0; i < f; i++)
         {
           for (j = 0; j < f; j++)
           {
             b[i][j] = 0;
             if (i != q && j != p)
             {
               b[m][n] = num[i][j];
               if (n < (f - 2))
                 n++;
               else
               {
                 n = 0;
                 m++;
               }
             }
           }
         }
         fac[q][p] = pow(-1, q + p) * detrminant(b, f - 1);
       }
     }
     trans(num, fac, f);
   }
   void trans(float num[25][25], float fac[25][25], float r)
   {
     int i, j;
     float b[25][25], inv[25][25], d;
     for (i = 0; i < r; i++)
     {
       for (j = 0; j < r; j++)
       {
         b[i][j] = fac[j][i];
       }
     }
     d = detrminant(num, r);
     inv[i][j] = 0;
     for (i = 0; i < r; i++)
     {
       for (j = 0; j < r; j++)
       {
         inv[i][j] = b[i][j] / d;
       }
     }
     printf("\nTHE INVERSE OF THE MATRIX:\n");
     for (i = 0; i < r; i++)
     {
       for (j = 0; j < r; j++)
       {
         printf("\  %2f", inv[i][j]);
       }
       printf("\n");
     }
   } 

Largest Among n digit

           #include<stdio.h>
   #include<conio.h>
   void main()
   {
     int max_num(int a[],int n);
     int max,i,n;
     int a[50];
     clrscr();
     printf("Enter n number:");
     scanf("%d",&n);
     printf("Enter the numbers:");
     for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     max=max_num(a,n);
     printf("The largest number is %d",max);
     getch();
   }
   int max_num(int a[],int n)
   {
     int i,m=0;
     for(i=0;i<n;i++)
     {
       if(a[i]>m)
         m=a[i];
     }
     return m;
   } 

LeapYear

       #include <stdio.h>
  int main()
  {
     int year;
     printf("Enter a year to check if it is a leap year\n");
     scanf("%d", &year);
     if ( year%400 == 0)
       printf("%d is a leap year.\n", year);
     else if ( year%100 == 0)
       printf("%d is not a leap year.\n", year);
     else if ( year%4 == 0 )
       printf("%d is a leap year.\n", year);
     else
       printf("%d is not a leap year.\n", year);
     return 0;
  } 
Learn C# Programming tutorial 21-30/58 Learn C# Programming tutorial 21-30/58 Reviewed by Abdul hanan on 08:29:00 Rating: 5
Powered by Blogger.