Sunday, March 10, 2024

Basic Trubo C++ programs

/*

A C++ program to accept ten numbers in ascending order using array.

and search for a number given by user using binary search method.

*/


#include<iostream.h>

#include<conio.h>


void main()

{ clrscr();


 //Declare all variables.

 int arr[10], i, initial, final, mid, data;


 cout<<"Enter 10 numbers in ascending order ";


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

 {

   cin >> arr[i];

 }


 cout<<"\nEnter the data to be searched : ";


 cin >> data;


 initial = 0;


 final = 9;


 mid = (initial + final)/2;


 while( initial <= final) && (arr[mid] != data))

 {

if( arr[mid] > data)

final = mid - 1;

else

initial = mid + 1;

 }


 if( arr[mid] == data)

cout<<"Data is present";


 if(initial > final)

cout<<"Data is not present in the list";


 getch();


}


---------------------------------------------------------------------------------------------------------------


/*

A C++ program to accept 10 numbers in an array and sort the numbers

in ascending order using Bubble sort method. Practical 15.

*/


#include<iostream.h>

#include<conio.h>


void main()

{ clrsrc();


 //Declare all variables.

 int arr[10], i, j , temp;


 cout<<"Enter 10 numbers ";


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

cin>> arr[i];


 //Bubble sort.

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

 { 

for(j = 0; j<10; j++)

    {

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

{

//swap the two.

temp = arr[j];

arr[j] = arr[j+1];


   arr[j+1] = temp;

}

}

 }


 cout<<"The sorted array is \n";


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

cout<< arr[i]<<" ";


 getch();


}


-------------------------------------------------------------------------------------------------------------

/*

A C++ program to accept 10 numbers in an array and sort the numbers

in ascending order using Selection sort method.

*/


#include<iostream.h>

#include<conio.h>


void main()

{ clrsrc();


 //Declare all variables.

 int arr[10], i, j, temp;


 //prompt user to enter 10 numbers.

 cout<<"Enter any 10 numbers ";


 //Store the numbers in an array.

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

  cin >> arr[i];


 //Selection sort.

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

 { 

for(j = 0; j<10; j++)

    {

if( arr[i] > arr[j])

{

//swap the two.

temp = arr[i];

arr[i] = arr[j];


   arr[j] = temp;

}

}

 }


 cout<<"The sorted array is \n";


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

cout<< arr[i]<<" ";


 getch();


}

 ----------------------------------------------------------------------------------------------------

/*

A C++ program to insert a data in an array, at a given location or at its end.

*/


#include<iostream.h>

#include<conio.h>


void main()

{ clrscr();

 

 //Declare all the variables.

 int arr[20], i, loc, n, data;


 //prompt user to enter the number of elements ";

 cout<<"Enter the size of the array ";


 //store size in 'n'.

 cin >> n;


 cout<<"Enter the array elements\n";


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

cin >> arr[i];


 cout<<"enter the location after which the data is to be inserted \n";


 cin >> loc;


 //Shift all the elements after loc to the right of the array by one.

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

 {

arr[i+1] = arr[i];

 }

 

 cout<<"Enter data to be inserted at "<<loc;


 cin >> arr[loc];


 //Increment size by 1

 n++;

 

 cout<<"Array after insertion \n";


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

  cout<<arr[i]<<" ";


 getch();


}

-------------------------------------------------------------------------------------------------------------

/*

A C++ program to delete a data in an array, at a given location or at its end.

*/


#include<iostream.h>

#include<conio.h>


void main()

{ clrscr();

 

 //Declare all the variables.

 int arr[20], i, loc, n, data;


 //prompt user to enter the number of elements ";

 cout<<"Enter the size of the array ";


 //store size in 'n'.

 cin >> n;


 cout<<"Enter the array elements\n";


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

cin >> arr[i];


 cout<<"enter the location after which the data is to be inserted \n";


 cin >> loc;


 

 //Shift all the elements after loc to the right of the array by one.

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

 {

arr[i+1] = arr[i];

 }

 

 cout<<"Enter data to be inserted at "<<loc;


 cin >> arr[loc];


 if( loc != 0)

 {

//Shift all the elements after the location to the left by 1.

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

{

arr[i] = arr[i + 1];

}


 }


 //Decrement size by 1

 n--;

 

 cout<<"Array after insertion \n";


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

  cout<<arr[i]<<" ";


 getch();


}

