Sunday, 26 February 2023

Print Alphabet Pattern Using C Program


#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

{

    int n,t,i,j,m;

    //clrscr();

    printf("\n pick a number ");

    scanf("%d",&n);

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

    {

        m=n-abs(i);

        for(j=65,t=1;t<=m;t++,j++){

            printf("%3c",j);

        }

        printf("\n");

    }

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

    return 0;

}


Prime Number By Recursion Method Using C Program

 

#include<stdio.h>
#include<conio.h>
#include<math.h>
int prime(int n, int i)
{
    if(i<=sqrt(n))
    {
        if(n%i==0)
            return(i);
        i=prime(n,i+1);     //Recursion: Function calling itself
    }
    //printf("%d",i);
    return(i);
}
int main()
{                        
    int n,t;
    char ans;
    //clrscr();
    do{
    printf("\n enter any number: ");
    scanf("%d",&n);
    t=prime(n,2);
    if(t>sqrt(n))
        printf("\n your number %d is prime number",n);
    else
        printf("\n your number %d is not prime",n);

    fflush(stdin);        //clean the garbage of standard input stream before taking input
    printf("\n Want to continue (y/n): ");
    scanf("%c",&ans);
    }while(ans=='y'||ans=='Y');
    //getch();                             //use getch() in TurboC++ to hold the console
    return 0;
}

Sunday, 12 February 2023

C Program to Delete and Replace Words form a String (Note: Give space too if you want to delete or replace a single character with no letter around it)



#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>


//using namespace std;  //Don't use this line because it is only useful for C++ programs 

int main()

{

    char n[50],m[50],o[50],ans;

    int i,j,k,ln,lm,lo,l;

//  clrscr();      //use this line in TurboC++ to clear console

    printf("\n enter your sentence: ");

    fflush(stdin);       // this function is used for clear standard input stream so garbage don't interrupt our programe 

    gets(n);          //this is function to take input string until enter is pressed

Dragon:    //this word is used to give a check point to our programe you can also use do...while() instead of this line

    printf("\n press d to delete or r to replace any string you want and x to exit program: ");

    fflush(stdin);

    ans=tolower(getchar());          //use getchar to take character input

if(ans=='d'||ans=='D')

    printf("\n Enter word you want to delete: ");

else if(ans=='r'||ans=='R')

    printf("\n Enter word you want to replace: ");

    else if(ans=='x'||ans=='X'){

    printf("\n Exiting Program............... ");

        return 0;

    }

    else{

    printf("\n Please chose one option: ");

    goto Dragon;                       //goto is used to get to the check point

    }

    fflush(stdin);

    gets(m);

    for(i=0;n[i]!='\0';i++)

    {

if(n[i]==m[0])

{

    for(k=i,j=0;m[j]!='\0';j++,k++)

if(n[k]!=m[j])

    break;

    if(m[j]=='\0')

    {

            if(ans=='d')

            {

                if(isspace(n[k]))

                k++;

                for(j=i;n[k]!='\0';j++,k++)

                n[j]=n[k];

                n[j]='\0';

            }

            else

            {

                printf("\n enter new word to replace with: ");

                fflush(stdin);

                gets(o);

                lo=strlen(o);

                for(j=i;n[k]!='\0';j++,k++)

                n[j]=n[k];

                n[j]='\0';

                for(ln=strlen(n);ln>=i;ln--)

                n[ln+lo]=n[ln];

                for(j=0,k=i;o[j]!='\0';k++,j++)

                n[k]=o[j];

            }

    }

}

    }

    printf("\n yor new sentence: %s",n);

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


    return 0;

}


 

Saturday, 4 February 2023

Defining functions inside class and demonstration (how to use it in C++).....(eg. input, output, merge, linear sort, bubble sort, sequential sort, binary sort)

 

#include<iostream>   //put <iostream.h> to compile in TurboC++

#include<conio.h>


using namespace std;  //this (namespace std) only work in modern compiler not in Turb0C++


class Dragon

{ /*this is a class named as Dragon which has following functions

these functions can be used anywhere by using the class name within Same Script

or in Same Directory.

Note: To use this in any other directory you need to import this file*/

int *a;

int n;

public:

Dragon(int t)    // constructor

{

n=t;

a=new int[n];

}

void getdata();      //function to take input

void putdata();      //function to print output data

void mergobj(Dragon &,Dragon &);     // function merge two objects

void lsort();        //function of linear sorting

void bubsort();      //function of bubble sorting

void seqser();       //function of sequential search

void biser();        //function of binary search

};


void Dragon::getdata()     //Defining the Function followed by Class name

{

cout<<"\n enter "<<n<<" values: ";

for(int i=0;i<n;i++)

cin>>a[i];

}


void Dragon::putdata()     //Defining the Function followed by Class name

{

cout<<"\n Puting "<<n<<" Values: ";

for(int i=0;i<n;i++)

cout<<" "<<a[i];

}


void Dragon::mergobj(Dragon &t1,Dragon &t2)//Defining the Function followed by Class name

{

int i;

for(i=0;i<t1.n;i++)

a[i]=t1.a[i];

for(i=0;i<t2.n;i++)

a[t1.n+i]=t2.a[i];

}


void Dragon::lsort()     //Defining the Function followed by Class name

{

int i,j,t;

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

{

for(j=i+1;j<n;j++)

{

if(*(a+i)>*(a+j))

{

t=*(a+i);

*(a+i)=*(a+j);

*(a+j)=t;

}

}

}

}


void Dragon::bubsort()     //Defining the Function followed by Class name

{

int i,j,t;

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

{

for(j=0;j<n-i-1;j++)

{

if(a[j]>a[j+1])

{

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

}

}


void Dragon::seqser()     //Defining the Function followed by Class name

{

int num,i;

cout<<"\n enter any number to search: ";

cin>>num;

putdata();

for(i=0;i<n;i++)

if(*(a+i)==num)

break;

if(i==n)

{

cout<<"\n that's a wrong number";

return;

}

cout<<"\n your number "<<*(a+i)<<" is in this the array on psition: "

<<i+1;

}


void Dragon::biser()     //Defining the Function followed by Class name

{

int num;

cout<<"\n enter any number to search: ";

cin>>num;

bubsort();

putdata();

int i=n/2,j=0,k=n-1;

while(j<k)

{

if(*(a+i)==num)

{

cout<<"\n found your number "<<a[i]<<"\n on position "<<i+1;

return;

}

if(*(a+i)>num)

k=i-1;

else

j=i+1;

i=(j+k)/2;


}

if(j==k)

cout<<"\n Wrong number";

}


int main() //This is Main function

{

int t1,t2;

//clrscr();

cout<<"\n enter size of the object 1: ";

cin>>t1;

Dragon obj1(t1);

cout<<"\n enter size of object 2: ";

cin>>t2;

Dragon obj2(t2);

Dragon obj3(t1+t2);

obj1.getdata();

obj2.getdata();

obj3.mergobj(obj1,obj2);

cout<<"\n details of obj 1";

obj1.putdata();

cout<<"\n details of obj 2";

obj2.putdata();

cout<<"\n after obj 1, obj 2 the details of obj 3: ";

obj3.putdata();

obj3.lsort();

cout<<"\n details of ob3 after sorting";

obj3.putdata();

obj3.bubsort();

cout<<"\n after sorting in bubble method obj3 is";

obj3.putdata();

cout<<"\nFor Object1 1:";

obj1.seqser();

    cout<<"\nFor Object1 2:";

obj2.biser();

cout<<"\nFor Object1 3 (which is merged from obj1 and obj2):";

obj3.biser();

//getch();

return 0;

}