Recent Posts

Pages: 1 ... 7 8 [9] 10
81
Turbo C Programming / Turbo C Binary Number Addition without Array
« Last post by kathir on April 02, 2012, 01:32:27 pm »
Here is the Turbo C program to add two binary numbers without using array.

Code: [Select]
#include <stdio.h>
#include <conio.h>

#define NUM_BINARY_DIGITS 16

int AddBinaryNumbers(int number1, int number2)
{
   int i, c = 0, sum = 0;
   int a, b;
   int factor = 1;

   for(i = 0; i < NUM_BINARY_DIGITS; i++)
   {
      a = (number1 % 10) ? 1 : 0;
      b = (number2 % 10) ? 1 : 0;
      number1 /= 10;
      number2 /= 10;
      sum += ((a ^ b) ^ c) * factor;
      factor *= 10;
      c = ((a & b) | (b &c)) | (b & c);
   }
   if(c > 0)
      sum += factor;
   
   return sum;
}

void main()
{
   printf("Program for Binary Number Addition\n");
   
   printf("Number First Number:");
   scanf("%d", &x);

   printf("Number Second Number:");
   scanf("%d", &y);

   z = AddBinaryNumbers(x,y);

   printf("Sum = %d\n", z);
}


Program for Binary Number Addition
Number First Number:10101010
Number Second Number:1
Sum = 10101011
82
Turbo C Programming / Turbo C Program for series: 1+(1/2)^2+(1/3)^3+(1/4)^4+...
« Last post by kathir on March 16, 2012, 04:11:27 am »
Here is the Turbo C Program for series: 1+(1/2)^2+(1/3)^3+(1/4)^4+...

Code: [Select]
#include <stdio.h>
#include <math.h>

void main()
{
   int i, n;
   float sum = 0, term = 0;
   printf("Program for series: 1+(1/2)^2+(1/3)^3+(1/4)^4+...");
   printf("\nEnter number: ");
   scanf("%d", &n);
   
   for(i = 1; i <= n; i++)
   {
      term = 1.0 / i;
      term = pow(term, i);
      sum = sum + term;
      printf("\nTerm: %-5d Value = %.4f Sum = %.6f", i, term, sum);
   }

   printf("\n\n");
}

output:

Program for series: 1+(1/2)^2+(1/3)^3+(1/4)^4+...
Enter number: 5

Term: 1     Value = 1.0000 Sum = 1.000000
Term: 2     Value = 0.2500 Sum = 1.250000
Term: 3     Value = 0.0370 Sum = 1.287037
Term: 4     Value = 0.0039 Sum = 1.290943
Term: 5     Value = 0.0003 Sum = 1.291263

Press any key to continue . . .
83
Turbo C Programming / Turbo C Program for series: 1/2+2/3+3/4+4/5.........n/n+1
« Last post by kathir on March 16, 2012, 04:04:24 am »
Here is the Turbo C Program for series:  1/2+2/3+3/4+4/5.........n/n+1

Code: [Select]
#include <stdio.h>

void main()
{
   int i, n;
   float sum = 0, term = 0;
   printf("Program for series:  1/2+2/3+3/4+4/5.........n/n+1");
   printf("\nEnter number: ");
   scanf("%d", &n);
   
   for(i = 1; i <= n; i++)
   {
      term = (float) i / (i+1);
      sum = sum + term;
      printf("\nTerm: %-5d Value = %.4f Sum = %.4f", i, term, sum);
   }
   printf("\n\n");
}


output

Program for series:  1/2+2/3+3/4+4/5.........n/n+1
Enter number: 10

Term: 1     Value = 0.5000 Sum = 0.5000
Term: 2     Value = 0.6667 Sum = 1.1667
Term: 3     Value = 0.7500 Sum = 1.9167
Term: 4     Value = 0.8000 Sum = 2.7167
Term: 5     Value = 0.8333 Sum = 3.5500
Term: 6     Value = 0.8571 Sum = 4.4071
Term: 7     Value = 0.8750 Sum = 5.2821
Term: 8     Value = 0.8889 Sum = 6.1710
Term: 9     Value = 0.9000 Sum = 7.0710
Term: 10    Value = 0.9091 Sum = 7.9801

Press any key to continue . . .
84
Turbo C Programming / Turbo C Program for sum of factorial numbers
« Last post by kathir on March 16, 2012, 03:58:04 am »
Here is the Turbo C Program for sum of factorial numbers

Code: [Select]

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

int Fact(int n)
{
   if( n <= 1)
      return 1;
   return n * Fact(n - 1);
}


void main()
{
   int i, n;
   int sum = 0;
   printf("Enter number: ");
   scanf("%d", &n);
   
   for(i = 1; i <= n; i++)
   {
      sum = sum + Fact(i);
      printf("\nTerm: %-5d Value = %-5d Sum = %-5d", i, Fact(i), sum);
   }
   printf("\n\n");
}

output:

Enter number: 5

Term: 1 Value = 1     Sum = 1
Term: 2 Value = 2     Sum = 3
Term: 3 Value = 6     Sum = 9
Term: 4 Value = 24    Sum = 33
Term: 5 Value = 120   Sum = 153