----------------------------------------------------------------------------------------------------------------

/*

A C++ program to swap two numbers using call by value

and call by reference. Practical 11.

*/


#include<iostream.h>

#include<conio.h>


//Function prototype declaration for call by reference.

void swapReference(int *, int *);


void main()

{

clrscr();


//Declare all variables.

int a, b, temp, choice;


//Menu.

cout<<"Enter '1' for swapping using call by value \n";


cout<<"Enter '2' for swapping using call by reference \n";


cout<<"Enter '0' to exit \n";


//Menu options using switch statement.


switch(choice):

{

case 1:

cout<<"Enter any two numbers : \n";


cin >> a >> b;


cout<<"Before swap a = "<<a<<" b = "<<b;


temp = a;


a = b;


b = temp;


cout<<"\nAfter swap a = "<<a<<" b = "<<b;


break;


case 2:

cout<<"Enter any two numbers : \n";


cin >> a >> b;


cout<<"Before swap a = "<<a<<" b = "<<b;


swapReference(&a, &b);


cout<<"\nAfter swap a = "<<a<<" b = "<<b;


break;


case 0:


cout<<"Thank you\n";


break;


default:


cout<<"Invalid input! try again. \n";


}

getch();

}


//Function definition.

void swapReference(int * a, int *b)

{

int temp;


temp = *a;


*a = *b;


*b = temp;

}

--------------------------------------------------------------------------------------------------------

/*

A C++ program to print all the Pythagorean triplets between 1 and user given limit using

Turbo C++ compiler.

*/


#include<iostream.h>

#include<conio.h>

void main()

{

    //Declare int variables.

    int a, b, c, sum = 0, limit;


    //Prompt user to input upper limit.

    cin>>limit;

    for(a = 1; a<limit; a++)

    {

        for(b = 1; b<limit; b++)

        {

            for(c = 1; c<limit; c++)

            {

                if( a*a == (b*b + c*c) && (a+b+c) != sum)

                {

                    //To filter duplicate triplets.

                    sum = a+b+c;


                    //Print the triplets.

                    cout<<a<<" "<<b<<" "<<c<<endl;

                }

            }

        }

    }

        getch();

}


--------------------------------------------------------------------------------------------------

 /*

A C++ program to count the number of characters and words in a sentence entered by user.

*/


#include<iostream.h>

#include<conio.h>


void main()

{

clrscr();


int chars=0, words = 1;


char c;


while((c=getche()) != '\r')

{

if(c == ' ')

words++;

else

chars++;

}


cout<<"Number of characters "<<chars;

cout<<"\nNumber of words "<<words;


getch();


}


----------------------------------------------------------------------------------------------------------------------

/*


A C++ program to return cube of a number using inline function.


*/


#include<iostream.h>

#include<conio.h>


//Declare the inline function.


inline int cube(int a)

{

return a*a*a;

}


void main()

{


clrscr();


//Declare all variables.

int number, cube;


//Prompt user to enter a number.

cout<<"Enter a number ";


//Store user input in the integer variable 'number'.

cin>>number;


//Pass number as argument to the inline function 'cube()'.

cube = cube(number);


cout<<"The cube of the number is :"<<cube;


getch();


}


-------------------------------------------------------------------------------------------------------------------


/*

A C++ program to convert all upper case letters of a word to

lower case and vice-versa.

*/


#include<iostream.h>

#include<conio.h>

#include<ctype.h>


void main()

{

clrscr();


//Declare all variables.

int i=0, j=0;

char word[50];


//prompt user to enter a word.

cout<<"Enter a word ";


cin>>word;

//Replace lower case with upper and vice-versa.

while(word[i] != '\0')

if( isupper(word[i]))

{

word[i] = tolower(word[i]);

i++;

}

else

{

word[i] = toupper(word[i]);

i++;

}

}


//Print the new word with case reversed.

while( word[j] != '\0')

{


cout<<word[j];

j++;

}


