Learn C# Programming tutorial 1-10/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. 

  • Add Complex Numbers
  • Add without ADD Operator
  • Armstrong Number
  • Binary Search
  • Bubble Sort
  • Bucket Sort
  • Celsius To Fahrenheit
  • Character is Vowel or not
  • Combinations and Permutations
  • Convert Binary to Decimal,Octal,Hexadecimal




Name Code

Add Complex Numbers

 #include <stdio.h>
  #include <conio.h>
  #include <stdlib.h>
  struct complex
  {
      int real;
      int img;
  };
  int main()
  {
      struct complex a, b, c;
      printf(" Enter a and b where a + ib is the first complex number. ");
      printf("\n a = ");
      scanf(" %d ", &a.real);
      printf(" b = ");
      scanf(" %d ", &a.img);
      printf(" Enter c and d where c + id is the second complex number. ");
      printf("\n c = ");
      scanf(" %d ", &b.real);
      printf(" d = ");
      scanf(" %d ", &b.img);
      c.real = a.real + b.real;
      c.img = a.img + b.img;
      if ( c.img >= 0 )
          printf(" Sum of two complex numbers = %d + %di ", c.real, c.img);
      else
          printf(" Sum of two complex numbers = %d %di ", c.real, c.img);
      getch();
      return 0;
  } 

Add without ADD Operator

     #include<stdio.h>
  int main()
  {
      int a,b;
      int sum;
      printf("Enter any two integers: ");
      scanf("%d%d",&a,&b);
      //sum = a - (-b);
      sum = a - ~b -1;
      printf("Sum of two integers:%d",sum);
      return 0;
  } 

Armstrong Number

 void main()
 {
     int n,b=0,t;
     clrscr();
     printf("Enter the no");
     scanf("%d",&n);
     t=n;
     while(n>0)
     {
             a=n%10;
             b=b+a*a*a;
             n=n/10;
     }
     if(b==t)
     {
             printf("Armstrong no");
     }
      else
     {
             printf("Not an armstrong no");
     }
     getch();
 } 

Binary Search

 int BinarySearch(int *array, int number_of_elements, int key)
 {
      int low = 0, high = number_of_elements-1, mid;
      while(low <= high)
      {
          mid = (low + high)/2;
          if(array[mid] < key)
          {
                  low = mid + 1;
          }
          else if(array[mid] == key)
          {
                  return mid;
          }
          else if(array[mid] > key)
          {
                  high = mid-1;
          }
      }
      return -1;
 }
 int main()
 {
      int number_of_elements;
      scanf("%d",&number_of_elements);
      int array[number_of_elements];
      int iter;
      for(iter = 1;iter < number_of_elements;iter++)
      {
          if(array[iter]< array[iter - 1])
          {
                 printf("Given input is \n not sorted\n");
 return 0;
          }
      }
      int key;
      scanf("%d",&key);
/* Calling this functions searches for the key and returns its index. It returns -1 if key is not found.*/
      int index;
      index = BinarySearch(array,number_of_elements,key);
     if(index==-1)
     {
              printf("Element not found\n");
     }
     else
     {
             printf("Element is at index %d\n ",index);
     }
     return 0;
 } 

Bubble Sort

#include <stdio.h>
 void bubble_sort(long [], long);
 int main()
 {
      long array[100], n, c, d, swap;
      printf("Enter number of elements:");
      scanf("%ld", &n);
      printf("Enter %ld longegers\n", n);
      for (c = 0; c < n; c++)
      scanf("%ld", &array[c]);
      bubble_sort(array, n);
      printf("Sorted list in ascending order:n");
      for ( c = 0 ; c < n ; c++ )
      printf("%ld\n", array[c]);
      return 0;
 }
 void bubble_sort(long list[], long n)
 {
      long c, d, t;
      for (c = 0 ; c < ( n - 1 ); c++)
       {
           for (d = 0 ; d < n - c - 1; d++)
            {
                if (list[d] > list[d+1])
                 {
                      t = list[d];
                      list[d] = list[d+1];
                      list[d+1]= t;
                }
           }
      }
 } 

