• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Basic c++ programs
 

Basic c++ programs

on

  • 2,069 views

It includes 6 basic c++ programs, each program has been done twice, once normally and then using class.

It includes 6 basic c++ programs, each program has been done twice, once normally and then using class.

Statistics

Views

Total Views
2,069
Views on SlideShare
2,022
Embed Views
47

Actions

Likes
1
Downloads
112
Comments
0

2 Embeds 47

http://www.virtualians.pk 45
http://www.techgig.com 2

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.
  • This is same program as the previous one, but it use class structre.
  • It is a program to swap two values without using third variable and without using class structure

Basic c++ programs Basic c++ programs Presentation Transcript

  • 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 conditionaloperator.6.Program to find the smallest of three values using conditionaloperator.
  • #include<iostream.h>#include<conio.h>void main(){ int a,b,temp; OUTPUT clrscr(); Enter value of a: 45cout<<“Enter value of a:”; Enter value of b: 56cin>>a; Original value of a is:45cout<<“Enter value of b:”; Original value of b is:56cin>>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:45temp=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 swappingvoid 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: 10clrscr(); 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 operatorgetch();}
  • #include<iostream.h>#include<conio.h>void main(){ int a, b; OUTPUT clrscr(); Enter value of a: 45cout<<“Enter value of a:”; Enter value of b: 56cin>>a; Original value of a is:45cout<<“Enter value of b:”; Original value of b is:56cin>>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 conditionaloperator.  #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(); }