basic c++ programs

12
Basic c++ Programs 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.

Upload: harman-kaur

Post on 06-May-2015

4.994 views

Category:

Education


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Basic c++ programs

Basic c++ Programs1. 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.

Page 2: Basic c++ programs

1.Program to swap two values using third variable.

#include<iostream.h>#include<conio.h>void main(){ int a,b,temp; clrscr();cout<<“Enter value of a:”;cin>>a;cout<<“Enter value of b:”;cin>>b; cout<<"Original value of a is:"<<a<<endl; cout<<"Original value of b is:"<<b<<endl;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();}

OUTPUTEnter value of a: 45Enter value of b: 56Original value of a is:45Original value of b is:56After SwappingNew value of a is:56New value of b is:45

Page 3: Basic c++ programs

1.Program to swap two values using third variable.

#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; cout<<“New value of a is:"<<anew<<endl; cout<<“New value of b is:"<<bnew<<endl;}};void main(){ int a=10,b=15; //we have set the values of a and b as 10 ,15, it can be user defined too.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 operatorgetch();}

OutputValue of a is: 10Value of b is: 15After swappingNew Value of a is: 15New value of b is: 10

Page 4: Basic c++ programs

2.Program to swap two values without using third variable.

#include<iostream.h>#include<conio.h>void main(){ int a, b; clrscr();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; 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();}

OUTPUTEnter value of a: 45Enter value of b: 56Original value of a is:45Original value of b is:56After SwappingNew value of a is:56New value of b is:45

Page 5: Basic c++ programs

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; bnew=anew-bnew; anew=anew-bnew; cout<<"new value of a is:"<<anew<<endl; cout<<"new value of b is:"<<bnew<<endl; } void main() { int a,b; 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(); }

OUTPUTEnter value of a: 25Enter value of b: 65Value of a is: 25Value of b is:65After swappingValue of a is: 65Value of b is: 25

Page 6: Basic c++ programs

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; clrscr(); cout<<"enter 10 elements of array:"; for(i=0;i<=9;i++) { cin>>a[i]; } m=a[0]; for(j=1;j<=9;j++) { if(a[j]>m) { m=a[j]; loc=j+1; } } cout<<"max value is:"<<m; cout<<"its loc is:"<<loc; getch(); }

OUTPUTEnter 10 elements of array:

582

12653698452596

Max value is: 98Its location is: 7

Page 7: Basic c++ programs

3.Program to find maximum of 10 values stored in array. #include<iostream.h> #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:"; for(int i=0;i<=9;i++) { cin>>a[i]; } max=a[0]; for(j=1;j<=9;j++) { if(a[j]>max) { max=a[j]; } } return max; } }; void main() { int max1; clrscr(); greatest g1; max1=g1.largest(); cout<<"Greatest of ten values is:"<<max1; getch(); }

OUTPUT

Enter 10 elements of array: 582

12653698452596

Max value is: 98

Page 8: Basic c++ programs

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; clrscr(); cout<<“Enter 10 elements of array.”; for(i=0;i<=9;i++) cin>>a[i];

for(j=1;j<=9;j++) { If(a[j]<min) min=a[j]; } cout<<“smallest of 10 values is:”<<min; getch(); }

OUTPUTEnter 10 elements of array:

582

12653698452596

Smallest of ten values is: 2

Page 9: Basic c++ programs

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; int calculate_smallest() { cout<<"enter 10 elements of array:"; for(int i=0;i<=9;i++) { cin>>a[i]; } min=a[0]; //0th element is set as minimum values for(j=1;j<=9;j++) { if(a[j]<min) { min=a[j]; } } return min; } }; void main() { int min1; clrscr(); smallest s1; min1=s1.calculate_smallest(); cout<<“Smallest of ten values is:"<<min1; getch(); }

OUTPUTEnter 10 elements of array:

-582

12653698452596

Smallest of ten values is: -5

Page 10: Basic c++ programs

5.Program to find largest of two values using conditional operator.#include<iostream.h>#include<conio.h>void main(){ int a , b, c;clrscr();cout<<“Enter value of a:”;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();}

OUTPUTEnter value of a: 56Enter value of b: 3656 is greatest.

Page 11: Basic c++ programs

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(); cout<<"enter a:"; cin>>a; cout<<"enter b:"; cin>>b; comparison c1; cout<<"Greatest of two values is:"<<c1.greatest(a,b); getch(); }

OUTPUT

Enter value of a: 62Enter value of b: 36Greatest of two values is:62

Page 12: Basic c++ programs

6.Program to find smallest of three values using ternary operator.

#include<iostream.h>#include<conio.h>void main(){ int a , b, c, max;clrscr();cout<<"Enter value of a:";cin>>a;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();}

OUTPUTEnter value of a: 96Enter value of b: 125Enter value of c: 3636 is greatest