getch();

}


--------------------------------------------------------------------------------------------------------------------

/*


A C++ program to get maximum of given numbers.


*/


#include<iostream.h>

#include<conio.h>


void main()

{


clrscr();


int max, i;


A[5] = {12,54,6,99,2};


max = A[0];


for(i=1; i<5; i++)

if max < A[i];


{

max = A[i];


}

}


cout<<"The maximum value is "<<max;


getch();


}

-----------------------------------------------------------------------------------------------------------------

/*

A C++ program to get minimum of given numbers.

*/

#include<iostream.h>
#include<conio.h>

void main()
{

clrscr();

int min, i;

A[5] = {12,54,6,99,2};

min = A[0];

for(i=1; i<5; i++)
if min > A[i];

{
min = A[i];

}
}

cout<<"The minimum value is "<<min;

getch();

}

-------------------------------------------------------------------------------------------------------------------

/*

A C++ program to reverse a number using pointers. Practical 22.

*/

#include<iostream.h>
#include<conio.h>


void main()
{

clrscr();

//Declare all variables.
int digit;

long int num;

//Declare a pointer variable 'p' that points to num.
long int * p = &num;

//Prompt user to enter a number.
cout<<"Enter a number : ";

//Store user input in num.
cin >> num;

//Separate the digits of num from end and print them on screen.
while(num % 10)
{
digit = (* p) % 10;

//Print the last digit first.
cout<<digit;

//Drop the last digit of num.

* p = (* p)/10;

}
getch();

}

------------------------------------------------------------------------------------------------------------------

/*
A C++ program to accept an integer array of size 10 . Display the 
squares of all the elements by using pointer to array. Practical 23.
*/

#include<iostream.h>
#include<conio.h>

void main()
{

clrscr();

//Declare variables.

int arr[10], i = 0;

//Prompt user to enter 10 integers.
cout<<"Enter 10 integers : ";

//Store all the integers in the array.
for( i = 0; i<10; i++)
{
cin >> arr[i];
}

//Display the squares of all elements using pointer to array.
for( i = 0; i<10; i++)
{
cout<<(*(arr + i))*(*(arr + i))<<" ";
}

getch();

}

--------------------------------------------------------------------------------------------------------------------

/*
A C++ program to accept a 4x4 matrix and find the sum of
the odd numbers of the matrix. Practical 17.
*/

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
//Declare variables.
int matrix[4][4], i =0 , j =0, sum = 0;

//Prompt user to enter a 4x4 matrix.
cout<<"Enter a 4x4 matix : \n";

//Store the user input in the 2-D array 'matrix'.
for( i = 0; i<4; i++)
{
for(j =0; j<4 ; j++)
{
cin >> matrix[i][j];

}
}

//Sum all the odd numbers of the matrix.
for( i = 0; i<4; i++)
{
for(j =0; j<4 ; j++)
{
if( matrix[i][j] % 2 != 0)
{
sum = sum + matrix[i][j];
}

}
}

//Display the sum.
cout<<"Sum of odd numbers of matrix : "<<sum;

getch();
}

----------------------------------------------------------------------------------------------------------------

//A C++ program to create a class with given members. Practical 19.

#include<iostream.h>
#include<conio.h>
#include<string.h>

//Define the class.
class Competition
{

private:
int learner_id;
string learner_name;

char category[6];

string competition_name;

int position;

int points;

public:
//Define a function that returns points.
int points( int position)
{

switch(position)
{

case 1:
points = 10;

break;

case 2:

points = 7;

break;

case 3:

points = 5;

break;
default:
points = 0;

}
return points;

}


}; //End of class definition.


void main()
{
clrscr();


//Create objects of the class.
Competition comp;

//Access class member functions using dot operator.
comp.points(7);

getch();

}

-----------------------------------------------------------------------------------------------------------------------

// Derived class , practical 20.

#include<iostream.h>
#include<conio.h>
#include<string.h>

//Define the class COURSE.
class COURSE
{
//Declare all the private data members of the class.
private:
int course_id;

string course_name;

int duration;

float fee;
//Declare and define all the public member functions of the class.

public:
void accept(int course_id, string course_name, int duration, float fee)
{
this.course_id = course_id;

this.course_name = course_name;

this.duration = duration;

this.fee = fee;
}

};

