SlideShare a Scribd company logo
1. Program to swap two values using third variable.
2. Program to swap two values without using third variable.
3. Program to find the maximum of 10 values stored in array.
4. Program to find the smallest of 10 values stored in array.
5. Program to find the largest of two values using conditional
operator.
6.Program to find the smallest of three values using conditional
operator.
#include<iostream.h>
#include<conio.h>
void main()
{
 int a,b,temp;                                       OUTPUT
 clrscr();                                   Enter value of a: 45
cout<<“Enter value of a:”;                   Enter value of b: 56
cin>>a;                                      Original value of a is:45
cout<<“Enter value of b:”;                   Original value of b is:56
cin>>b;                                      After Swapping
 cout<<"Original value of a is:"<<a<<endl;   New value of a is:56
 cout<<"Original value of b is:"<<b<<endl;   New value of b is:45
temp=a;
 a=b;
 b=temp;
cout<<“After Swapping..”<<endl;
 cout<<“New value of a is:"<<a<<endl;
 cout<<“New value of b is:"<<b<<endl;
getch();
}
#include<iostream.h>
#include<conio.h>
class swapping
{
 public:
 int anew,bnew,temp;
 void swap( int anew, int bnew) //function receives arguments via object of swapping class
 {
  temp=anew;
  anew=bnew;
  bnew=temp;                                                                                         Output
  cout<<“New value of a is:"<<anew<<endl;                                                    Value of a is: 10
  cout<<“New value of b is:"<<bnew<<endl;
}                                                                                            Value of b is: 15
};                                                                                           After swapping
void main()
{                                                                                            New Value of a is: 15
 int a=10,b=15; //we have set the values of a and b as 10 ,15, it can be user defined too.   New value of b is: 10
clrscr();
 swapping s1; //created object of swapping class
 cout<<“Value of a is:"<<a<<endl;
 cout<<“Value of b is:"<<b<<endl;
cout<<“After Swapping”<<endl;
 s1.swap(a,b); //called swap function using object and dot operator
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
 int a, b;                                      OUTPUT
 clrscr();                              Enter value of a: 45
cout<<“Enter value of a:”;              Enter value of b: 56
cin>>a;                                 Original value of a is:45
cout<<“Enter value of b:”;              Original value of b is:56
cin>>b;                                 After Swapping
 cout<<“Value of a is:"<<a<<endl;       New value of a is:56
 cout<<“Value of b is:"<<b<<endl;       New value of b is:45
 a=a+b;;
 b=a-b;
 a=a-b;
cout<<“After Swapping..”<<endl;
 cout<<“New value of a is:"<<a<<endl;
 cout<<“New value of b is:"<<b<<endl;
getch();
}
2.Program to swap two values without using third variable.
    #include<iostream.h>
    #include<conio.h>
    class swapping
    {
     public:
     int anew,bnew;
     void swap( int anew, int bnew);
     };
     void swapping :: swap(int anew , int bnew)
     {
      anew= anew+bnew;                                        OUTPUT
      bnew=anew-bnew;                             Enter value of a: 25
      anew=anew-bnew;                             Enter value of b: 65
      cout<<"new value of a is:"<<anew<<endl;     Value of a is: 25
      cout<<"new value of b is:"<<bnew<<endl;
                                                   Value of b is:65
     }
    void main()                                   After swapping
    {                                             Value of a is: 65
     int a,b;                                     Value of b is: 25
     clrscr();
     swapping s1,s2;
     cout<<"Enter value of a:";
     cin>>a;
     cout<<"Enter value of b:";
     cin>>b;
     cout<<“Value of a is:"<<a<<endl;
     cout<<“Value of b is:"<<b<<endl;
     s1.swap(a,b);
      getch();
    }
3.Program to find maximum of 10 values stored in array.

     #include<iostream.h>
     #include<conio.h>
     void main()
     {
     int a[10],i,j,m,loc=0;
                                                           OUTPUT
     clrscr();                              Enter 10 elements of array:
     cout<<"enter 10 elements of array:";                                  5
     for(i=0;i<=9;i++)                                                     8
     {
      cin>>a[i];                                                           2
     }                                                                    12
     m=a[0];                                                              65
     for(j=1;j<=9;j++)
     {
                                                                           36
      if(a[j]>m)                                                          98
      {                                                                   45
       m=a[j];                                                            25
       loc=j+1;
      }                                                                   96
     }
     cout<<"max value is:"<<m;              Max value is: 98
     cout<<"its loc is:"<<loc;
     getch();
                                             Its location is: 7
     }