Bucket Sort

     #include<stdio.h>
  void Bucket_Sort(int array[], int n)
  {
      int i, j;
      int count[n];
      for(i=0; i < n; i++)
      {
          count[i] = 0;
      }
      for(i=0; i < n; i++)
      {
          (count[array[i]])++;
      }
      for(i=0,j=0; i < n; i++)
      {
          for(; count[i]>0;(count[i])--)
          {
              array[j++] = i;
          }
      }
  }
  int main()
  {
      int array[100];
      int num;
      int i;
      printf("Enter How many Numbers : ");
      scanf("%d",&num);
      printf("Enter the %d elements to be sorted:\n",num);
      for(i = 0; i < num; i++ )
      {
          scanf("%d",&array[i]);
      }
      printf("\n The array of elements before sorting : \n");
      for (i = 0;i < num;i++)
      {
          printf("%d ", array[i]);
      }
      printf("\n The array of elements after sorting : \n");
      Bucket_Sort(array, num);
      for (i = 0;i < n;i++)
      {
          printf("%d ", array[i]);
      }
      printf("\n");
      return 0;
  } 

Celsius To Fahrenheit

   #include <stdio.h>
  #include <conio.h>
  void main()
  {
      float c, f;
      clrscr();
      printf(" Enter temp in centigrade: ");
      scanf("%f",&c);
      f = ( 1.8 * c ) + 32;
      printf(" Temperature in Fahrenheit = %f", f);
      getch();
  } 

Character is Vowel or not

    #include <stdio.h>
  main()
  {
      char ch;
      printf("Enter a character\n");
      scanf("%c", &ch);
      if (ch == \'a\' || ch == \'A\' || ch == \'e\' || ch == \'E\' || ch == \'i\' || ch == \'I\' || ch ==\'o\' || ch==\'O\' || ch == \'u\' || ch == \'U\')
          printf("%c is a vowel.\n", ch);
      else
          printf("%c is not a vowel.\n", ch);
      return 0;
  } 

Combinations and Permutations

    #include <stdio.h>
  #include <conio.h>
  main()
  {
      int n , r, ncr( int , int);
      long npr( int , int);
      long double fact( int);
      printf(" Enter value of n & r \n");
      scanf("%d %d",&n , &r);
      if( n>= r)
      {
          printf( "%d C %d is %d \n", n,r,ncr( n , r));
          printf("%d P %d is %ld", n,r,npr( n, r));
      }
      else
      {
          printf("WRONG INPUT?? enter the correct input");
      }
  }
  long double fact( int p)
  {
      long double facts = 1;
      int i;
      for( i = 1; i<= p; i++)
      facts = facts * i;
      return( facts);
  }
  int ncr ( int n, int r)
  {
      return( fact( n) / (fact( r) * fact(n- r) ) ) ;
  }
  long npr( int n , int r)
  {
      return( fact( n) / fact( n- r));
  } 

Convert Binary to Decimal,Octal,Hexadecimal

        #include<stdio.h>
   #include<string.h>
   void hexadecimal();
   void main()
   {
     int num, bnum, dec = 0, base = 1, rem ,dec1=0,oct[25],dec2=0,flag=0,i=0,counter=0,j;
     printf("Enter the binary number(1s and 0s)\n");
     scanf("%d", &num);
     bnum = num;
     while( num > 0)
     {
       rem = num % 10;
       if((rem==0) || (rem==1))
       {
         dec = dec + rem * base;
         num = num / 10 ;
         base = base * 2;
         flag=1;
       }
       else
       {
         flag=0;
         printf("\n Enter binary number \n");
         break;
       }
     }
     if(flag==1)
     {
       printf("The Binary number is = %d\n", bnum);
       printf("Its decimal equivalent is =%d\n", dec);
       dec1=dec;
       dec2=dec1;
       while(dec>0)
       {
         rem=dec%8;
         oct[i]=rem;
         dec=dec/8;
         i++;
         counter++;
       }
       counter--;
       printf("\n Its octal equivalent is:");
       while(counter>=0)
       {
         printf("%d" ,oct[counter]);
         counter--;
       }
       printf("\nIts Hexa Decimal equivalant is: ");
       hexadecimal(dec2);
     }
   }
   void hexadecimal(long n)
   {
     long i;
     if(n>0)
     {
       i=n%16;
       n=n/16;
       hexadecimal(n);
       if(i>=10)
       {
         switch(i)
         {
           case 10:
             printf("A");
             break;
           case 11:
             printf("B");
             break;
           case 12:
             printf("C");
             break;
           case 13:
             printf("D");
             break;
           case 14:
             printf("E");
             break;
           case 15:
             printf("F");
             break;
         }
       }
       else
         printf("%ld",i);
     }
   }   
Learn C# Programming tutorial 1-10/58 Learn C# Programming tutorial 1-10/58 Reviewed by Abdul hanan on 04:25:00 Rating: 5
Powered by Blogger.