Pages

Thursday, July 14, 2011

Making Pyramid with C and C++ Program

The following program will show how to make a pyramid using C and C++. The  number of rows of the pyramid will be taken from the user and then it will be stored into a variable. Also two while loops are being used to build the pyramid.  

C and C++ pyramid program

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

void main()
{
   int i = 0, j , n ;
   clrscr();
   printf("How many rows in pyramid:");
   scanf("%d",&n);

 while(i<n)
   {
             j=0;
             while(j<=i)
             {
               gotoxy(30+j,10+i);
               printf("%d",j);
               j++;
             }
             j=0;
             while(j<=i)
             {
                gotoxy(30-j,10+i); 
                printf("%d",j);
               j++;
             }
             i++;
             printf("\n");
   }

   getch();
}

Output



Remove Directory/Folder with C and C++ Program



The following program will show how to remove a directory/folder with C and C++. This program will store directory path and name from the user and then directory will be deleted with the rmdir() function. The rmdir() function will return 0 if directory is removed successfully otherwise it will return 1.



Directory removal program with C and C++

#include<stdio.h>

#include<conio.h>
#include<process.h>
#include<dir.h>

void main()

{ 
   int cheack;
   char *dirname;
   clrscr(); 
   printf("Enter a directory path and name to be deleted (C:/name):");
   gets(dirname);
   system("dir/p");
 
   cheack = rmdir(dirname);
   if (!cheack)
      printf("Directory deleted\n");
   else
   {   
            printf("Unable to remove directory\n");
            getch();
            exit(1);
   }
  getch();
}

Program Output

Enter a directory path and name to be deleted (C:/name):C:/test

Directory deleted.


Create Directory/Folder with C/C++ Program

The following program will show how to create a directory/folder with C and C++. This program will create a folder in a specific directory path with the mkdir() function. The mkdir() function accepts an argument which is the name and path of the directory.




C and C++  program to create a directory/folder

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


void main() {
   int check;
  char *dirname;
  clrscr();
  printf("Enter a directory path and name to be created (C:/name):");
  gets(dirname);
  check = mkdir(dirname);


  if (!check)
  printf("Directory created\n");


 else
 {
   printf("Unable to create directory\n");
   exit(1);  }

  getch();
  system("dir/p");
  getch();
}


Program Output

Enter a directory path and name to be created (C:/name): C:/test.
Directory created.