3.Program to find maximum of 10 values stored in array.
    #include<iostream.h>                                                                            OUTPUT
    #include<conio.h>
    class greatest
    {
      public:
      int a[10],j,max;
      int largest() //member func of greatest class that returns a value of integer type
 
 
       {
       cout<<"enter 10 elements of array:";
                                                                                            Enter 10 elements of array:
      for(int i=0;i<=9;i++)                                                                                           5
      {
       cin>>a[i];                                                                                                     8
      }
       max=a[0];                                                                                                      2
       for(j=1;j<=9;j++)
       {                                                                                                            12
 
 
         if(a[j]>max)
         {
                                                                                                                     65
          max=a[j];                                                                                                 36
        }
      }                                                                                                             98
      return max;
     }                                                                                                              45
    };
    void main()                                                                                                     25
 
 
     {
     int max1;
                                                                                                                     96
    clrscr();
    greatest g1;
    max1=g1.largest();                                                                     Max value is: 98
    cout<<"Greatest of ten values is:"<<max1;
    getch();
    }
4.Program to find smallest of 10 values stored in an array.
   #include<iostream.h>
   #include<conio.h>
   void main()
   {
    int min,a[10],I;                                   OUTPUT
   clrscr();                                 Enter 10 elements of array:
   cout<<“Enter 10 elements of array.”;                                      5
   for(i=0;i<=9;i++)                                                         8
   cin>>a[i];                                                                2
                                                                             12
   for(j=1;j<=9;j++)                                                        65
   {                                                                        36
     If(a[j]<min)                                                           98
     min=a[j];                                                              45
   }                                                                        25
   cout<<“smallest of 10 values is:”<<min;                                  96
   getch();
                                              Smallest of ten values is: 2
   }
4.program to find smallest of 10 values stored in an array.
   #include<iostream.h>
   #include<conio.h>
   class smallest
   {
     public:
     int a[10],j,min;                                                     OUTPUT
     int calculate_smallest()
     {                                                           Enter 10 elements of array:
     cout<<"enter 10 elements of array:";
     for(int i=0;i<=9;i++)                                                                 -5
     {
      cin>>a[i];                                                                            8


      }
       min=a[0];         //0th element is set as minimum values
                                                                                             2
      for(j=1;j<=9;j++)                                                                   12
      {
       if(a[j]<min)                                                                       65
       {
         min=a[j];                                                                        36
       }
     }                                                                                    98

    }
      return min;                                                                          45
   };                                                                                     25
   void main()
   {                                                                                      96
   int min1;
   clrscr();
   smallest s1;
   min1=s1.calculate_smallest();
                                                                  Smallest of ten values is: -5
   cout<<“Smallest of ten values is:"<<min1;
   getch();
   }
5.Program to find largest of two values using conditional operator.
  #include<iostream.h>
  #include<conio.h>
  void main()
 {
  int a , b, c;                           OUTPUT
                                           Enter value of a: 56
  clrscr();                               Enter value of b: 36
  cout<<“Enter value of a:”;              56 is greatest.
  cin>>a;
  cout<<“ Enter value of b:”;
  cin>>b;
  c=a>b?a:b; //using conditional operator, c stores the
  biggest of two values.
 cout<<a<<“ is greatest”;
 getch();
}
5.Program to find greatest of two values using conditional
operator.
      #include<iostream.h>
      #include<conio.h>
      class comparison
      {
         public:
          int a1,b1,max;
         int greatest (int a1,int b1)
         {
          max=a1>b1?a1:b1; //using conditional(ternary operator) to compare a and b, storing result in max.
          return max;
         }
      };
      void main()
      {
      int a, b;
      clrscr();                                                               OUTPUT
      cout<<"enter a:";
      cin>>a;
      cout<<"enter b:";
      cin>>b;                                                     Enter value of a: 62
      comparison c1;                                              Enter value of b: 36
      cout<<"Greatest of two values is:"<<c1.greatest(a,b);
      getch();
                                                                   Greatest of two values is:62
      }
6.Program to find smallest of three values using ternary operator.
     #include<iostream.h>
     #include<conio.h>
     void main()
     {                                          OUTPUT
      int a , b, c, max;           Enter value of a: 96
     clrscr();                     Enter value of b: 125
     cout<<"Enter value of a:";    Enter value of c: 36
     cin>>a;                       36 is greatest
     cout<<" Enter value of b:";
     cin>>b;
     cout<<"Enter value of c:";
     cin>>c;
      max=a<b?(a<c?a:c):(b<c?b:c); //using conditional
      operator, max stores the biggest of three values.
     cout<<max<<" is smallest";
     getch();
    }

