Recent Posts

Pages: 1 ... 8 9 [10]
91
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;
}

92
Turbo C Programming / Turbo C Program for displaying number of Vowels in a line
« Last post by kathir on February 21, 2012, 12:09:17 pm »
Here is the Turbo C Program for displaying number of Vowels in a line:


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

void main()
{
   int i, j, len;
   int count = 0;
   int isVowel = 0;
   char buffer[512];
   static char v[] = { 'a', 'e', 'i', 'o', 'u',
                       'A', 'E', 'I', 'O', 'U',
                     };
   printf("Enter a String: ");
   gets(buffer);

   len = strlen(buffer);

   for(i = 0; i < len; i++)
   {
      isVowel = 0;
      for(j = 0; j < sizeof(v) / sizeof(v[0]); j++)
      {
         if(buffer[i] == v[j])
         {
            isVowel = 1;
            break;
         }
      }
      if(isVowel == 1)
         count++;
   }
   printf("\nNumber of vowels in the given string is: %d", count);
   getch();
   return;
   
}

Output:

Enter a String: www.softwareandfinance.com
Number of vowels in the given string is: 8
93
I have given here a Turbo C Program for displaying number of consonants in a statement:


Code: [Select]

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

void main()
{
   int i, j, len;
   int count = 0;
   int isVowel = 0;
   char buffer[512];
   static char v[] = { 'a', 'e', 'i', 'o', 'u',
                       'A', 'E', 'I', 'O', 'U',
                     };
   printf("Enter a String: ");
   gets(buffer);

   len = strlen(buffer);

   for(i = 0; i < len; i++)
   {
      isVowel = 0;
      for(j = 0; j < sizeof(v) / sizeof(v[0]); j++)
      {
         if(buffer[i] == v[j])
         {
            isVowel = 1;
            break;
         }
      }
      if(isVowel == 0)
         count++;
   }
   printf("\nNumber of consonants in the given string is: %d", count);
   getch();
   return;
   
}


output:
Enter a string: www.softwareandfinance.com
Number of consonants in the given string is: 18
94
Turbo C Programming / Turbo C Program for (a + b) ^ 2
« Last post by kathir on February 10, 2012, 06:36:11 pm »
Here is the turbo c program for (a + b) ^ 2.

Code: [Select]

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

void main()
{
     int a, b, result;
     printf("Enter A: ");
     scanf("%d", &a);
     printf("Enter B: ");
     scanf("%d", &b);

     result = (a+b) * (a+b);

    printf("The answer is: %d", result);

}



Sample Output:

Enter A: 5
Enter B: 3
The answer is: 64
95
Java Programming / Java Program to display abcd in a diamond shape pattern
« Last post by kathir on February 10, 2012, 06:28:15 pm »
Here is the Java Program to display abcd in a diamond shape pattern:

      A
    B C D
  E F G H I
J K L M N O P
  Q R S T U
    V W X
      Y



Code: (java) [Select]
import java.lang.*;
import java.util.*;
import java.io.*;
 
 
class DisplayABC
{
     
      public static void main(String[] args)
      {
           System.out.println("Display ABC in diamond shape: ");
           int [] shape = new int [] { 1, 3, 5, 7, 5, 3, 1};
           int [] space = new int [] { 6, 4, 2, 0, 2, 4, 6};
           int count = 0;
           int rowindex = 0;
           for(int i = 0; i < 26; i++)
           {
                if(count == 0)
                {
                  int spacecount = space[rowindex];
                  for(int j = 0; j < spacecount; j++)
                     System.out.print(" ");
                } 
                System.out.format("%1$-2c", (char) (i + 'A') );
                count++;
                if(shape[rowindex] == count)
                {
                  System.out.println();
                  rowindex++;
                  count = 0;
                }
           }
      }
}

96
Turbo C Programming / Turbo C Graphics Program for Bar Chart
« Last post by kathir on February 10, 2012, 12:28:39 pm »
Here is the Turbo C Graphics program for bar chart.



Code: [Select]
#include <stdio.h>
#include <string.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <math.h>

