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;

}


Sunday, 29 January 2023

Java Program to find the Prime numbers using values from Terminal as input through (String [] args)

class PrimeNo
{
    public static void main(String[] args) /*String []args will take
input from the terminal(cmd command line) directly */
    {
        int n;
        for(int i=0;i<args.length;i++) /* using length function to get the total
length of the one dimensional array*/
        {
            int j=2;
            n=Integer.parseInt(args[i]); /*using parseInt function
from Integer class to convert the
numbers (which are in String) into the integer*/
            for(;j<=Math.sqrt(n);j++)
            {
                if(n%j==0)
                    break;
            }
            if(j>Math.sqrt(n))
                System.out.println(n+" = Prime Number");
            else
                System.out.println(n+" = Not Prime Number");
        }
    }
}

Sunday, 15 January 2023

Day Name Finder using C++


 

#include<iostream>    //use iostream.h in TurboC++
#include<conio.h>

using namespace std;   //comment this line in TurboC++ 

int dayofweek(int d,int m,int y)
{
static int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=m<3;
   /*if(m<3)   //this (if) line and the above line of code is same as for the functioning aspect
y-=m;*/
return(y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
int main(void)
{
int d,m,y;
cout<<"\nDay Name Finder \"Enter the below data to find the day on that date:\" ";
cout<<"\n\nEnter date: ";
cin>>d;
cout<<"\nEnter Month: ";
cin>>m;
cout<<"\nEnter Year: ";
cin>>y;
cout<<"\nThe Day on this date is ->: ";
switch(dayofweek(d,m,y))
{
case 0:cout<<" Sunday";break;
case 1:cout<<" Monday";break;
case 2:cout<<" Tuesday";break;
case 3:cout<<" Wednesday";break;
case 4:cout<<" Thursday";break;
case 5:cout<<" Friday";break;
case 6:cout<<" Saturday";break;
}
//getch();   //Use this line in TurboC++ to hold Console Screen
}

Sunday, 8 January 2023

Airline Reservation System Project in C++

 


/*(Note
: This will work on any Modern compiler is you want to use this in TurboC++ then do some necessary change needed in TurboC++ then it is good to go.)*/

#include<iostream>

#include<conio.h>

#include<fstream>

#include<stdlib.h>

#include<iomanip>


using namespace std;

class flightDetails

{

double flamt;

char flnum[100], flname[100], flfrom[200], usdestin[200], fltime[100];

int columnID;

char opt;


public:

void fldisplay();

void flgetdata();

};

flightDetails f;


void flightDetails::fldisplay()

{

fstream file2;

file2.open("flightDetails1234.txt",ios::in | ios::out | ios::binary);

if(!file2)

{

cout << "\n File Not Found!!!!";

file2.close();

return;

}

cout << "\n------------------------------------------------------------------------------------------" << endl;

cout << "\n| columnID | "<< "| Flight No |"<< "| From |"<< "| To |"<< "| Flight Time |"<< "| Amount |" << endl;


file2.seekg(0,ios::beg);

while (file2.read((char *)&f, sizeof(f)))

{

cout<< " | " <<columnID << "     | " << flnum << "     | " << flfrom << "     | " << usdestin << "     | " << fltime << "     | " << flamt << "     | " << endl;

cout<< "----------------------------------------------------------------------------------------------------------------------------------------------" << endl;

}

file2.close();

}

void flightDetails::flgetdata()

{

fstream file; //will be able to read and write into the file

int flag = 0;

columnID=1;

file.open("flightDetails1234.txt", ios::in | ios::app | ios::binary);

file.seekp(0, ios::end);

cout << "\n Enter Flight Number: ";

cin>>flnum;

cout << "\n Enter Flight Name: ";

cin>>flname;

cout << "\n Enter Flight From: ";

cin>>flfrom;

cout << "\n Enter Flight destination: ";

cin>>usdestin;

cout<< "\n Enter Flight Time (in HH:MM format): ";

cin>>fltime;

cout << "\n Enter Flight amout: ";

cin >> flamt;

cout << "\n Enter Flight Availability: (A (Available) / N (Not Available)): ";

cin >> opt;

if (opt == 'A' || opt == 'a')

{

if(file)

{

file.seekp(0,ios::end);

file.write((char *)&f,sizeof(f));

//file<<columnID<<setw(10)<<flnum<<setw(20)<<flname<<setw(20)<<left<<flfrom<<left<<usdestin<<left<<setw(20)<<fltime<<left<<setw(20)<<flamt<<endl;

flag = 1;

cout << " Successfully Added into the file...."<<endl;

}

else

{

cout << "Unable to Open / Write into the File." << endl;

}

if (flag == 1)

{

columnID += 1;

}

}

else

cout << "The Flight Is Not Available" << endl;


file.close();

fldisplay();

}

int main(void)

{

cout << "\nWelcom to Airlines Reservation System";

cout << "\n---------------------------------------------------";

f.flgetdata();

system("pause");

return 0;

}


Monday, 2 January 2023

Magic Matrices using C language

 

#include<stdio.h>

#include<conio.h>

int main(void){

    char ans;

    int a[15][15],i,j,k,l,m,x,y,n;

    do{

        ejaz:

        //clrscr();                   //this line will work in TurboC++  to clear the console

        printf("\n Enter any odd number b/n 3 to 15 to get magic matrix: ");

        scanf("%5d",&n);

        if(n%2==0||n<3||n>15)

        goto ejaz;

        for(j=(n-1)/2,k=1,i=0,l=0;l<n;l++){

            for(m=1;m<=n;i--,j--,m++,k++){

                if(j<0)

                j=n-1;

                if(m<=n&&i+1==0)

                i=n-1;

                a[i][j]=k;

            }

            i+=2;

            j++;

        }

        printf("\n\n");

        for(y=3,i=0;i<n;i++,y+=3){

            for(x=5,j=0;j<n;j++,x+=5){

               //gotoxy(x,y);                          //gotoxy can give printing location to curser in TurboC++  

               printf("%5d",a[i][j]);

            }

            printf("\n\n");                      // this line does not needed in TurboC++, the gotoxy is all it needed

        }

        printf("\n Want to continue y/n:  ");

        fflush(stdin);           // fflush(stdin) is uesd to clear the garbage from standard input stream

        ans=getchar();

    }while(ans=='y'||ans=='Y');

    //getch();                //this line will work in TurboC++  to hold screen of the console

}

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++ 

}