More Related Content

PPTX
PDF
Modern c++ (C++ 11/14)
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
C++11 concurrency
PPTX
C++ Presentation
PDF
C++11
PDF
C++17 introduction - Meetup @EtixLabs
PPTX
C++ theory
Modern c++ (C++ 11/14)
Fun with Lambdas: C++14 Style (part 1)
C++11 concurrency
C++ Presentation
C++11
C++17 introduction - Meetup @EtixLabs
C++ theory

What's hot (20)

PPTX
PPT
CBSE Class XI Programming in C++
PDF
Le langage rust
PPTX
Compile time polymorphism
PPTX
Summary of C++17 features
PPTX
Fun with Lambdas: C++14 Style (part 2)
PDF
C++11 & C++14
PPTX
Library functions in c++
PDF
C++ book
PPTX
C++ 11 Features
PDF
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
PPTX
PPTX
basics of c++
PDF
High performance web programming with C++14
PPTX
Functions in c++
PPTX
C++ language basic
ODP
OpenGurukul : Language : C++ Programming
PPTX
Operator overloading2
PPTX
C introduction by thooyavan
CBSE Class XI Programming in C++
Le langage rust
Compile time polymorphism
Summary of C++17 features
Fun with Lambdas: C++14 Style (part 2)
C++11 & C++14
Library functions in c++
C++ book
C++ 11 Features
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
basics of c++
High performance web programming with C++14
Functions in c++
C++ language basic
OpenGurukul : Language : C++ Programming
Operator overloading2
C introduction by thooyavan
Ad

Viewers also liked (20)

PPT
Intro. to prog. c++
PPTX
Effective writing, tips for Bloggers
PPT
System outputs - Computer System
PPT
Principle of marketing
PPSX
C++ Programming Language
PPT
Basics of c++ Programming Language
PPTX
Basic characteristics of business
PPTX
What is business
PPT
What is computer Introduction to Computing
PDF
Python in Computer Vision
PPTX
Basic qualities of a good businessman
PPT
An overview of computers and programming languages
PDF
IBM MQ V9 Overview
PDF
Whats new in IBM MQ; V9 LTS, V9.0.1 CD and V9.0.2 CD
PPT
Control structures in C++ Programming Language
PPT
Control structures ii
PPTX
C++ ppt
PPT
Basic elements of java
PPT
Strategic planning and mission statement
PPT
Introduction to objects and inputoutput
Intro. to prog. c++
Effective writing, tips for Bloggers
System outputs - Computer System
Principle of marketing
C++ Programming Language
Basics of c++ Programming Language
Basic characteristics of business
What is business
What is computer Introduction to Computing
Python in Computer Vision
Basic qualities of a good businessman
An overview of computers and programming languages
IBM MQ V9 Overview
Whats new in IBM MQ; V9 LTS, V9.0.1 CD and V9.0.2 CD
Control structures in C++ Programming Language
Control structures ii
C++ ppt
Basic elements of java
Strategic planning and mission statement
Introduction to objects and inputoutput
Ad

Similar to Basic c++ programs (20)

PPTX
C- Programs - Harsh
DOCX
C++ file
DOCX
C++ file
PDF
C++ practical
DOCX
DOCX
Cpp programs
DOC
Oops lab manual2
PPT
Computer Programming- Lecture 9
PPT
PPTX
cpp promo ppt.pptx
PPTX
C++ lectures all chapters in one slide.pptx
PDF
C programs Set 2
PDF
Final DAA_prints.pdf
DOC
Pads lab manual final
PPTX
Story of static code analyzer development
DOCX
12th CBSE Practical File
PDF
54602399 c-examples-51-to-108-programe-ee01083101
PDF
C++ TUTORIAL 5
PDF
Array notes
C- Programs - Harsh
C++ file
C++ file
C++ practical
Cpp programs
Oops lab manual2
Computer Programming- Lecture 9
cpp promo ppt.pptx
C++ lectures all chapters in one slide.pptx
C programs Set 2
Final DAA_prints.pdf
Pads lab manual final
Story of static code analyzer development
12th CBSE Practical File
54602399 c-examples-51-to-108-programe-ee01083101
C++ TUTORIAL 5
Array notes

More from harman kaur (14)