typedef struct _PieInfo
{
    char desc[128];
    double percentage;
    int color;
} BarInfo;

void Fill_BarInfo(BarInfo *p, const char *des, double percent, int clr)
{
    strcpy(p->desc, des);
    p->percentage = percent;
    p->color = clr;
}

extern BarInfo theBarInfo[32];
extern int theSliceCount;

BarInfo theBarInfo[32];
int theSliceCount = 0;


void main()
{
    const int SPACE_BAR = 40;
    const int BAR_WIDTH = 20;
    const double MAX_PERCENTAGE = 50.0;
    int grd, grm;
    int xmax, ymax;
    int left, top, right, bottom;
    double percent = 0;
    double x, y;
    int i,j,k;
    double xpos,ypos;
    char buf[128];


    detectgraph(&grd,&grm);
    initgraph(&grd, &grm, "");

    setbkcolor(BLUE);

    setcolor(WHITE);
    left = 40;
    top = 40;
    right =  getmaxy() - 20;
    bottom = getmaxy() - 60;

    xmax = right - left;
    ymax = bottom - top;

    rectangle(left, top, right, bottom);
    for(j = 0; j <= MAX_PERCENTAGE; j += 10)
    {
ypos = j / MAX_PERCENTAGE * ymax;
setlinestyle(DASHED_LINE, 1, 1);
line(left, bottom - ypos, right, bottom - ypos);
sprintf(buf, "%d%%", j);
outtextxy(left - 30, bottom - ypos - 5, buf);
    }
    setlinestyle(SOLID_LINE, 1, 1);

    Fill_BarInfo(&theBarInfo[0], "Gold", 20, RED);
    Fill_BarInfo(&theBarInfo[1], "Stocks", 15, GREEN);
    Fill_BarInfo(&theBarInfo[2], "Bonds", 35, CYAN);
    Fill_BarInfo(&theBarInfo[3], "ETFs", 15, MAGENTA);
    Fill_BarInfo(&theBarInfo[4], "Oprions", 7.5, YELLOW);
    Fill_BarInfo(&theBarInfo[5], "Cash", 7.5, LIGHTRED);
    theSliceCount = 6;

    xpos = left + SPACE_BAR;
    for(i = 0; i < theSliceCount; i++)
    {
percent = theBarInfo[i].percentage;
if(percent > MAX_PERCENTAGE)
    percent = MAX_PERCENTAGE;
setfillstyle(SOLID_FILL, theBarInfo[i].color);
ypos = percent / MAX_PERCENTAGE * ymax;
bar(xpos, bottom, xpos + BAR_WIDTH, bottom - ypos);
rectangle(xpos, bottom, xpos + BAR_WIDTH, bottom - ypos);

sprintf(buf, "%3.0lf%%", theBarInfo[i].percentage);
outtextxy(xpos - 5, bottom - ypos - 15, buf);

sprintf(buf, "%s", theBarInfo[i].desc);
outtextxy(xpos - 5, bottom + 15, buf);
xpos += (SPACE_BAR + BAR_WIDTH);
    }

    xpos = right + SPACE_BAR;
    for(k = 0; k < theSliceCount; k++)
    {
rectangle(xpos, ypos, xpos + 30, ypos + 25);
setfillstyle(SOLID_FILL,theBarInfo[k].color);
floodfill((xpos + xpos + 30) / 2, (ypos + ypos + 25) / 2, WHITE);
sprintf(buf, "%3.0lf%% %s", theBarInfo[k].percentage, theBarInfo[k].desc);
outtextxy(xpos + 35, ypos + 10, buf);
ypos += 35;
    }


    getch();
    closegraph();
}

97
I have given here a simple Turbo C Program to enumerate all sub directories and files in a folder.

It uses dirend.h header file and opendir, readdir and closedir functions to get the list of files and folders.

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

int Display_All_Directories(char *searchstring)
{
   int count = 0;
   DIR *directory_list;
   struct dirent *dentry;
   char buf[128];
   directory_list = opendir (searchstring);
   if (directory_list != NULL) {
      while ((dentry = readdir (directory_list)) != NULL) {
strcpy(buf,searchstring);
strcat(buf, dentry->d_name);
if(opendir(buf) != NULL)
{
printf ("Dir: %s\n", dentry->d_name);
count++;
}
      }
      closedir (directory_list);
   } else {
      printf("could not open directory");
   }
   return count;
}