//Define the publically inherited derived class SR_SECONDARY.
class SR_SECONDARY : public COURSE
{
//Declare private data members of the class.
private:
string subject1;

string subject2;

string subject3;

//Declare and define all the public member functions of the class.
public:
void enter(string subject1, string subject2, string subject3)
{
this.subject1 = subject1;

this.subject2 = subject2;

this.subject3 = subject3;
}

void display()
{
cout<<"Subject1 : "<<subject1<<'\n';

cout<<"Subject2 : "<<subject2<<'\n';

cout<<"Subject3 : "<<subject3<<'\n';
}

};

//Define the protectedly derived class VOCATIONAL from the class SR_SECONDARY.

class VOCATIONAL : protected SR_SECONDARY
{
//Declare all private data members of the class.
private:
string voc_name;

string requirement;

int age;

//Declare and define all public member functions of the class.
public:
void voc_accept(string voc_name, string requirement, int age)
{
this.voc_name = voc_name;

this.requirement = requirement;

this.age = age;
}

void voc_display()
{
cout<<"Vocationa Name : "<<voc_name<<'\n';

cout<<"Requirement : "<<requirement<<'\n';

cout<<"Age : "<<age;
}
};



void main()
{

clrscr();

//Create objects of all the classes and invoke respective member functions.
COURSE cr;

cr.accept(1, "BCA", 3, 12000.00);

SR_SECONDARY sr;

sr.enter("Maths", "Data Structues", "Algorithms");

sr.display();

VOCATIONAL vc;

vc.voc_accept("Music", "High School", 16);

vc.voc_display();

getch();

}

----------------------------------------------------------------------------------------------------------------

/*

A C++ program to find the area of a triangle, given its three sides by the user.

*/

#include<conio.h>
#include<iostream.h>
#include<math.h>


void main()
{

clrscr();

//declare all variables.
float a, b, c, s, A;

//Prompt user to input three sides of the triangle.
cout<<"Enter the three sides of the triangle :";

//store user input.
cin>>a>>b>>c;

//Calculate the semi-perimeter.
s = (a+b+c)/2;

//Calculate the area of the triangle using Heron's formula.
A = sqrt( s*(s-a)*(s-b)*(s-c))

//Print area on the screen.
cout<<"\nArea of the triangles is :"<<A;

getch();
}

------------------------------------------------------------------------------------------------------------

/*

A C++ program to check if a number given by user is an Armstrong number.

*/


#include <math.h>
#include<conio.h>
#include<iostream.h>

void main() {
   
    //Declare variables.
   int num, originalNum, remainder, n = 0;

   float result = 0.0;

   printf("Enter an integer: ");

   //Store number entered by user in integer variable 'num'.
   scanf("%d", &num);

   //Store value of 'num' into variable 'originalNum'
   originalNum = num;

   // store the number of digits in num in variable 'n'.

   for (originalNum = num; originalNum != 0; ++n) 
  {
       originalNum /= 10;
   }


   //Extract the digits of 'num', and raise to power 'n' and get their sum.

   for (originalNum = num; originalNum != 0; originalNum /= 10) 
{
       remainder = originalNum % 10;

      // store the sum of the power of individual digits in result
      result += pow(remainder, n);
   }


   // if num is equal to result, the number is an Armstrong number
   if ((int)result == num)
   {
    printf("%d is an Armstrong number.", num);
    }
   else
    {
    printf("%d is not an Armstrong number.", num);
     }


}

-----------------------------------------------------------------------------------------------------------------------------

/*

A C++ program to write the factorial of a number.

*/

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();

//Declare all the variables.

int n, f=1, i=1;

//prompt user to enter a positive number.
cout<<"Enter a positive number :";

//store user input into 'n'.
cin>>n;

//Compute the factorial and store its value in 'f'.
while(i <= n)
{

f = f*i;

i++;

}
//Print the factorial on screen.
cout<<"Factorial of "<<n<<" = "<<f <<endl;

 getch();
}

-----------------------------------------------------------------------------------------------------------