PPTX
Working with functions in matlab
PPTX
Matlab 1(operations on_matrix)
PPTX
Creating red black tree
DOCX
c plus plus programsSlide
DOCX
Program to illustrate Switch, Goto and Exit statements.
PPTX
Digital u1
PPTX
Quiz on Logic Gate
DOCX
Functions oracle (pl/sql)
PPTX
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
DOCX
Msql query
DOCX
Rules of inference
DOCX
Virtual function
PPTX
operator overloading in c++
PPTX
Introduction to digital electornics
Working with functions in matlab
Matlab 1(operations on_matrix)
Creating red black tree
c plus plus programsSlide
Program to illustrate Switch, Goto and Exit statements.
Digital u1
Quiz on Logic Gate
Functions oracle (pl/sql)
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
Msql query
Rules of inference
Virtual function
operator overloading in c++
Introduction to digital electornics

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Basic Mud Logging Guide for educational purpose
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
master seminar digital applications in india
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Types and Its function , kingdom of life
Microbial disease of the cardiovascular and lymphatic systems
Basic Mud Logging Guide for educational purpose
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
Renaissance Architecture: A Journey from Faith to Humanism
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
master seminar digital applications in india
Mark Klimek Lecture Notes_240423 revision books _173037.pdf

Basic c++ programs

  • 1. 1. Program to swap two values using third variable. 2. Program to swap two values without using third variable. 3. Program to find the maximum of 10 values stored in array. 4. Program to find the smallest of 10 values stored in array. 5. Program to find the largest of two values using conditional operator. 6.Program to find the smallest of three values using conditional operator.
  • 2. #include<iostream.h> #include<conio.h> void main() { int a,b,temp; OUTPUT clrscr(); Enter value of a: 45 cout<<“Enter value of a:”; Enter value of b: 56 cin>>a; Original value of a is:45 cout<<“Enter value of b:”; Original value of b is:56 cin>>b; After Swapping cout<<"Original value of a is:"<<a<<endl; New value of a is:56 cout<<"Original value of b is:"<<b<<endl; New value of b is:45 temp=a; a=b; b=temp; cout<<“After Swapping..”<<endl; cout<<“New value of a is:"<<a<<endl; cout<<“New value of b is:"<<b<<endl; getch(); }
  • 3. #include<iostream.h> #include<conio.h> class swapping { public: int anew,bnew,temp; void swap( int anew, int bnew) //function receives arguments via object of swapping class { temp=anew; anew=bnew; bnew=temp; Output cout<<“New value of a is:"<<anew<<endl; Value of a is: 10 cout<<“New value of b is:"<<bnew<<endl; } Value of b is: 15 }; After swapping void main() { New Value of a is: 15 int a=10,b=15; //we have set the values of a and b as 10 ,15, it can be user defined too. New value of b is: 10 clrscr(); swapping s1; //created object of swapping class cout<<“Value of a is:"<<a<<endl; cout<<“Value of b is:"<<b<<endl; cout<<“After Swapping”<<endl; s1.swap(a,b); //called swap function using object and dot operator getch(); }
  • 4. #include<iostream.h> #include<conio.h> void main() { int a, b; OUTPUT clrscr(); Enter value of a: 45 cout<<“Enter value of a:”; Enter value of b: 56 cin>>a; Original value of a is:45 cout<<“Enter value of b:”; Original value of b is:56 cin>>b; After Swapping cout<<“Value of a is:"<<a<<endl; New value of a is:56 cout<<“Value of b is:"<<b<<endl; New value of b is:45 a=a+b;; b=a-b; a=a-b; cout<<“After Swapping..”<<endl; cout<<“New value of a is:"<<a<<endl; cout<<“New value of b is:"<<b<<endl; getch(); }
  • 5. 2.Program to swap two values without using third variable.  #include<iostream.h>  #include<conio.h>  class swapping  {  public:  int anew,bnew;  void swap( int anew, int bnew);  };  void swapping :: swap(int anew , int bnew)  {  anew= anew+bnew; OUTPUT  bnew=anew-bnew; Enter value of a: 25  anew=anew-bnew; Enter value of b: 65  cout<<"new value of a is:"<<anew<<endl; Value of a is: 25  cout<<"new value of b is:"<<bnew<<endl; Value of b is:65  }  void main() After swapping  { Value of a is: 65  int a,b; Value of b is: 25  clrscr();  swapping s1,s2;  cout<<"Enter value of a:";  cin>>a;  cout<<"Enter value of b:";  cin>>b;  cout<<“Value of a is:"<<a<<endl;  cout<<“Value of b is:"<<b<<endl;  s1.swap(a,b);  getch();  }
  • 6. 3.Program to find maximum of 10 values stored in array.  #include<iostream.h>  #include<conio.h>  void main()  {  int a[10],i,j,m,loc=0; OUTPUT  clrscr(); Enter 10 elements of array:  cout<<"enter 10 elements of array:"; 5  for(i=0;i<=9;i++) 8  {  cin>>a[i]; 2  } 12  m=a[0]; 65  for(j=1;j<=9;j++)  { 36  if(a[j]>m) 98  { 45  m=a[j]; 25  loc=j+1;  } 96  }  cout<<"max value is:"<<m; Max value is: 98  cout<<"its loc is:"<<loc;  getch(); Its location is: 7  }
  • 7. 3.Program to find maximum of 10 values stored in array.  #include<iostream.h> OUTPUT  #include<conio.h>  class greatest  {  public:  int a[10],j,max;  int largest() //member func of greatest class that returns a value of integer type   { cout<<"enter 10 elements of array:"; Enter 10 elements of array:  for(int i=0;i<=9;i++) 5  {  cin>>a[i]; 8  }  max=a[0]; 2  for(j=1;j<=9;j++)  { 12   if(a[j]>max) { 65  max=a[j]; 36  }  } 98  return max;  } 45  };  void main() 25   { int max1; 96  clrscr();  greatest g1;  max1=g1.largest(); Max value is: 98  cout<<"Greatest of ten values is:"<<max1;  getch();  }
  • 8. 4.Program to find smallest of 10 values stored in an array.  #include<iostream.h>  #include<conio.h>  void main()  {  int min,a[10],I; OUTPUT  clrscr(); Enter 10 elements of array:  cout<<“Enter 10 elements of array.”; 5  for(i=0;i<=9;i++) 8  cin>>a[i]; 2 12  for(j=1;j<=9;j++) 65  { 36  If(a[j]<min) 98  min=a[j]; 45  } 25  cout<<“smallest of 10 values is:”<<min; 96  getch(); Smallest of ten values is: 2  }
  • 9. 4.program to find smallest of 10 values stored in an array.  #include<iostream.h>  #include<conio.h>  class smallest  {  public:  int a[10],j,min; OUTPUT  int calculate_smallest()  { Enter 10 elements of array:  cout<<"enter 10 elements of array:";  for(int i=0;i<=9;i++) -5  {  cin>>a[i]; 8   } min=a[0]; //0th element is set as minimum values 2  for(j=1;j<=9;j++) 12  {  if(a[j]<min) 65  {  min=a[j]; 36  }  } 98   } return min; 45  }; 25  void main()  { 96  int min1;  clrscr();  smallest s1;  min1=s1.calculate_smallest(); Smallest of ten values is: -5  cout<<“Smallest of ten values is:"<<min1;  getch();  }
  • 10. 5.Program to find largest of two values using conditional operator.  #include<iostream.h>  #include<conio.h>  void main() {  int a , b, c; OUTPUT Enter value of a: 56  clrscr(); Enter value of b: 36  cout<<“Enter value of a:”; 56 is greatest.  cin>>a;  cout<<“ Enter value of b:”;  cin>>b;  c=a>b?a:b; //using conditional operator, c stores the biggest of two values.  cout<<a<<“ is greatest”;  getch(); }
  • 11. 5.Program to find greatest of two values using conditional operator.  #include<iostream.h>  #include<conio.h>  class comparison  {  public:  int a1,b1,max;  int greatest (int a1,int b1)  {  max=a1>b1?a1:b1; //using conditional(ternary operator) to compare a and b, storing result in max.  return max;  }  };  void main()  {  int a, b;  clrscr(); OUTPUT  cout<<"enter a:";  cin>>a;  cout<<"enter b:";  cin>>b; Enter value of a: 62  comparison c1; Enter value of b: 36  cout<<"Greatest of two values is:"<<c1.greatest(a,b);  getch(); Greatest of two values is:62  }
  • 12. 6.Program to find smallest of three values using ternary operator.  #include<iostream.h>  #include<conio.h>  void main()  { OUTPUT  int a , b, c, max; Enter value of a: 96  clrscr(); Enter value of b: 125  cout<<"Enter value of a:"; Enter value of c: 36  cin>>a; 36 is greatest  cout<<" Enter value of b:";  cin>>b;  cout<<"Enter value of c:";  cin>>c;  max=a<b?(a<c?a:c):(b<c?b:c); //using conditional operator, max stores the biggest of three values.  cout<<max<<" is smallest";  getch(); }

Editor's Notes

  • #3: This is a program to swap two values using the third variable(temp). It does not use the class concept.The next program is same but it is done using class concept.
  • #4: This is same program as the previous one, but it use class structre.
  • #5: It is a program to swap two values without using third variable and without using class structure