int Display_All_Files(char *searchstring)
{
   DIR *directory_list;
   int count = 0;
   struct dirent *dentry;
   char buf[128];
   directory_list = opendir (searchstring);
   if (directory_list != NULL) {
      while ((dentry = readdir (directory_list)) != NULL) {
strcpy(buf,searchstring);
strcat(buf, dentry->d_name);
if(opendir(buf) == NULL)
{
printf ("File: %s\n", dentry->d_name);
count++;
}
      }
      closedir (directory_list);
   } else {
      printf("could not open directory");
   }
   return count;
}

void main()
{
   char searchstring[128] = "c:\\";
   int count = 0;

   count = Display_All_Directories(searchstring);
   printf("\n%d directories found\n\n", count);
   getch();

   count = Display_All_Files(searchstring);
   printf("\n%d files found\n\n", count);
   getch();
}

/***
The output would the list of sub directories and files in C:\ directory.
At the end it will display how many sub directories and files are there in the current directory.
***/

98
Turbo C Programming / Turbo C Program to enumerate all sub directories in a directory
« Last post by kathir on February 08, 2012, 02:02:39 pm »
Here is the simple Turbo C Program to enumerate all sub directories in a directory (folder).

Code: [Select]

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

int Display_All_Directories(char *searchstring)
{
   int count = 0;
   DIR *directory_list;
   struct dirent *dentry;
   char buf[128];
   directory_list = opendir (searchstring);
   if (directory_list != NULL) {
      while ((dentry = readdir (directory_list)) != NULL) {
strcpy(buf,searchstring);
strcat(buf, dentry->d_name);
if(opendir(buf) != NULL)
{
printf ("Dir: %s\n", dentry->d_name);
count++;
}
      }
      closedir (directory_list);
   } else {
      printf("could not open directory");
   }
   return count;
}

void main()
{
   char searchstring[128] = "c:\\";
   int count = 0;

   count = Display_All_Directories(searchstring);
   printf("\n%d directories found\n\n", count);
   getch();
}

/***
The output would the list of all sub directories under C:\ directory.
At the end it will display how many sub directories are there in the current directory.
***/

99
Turbo C Programming / Turbo C Program to list all files in a folder
« Last post by kathir on February 08, 2012, 01:47:24 pm »
I have given here Turbo C program to list all files in a folder:

Code: [Select]

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

int Display_All_Files(char *searchstring)
{
   DIR *directory_list;
   int count = 0;
   struct dirent *dentry;
   char buf[128];
   directory_list = opendir (searchstring);
   if (directory_list != NULL) {
      while ((dentry = readdir (directory_list)) != NULL) {
strcpy(buf,searchstring);
strcat(buf, dentry->d_name);
if(opendir(buf) == NULL)
{
printf ("File: %s\n", dentry->d_name);
count++;
}
      }
      closedir (directory_list);
   } else {
      printf("could not open directory");
   }
   return count;
}

void main()
{
   char searchstring[128] = "c:\\";
   int count = 0;
   count = Display_All_Files(searchstring);
   printf("\n%d files found\n\n", count);
   getch();
}

/***
The output would the list of files in C:\ directory. At the end it will display how many files are there in the current directory.
***/


100
Turbo C Programming / How to access long filenames or folders in MS-DOS?
« Last post by kathir on February 08, 2012, 12:44:33 pm »
Usually DOS format supports 8.3 file format.

You may see the folder name c:\program files on each windows operating system.

If you are using MS-DOS prompt (under 16 bit), the folder names will be truncated into 8 characters - that is progra~1.

In case, if you have two folders like given below:

c:\program files
c:\program data

then MS-DOS format would be the following:

c:\progra~1
c:\progra~2

The same logic goes for filenames also. This restriction in only if you are working with 16 bit applications.
Pages: 1 ... 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