/*

A C++ program to print the Fibonnaci series upto the nth term.

*/

#include <iostream.h>
#include<conio.h>

void main() 
{
    //clear screen.
     clrscr();

    //Declare all the variables.
     int n, t1 = 0, t2 = 1, nextTerm = 0;

     //Prompt user to enter the number of terms to be printed. 
    cout << "Enter the number of terms: ";

     //Store the term in variable 'n'.
    cin >> n;

     //Print "Fibonnaci Series" on the screen.
    cout << "Fibonacci Series: ";

    //For loop to print the series.
    for (int i = 1; i <= n; ++i) 
      {
        
       // Prints the first two terms.
        if(i == 1) 
        {
            cout << t1 << ", ";
            continue;
        }
        if(i == 2) 
       {
            cout << t2 << ", ";
            continue;
        }

        //Sum of the last two terms becomes the next term.
        nextTerm = t1 + t2;
        
        //The previous term becomes the first term for the next term.
        t1 = t2;

        //The new term becomes the second term for the next term.
        t2 = nextTerm;
        
        //Print the terms of the series on the screen.
        cout << nextTerm << ", ";
    }

    getch();
}

--------------------------------------------------------------------------------------------------------------

/*

A C++ program to find the GCD/HCF of two positive numbers.

*/

#include<iostream.h>
#include<math.h>
#include<conio.h>

int main()
{
    //clrscr();

    //Declare all variables.
    int a, b, result;

    //Prompt user to input two positive numbers.
    cout<<"Enter any two positive numbers :";

    //Store user inputs
    cin>>a>>b;

    //Find Minimum of a and b
    result = min(a, b);


    while (result > 0)
    {
        if (a % result == 0 && b % result == 0)
        {
            break;
        }

        result--;
    }

    // Return gcd of a and b
    cout<<"GCD of "<<a<<" and "<<b<<" is "<<result;

    //getch();

}

-------------------------------------------------------------------------------------

/*

A C++ program to check if the given number is a prime number.

*/

#include <iostream.h>
#include<conio.h>

void main() {


   //Clear screen.
    clrscr();

  //Declare all the variables.
  int i, n;
  bool is_prime = true;

  //Prompt user to enter a positive integer.
  cout << "Enter a positive integer: ";

  //Put user input into variable 'n'.
  cin >> n;

  // 0 and 1 are not prime numbers
  if (n == 0 || n == 1) 
  {
    is_prime = false;
  }

  // loop to check if n is prime
  for (i = 2; i <= n/2; ++i) 
{
    if (n % i == 0) 
    {
      is_prime = false;

      break;
    }

  }

  if (is_prime)
    {
    cout << n << " is a prime number";
    }
  else
   {
    cout << n << " is not a prime number";
     }

    getch();
}

--------------------------------------------------------------------------------------------------------

/*

A C++ program to print the table of 'n'.

*/

#include <iostream.h>
#include<conio.h>

void main()
{

//Clear the screen.
clrscr();

//Declare all the variables.
int i, n;

//prompt user to input a number.
cout<<"Please enter a integer number "<<endl;

              //Store user input into 'n'.
cin>>n;

              //Print the table.

  for(i = 1; i<= 10; i++)
{

cout<<"n x "<<i<<" = "<<n*i<<endl;
}

getch();
 
}

------------------------------------------------------------------------------------------------

/*

A C++ program to print all the Pythagorean triplets between 1 and user given limit.

*/

#include<iostream.h>
#include<conio.h>

void main()
{
    //Declare int variables.
    int a, b, c, sum = 0, limit;

    //Prompt user to input upper limit.
    cin>>limit;

    for(a = 1; a<limit; a++)
    {
        for(b = 1; b<limit; b++)
        {
            for(c = 1; c<limit; c++)
            {
                if( a*a == (b*b + c*c) && (a+b+c) != sum)
                {
                    //To filter duplicate triplets.
                    sum = a+b+c;

                    //Print the triplets.
                    cout<<a<<" "<<b<<" "<<c<<endl;

                }
            }
        }
    }

        getch();
}

--------------------------------------------------------------------------------------------------


No comments:

Post a Comment

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...