Press any key to continue . . .
85
Here is the Turbo C Program to calculate hypotenuse of right angle triangle:
Code: [Select]
#include <stdio.h>
#include <math.h>

void main()
{

   float a, b, c;

   printf("Enter Triangle Side A: ");
   scanf("%f", &a);
   printf("Enter Triangle Side B: ");
   scanf("%f", &b);

   c = sqrt(a * a + b * b);
   printf("Hypotenuse: %.4f", c);
}
86
Here is Turbo C Program for X series and N terms value for the series: Y = 1 + 1 / x + 1 / x^2 + 1 / x^3 + 1 / x^4 + ...

Code: [Select]
#include <stdio.h>
#include <conio.h>

void main()
{
   int i;
   float x, xterm;
   int numterms = 5;
   float sum = 1;
   printf("Enter number X: ");
   scanf("%f", &x);
   xterm = x;
   for(i = 1; i <= numterms; i++)
   {
      sum = sum + 1.0 / xterm;
      xterm = xterm * x;
      printf("\nTerm: %d Sum = %.4f", i, sum);
   }
   printf("\n\n");
   return;
}

Output:
Enter number X: 5

Term: 1 Sum = 1.2000
Term: 2 Sum = 1.2400
Term: 3 Sum = 1.2480
Term: 4 Sum = 1.2496
Term: 5 Sum = 1.2499

Press any key to continue . . .
87
Turbo C Programming / Turbo C Program to Rotating Number Pattern 1 - 25
« Last post by kathir on March 08, 2012, 12:22:11 pm »
Here is the Turbo C Program for Rotating Number Pattern 1 - 25


Code: [Select]

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


void main()
{
   int i = 1;
   int x = 10;
   int y = 10;

   for(i = 1; i <= 25; i++)
   {
      if(i <= 5)
         x += 3;
      else if(i <= 9)
         y += 1;
      else if(i <= 13)
         x -= 3;
      else if(i <= 16)
         y--;
      else if(i <= 19)
         x += 3;
      else if(i <= 21)
         y += 1;
      else if(i <= 23)
         x -= 3;
      else if(i <= 24)
         y--;
      else if(i <= 25)
         x += 3;
      gotoxy(x, y);
      printf("%-2d", i);
   }

   gotoxy(x, y + 5);
   return;
}

88
Visual C++ Programming / Visual C++ function to load and display bitmap from resource
« Last post by kathir on February 21, 2012, 01:06:39 pm »
Here is the Visual C++ function to load and display bitmap from resource.

Code: [Select]
   void DisplayBitmap(CDC *pDC, UINT nResourceID, int left, int top, int right, int bottom)
   {     
      CBitmap bitmap;
      bitmap.m_hObject=(HBITMAP)::LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(nResourceID));

      CDC *bitmapDC = new CDC();
      bitmapDC->CreateCompatibleDC(pDC);

      bitmapDC->SelectObject(bitmap);
      pDC->BitBlt(left, top, right, bottom,bitmapDC,0,0,SRCCOPY);

      delete bitmapDC;
   }
89
Visual C++ Programming / Visual C++ function for displaying Bitmap using CDC
« Last post by kathir on February 21, 2012, 12:41:02 pm »
Here is the Visual C++ function for displaying Bitmap using CDC:

Code: [Select]

void DisplayImage(CDC *pDC, const char *filename, int left, int top, int right, int bottom)
   {
      int height = bottom - top;
      int width = right - left;

      CBitmap bitmap;
      bitmap.m_hObject=(HBITMAP)::LoadImage(NULL, filename, IMAGE_BITMAP, width, height, LR_LOADFROMFILE);

      CDC *bitmapDC = new CDC();
      bitmapDC->CreateCompatibleDC(pDC);

      bitmapDC->SelectObject(bitmap);
      pDC->BitBlt(left, top, right, top + 20,bitmapDC,0,0,SRCCOPY);

      delete bitmapDC;

   }



90
Turbo C Programming / Turbo C Program for displaying pattern of Coke using Star
« Last post by kathir on February 21, 2012, 12:33:11 pm »
Here is the Turbo C Program for displaying pattern of Coke using Star.



Code: [Select]

#include <stdio.h>
#include <string.h>

int main()
{
   static int pattern[] = { 3, 3, 5, 7, 7, 7, 5, 5, 7, 7, 7, 5 };
   int i = 0, j = 0;
   printf("Program for displaying pattern of Coke Star\n\n");
   
   for(i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++)
   {
      printf("%10s", " ");
      if(i < 2)
         printf("%3s", " ");
      else if(i < 3)
         printf("%2s", " ");
      else if(i < 6)
         printf("%1s", " ");
      else if(i < 8)
         printf("%2s", " ");
      else if(i < 11)
         printf("%1s", " ");
      else
         printf("%2s", " ");

      for(j = 0; j < pattern[i]; j++)
      {
         printf("*");
      }

      printf("\n");
   }

   getch();

   return 0;
}

Pages: 1 ... 7 8 [9] 10

Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1