Presented By- Harsh Sharma
OUTPUT
Enter value of a: 45
Enter value of b: 56
Original value of a is:45
Original value of b is:56
After Swapping
New value of a is:56
New value of b is:45
Output
Value of a is: 10
Value of b is: 15
After swapping
New Value of a is: 15
New value of b is: 10
OUTPUT
Enter value of a: 45
Enter value of b: 56
Original value of a is:45
Original value of b is:56
After Swapping
New value of a is:56
New value of b is:45
 #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();
 }
OUTPUT
Enter value of a: 25
Enter value of b: 65
Value of a is: 25
Value of b is:65
After swapping
Value of a is: 65
Value of b is: 25
 #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();
 }
OUTPUT
Enter 10 elements of array:
5
8
2
12
65
36
98
45
25
96
Max value is: 98
Its location is: 7
 #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:
5
8
2
12
65
36
98
45
25
96
Max value is: 98
 #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();
 }
OUTPUT
Enter 10 elements of array:
5
8
2
12
65
36
98
45
25
96
Smallest of ten values is: 2
 #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();
 }
OUTPUT
Enter 10 elements of array:
-5
8
2
12
65
36
98
45
25
96
Smallest of ten values is: -
5
 #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();
 }
OUTPUT
Enter value of a: 56
Enter value of b: 36
56 is greatest.
 #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: 62
Enter value of b: 36
Greatest of two values is:62
 #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();
 }
OUTPUT
Enter value of a: 96
Enter value of b: 125
Enter value of c: 36
36 is greatest

C- Programs - Harsh

  • 1.
  • 3.
    OUTPUT Enter value ofa: 45 Enter value of b: 56 Original value of a is:45 Original value of b is:56 After Swapping New value of a is:56 New value of b is:45
  • 4.
    Output Value of ais: 10 Value of b is: 15 After swapping New Value of a is: 15 New value of b is: 10
  • 5.
    OUTPUT Enter value ofa: 45 Enter value of b: 56 Original value of a is:45 Original value of b is:56 After Swapping New value of a is:56 New value of b is:45
  • 6.
     #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();  } OUTPUT Enter value of a: 25 Enter value of b: 65 Value of a is: 25 Value of b is:65 After swapping Value of a is: 65 Value of b is: 25
  • 7.
     #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();  } OUTPUT Enter 10 elements of array: 5 8 2 12 65 36 98 45 25 96 Max value is: 98 Its location is: 7
  • 8.
     #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: 5 8 2 12 65 36 98 45 25 96 Max value is: 98
  • 9.
     #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();  } OUTPUT Enter 10 elements of array: 5 8 2 12 65 36 98 45 25 96 Smallest of ten values is: 2
  • 10.
     #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();  } OUTPUT Enter 10 elements of array: -5 8 2 12 65 36 98 45 25 96 Smallest of ten values is: - 5
  • 11.
     #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();  } OUTPUT Enter value of a: 56 Enter value of b: 36 56 is greatest.
  • 12.
     #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: 62 Enter value of b: 36 Greatest of two values is:62
  • 13.
     #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();  } OUTPUT Enter value of a: 96 Enter value of b: 125 Enter value of c: 36 36 is greatest

Editor's Notes

  • #4 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.
  • #5 This is same program as the previous one, but it use class structre.
  • #6 It is a program to swap two values without using third variable and without using class structure