Saturday, 24 December 2022

Print the triangle pattern with * from 1 to n lines using Time Complexity of O(n)=n*(n+1)/2.

 

#include<stdio.h>

#include<conio.h>

int main(void)

{

    int i=1,n,t,j;

    //clrscr();    //use clrscr to clean console screen in TurboC++ 

    printf("Pick a number: \n");

    scanf("%d",&n);

    printf("Printing the triangle with * from 1 to %d line\n",n);

    for (i=0,t=1,j=1;i<(n*(n+1)/2);i++)

    {

        if(i==t*(t+1)/2)

        {

            printf("\n");

            t++;

        }

        printf("* ");

    }

    //getch();    //use getch() to hold console in TurboC++ 

}


Print Number Triangle using Time Complexity formula O(n) = n*(n+1)/2 in C language

 

#include<stdio.h>

#include<conio.h>

int main(void)

{

    int i,n,t,j;

    //clrscr();  //use clrscr to clean console screen in TurboC++ 

    printf("Pick a number: \n");

    scanf("%d",&n);

    printf("Printing the number triangle from 1 to %d \n",n);

    for (i=0,t=1,j=1;i<(n*(n+1)/2);i++)

    {

        if(i==t*(t+1)/2)

        {

            printf("\n");

            t++,j=1;

        }

        printf("%5d",j++);

    }

    //getch();   //use getch() to hold console in TurboC++ 

}


Saturday, 17 December 2022

C program to make hollow diamond pattern with alphabet

 


#include<stdio.h>

#include<conio.h>

#include<math.h>

int main(void){

    int i,j,k,m,n,t;

   // clrscr();            // clrscr(); will work in TurboC++

    printf("\n Enter your number: ");

    scanf("%d",&n);

    for(i=1-n;i<n;i++){

j=n-abs(i);

for(t=65,k=1,m=1-n;m<n;k++,t++,m++){

    if(m>0){

                k-=2;

                t-=2;

    }

    if(i>0-n && i<n){

            if(k>n-(j-1))

                printf("   ");

            else

                printf("%3c",t);

    }

}

printf("\n");

    }

    //getch();  //use this in TurboC++

}

Wednesday, 14 December 2022

Load runner program to copy file from one folder to another

char * filename ="D:\\File.txt";  // add \\ quote instead of single (\) for defining path.

char * filename2="D:\\TestFolder\\TestFile.txt";    // single (\) needed to terminate buy other single quote ( i.e. \\)
long file, FileVarriable;
char names[200];

Action()
{    
    fopen(filename, "r");    //open first filename with read only mode
     FileVarriable = fopen (filename2,"w+");  //open second filename with read and write permission mode
    
    if ((file = fopen(filename, "r")) == NULL) {
    lr_error_message ("Cannot open File = %s or File Does not exist", file);
    return -1; }
    
    while (!feof(file))
    {
        fgets(names, 200, file);  // here 200 decide the length of the contents in a line. So if you have bigger length to read, then you need to increase the length.
        
        if (!feof(file)) //if not at the end of file print names
        {
//            lr_output_message("%s",lr_eval_string(names));
            fprintf (FileVarriable, "%s"lr_eval_string(names));
        }
    }
    fclose(file);

    fclose (FileVarriable);
    return 0;
}

Note

FileVarriable– It is a variable which open file for reading/writing etc..

FileLocation– This variable keeps the file location.

fprintf function- sends formatted output to a stream.

fopen (FileLocation,”w+“)- Here w+ is the mode, which instruct the data to follow action accordingly.

“r” –Opens a file for reading. The file must exist.
“w” –Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
“a” –Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
“r+” –Opens a file to update both reading and writing. The file must exist.
“w+” –Creates an empty file for both reading and writing.
“a+” –Opens a file for reading and appending.

Monday, 12 December 2022

C Program to Convert Numeric digits into Alphabetic



#include<stdio.h>
#include<conio.h>
int main()
{
    char n[50];
    int i,c=0;
    //clrscr();             //clrscr();  will work in Turbo C ++     
    Dragon:
    printf("\n Enter your number:  ");
    scanf("%s",&n);
    for(i=0;n[i]!='\0';i++);
    if(i>19){
    printf("input should be less then or equal to 19 character");
    goto Dragon;
    }
    for(c=i,i=0;n[i]!='\0';i++,c--)
    {
if((c%2!=0 && c>=5)||c==2)
{
    switch(n[i])
    {
case'0':
    break;
case'1':
    switch(n[i+1])
    {
case'0':
    printf(" ten ");
    c--;
    i++;
    break;
case'1':
    printf(" eleven ");
    c--;
    i++;
    break;
case'2':
    printf(" twelve ");
    c--;
    i++;
    break;
case'3':
    printf(" thirteen ");
    c--;
    i++;
    break;
case'4':
    printf(" fourteen ");
    c--;
    i++;
    break;
case'5':
    printf(" fifteen ");
    c--;
    i++;
    break;
case'6':
    printf(" sixteen ");
    c--;
    i++;
    break;
case'7':
    printf(" seventeen ");
    c--;
    i++;
    break;
case'8':
    printf(" eighteen ");
    c--;
    i++;
    break;
case'9':
    printf(" nineteen ");
    c--;
    i++;
    break;
default:
    break;
    }
    break;
case'2':
    printf(" twenty ");
    break;
case'3':
    printf(" thirty ");
    break;
case'4':
    printf(" forty ");
    break;
case'5':
    printf(" fifty ");
    break;
case'6':
    printf(" sixty ");
    break;
case'7':
    printf(" seventy ");
    break;
case'8':
    printf(" eighty ");
    break;
case'9':
    printf(" ninty ");
    break;
default:
    printf("\n not a number = %c",n[i]);
    }
}
else
{
    switch(n[i])
    {
case'0':
    break;
case'1':
    printf(" one ");
    break;
case'2':
    printf(" two ");
    break;
case'3':
    printf(" three ");
    break;
case'4':
    printf(" four ");
    break;
case'5':
    printf(" five ");
    break;
case'6':
    printf(" six ");
    break;
case'7':
    printf(" seven ");
    break;
case'8':
    printf(" eight ");
    break;
case'9':
    printf(" nine ");
    break;
default:
    printf("\n not a number");
    }
}
if(n[i-1]=='0' && n[i]=='0' && n[i+1]=='0')
{
    c--;
    i++;
}
else {
switch(c)     /* c ek iniger hai*/
{
    case 3:
    if(n[i]=='0')
      break;
printf(" hundred ");
break;
    case 4:
printf(" thousent ");
break;
    case 6:
printf(" lakh ");
break;
    case 8:
printf(" cror ");
break;
case 10:
printf(" Arab ");
break;
case 12:
printf(" Kharab ");
break;
case 14:
printf(" Neel ");
break;
case 16:
printf(" Padam ");
break;
case 18:
printf(" shankh ");
break;
default:
break;
}
}
    }
   // getch();    //use getch(); in Turbo C ++
    return 0;
}