Tutorialspoint

C++ sort elements in lexicographical order

cpp

#include <iostream>
using namespace std;
int main() {
   int i,j;
   string s[5], temp;
   cout<<"Enter the elements..."<<endl;
   for(i = 0; i < 5; ++i)
      getline(cin, s[i]);
   for(i = 0; i < 4; ++i)
      for(j = i+1; j < 5; ++j) {
         if(s[i] > s[j]) {
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
         }
      }
      cout << "The elements in lexicographical order are... " << endl;
      for(int i = 0; i < 5; ++i)
         cout << s[i] << endl;
      return 0;
}

C++ add complex numbers by passing structure to a function

cpp

#include <iostream>
using namespace std;
typedef struct complexNumber {
   float real;
   float imag;
};
complexNumber addCN(complexNumber num1,complexNumber num2) {
   complexNumber temp;
   temp.real = num1.real + num2.real;
   temp.imag = num1.imag + num2.imag;
   return(temp);
}
int main() {
   complexNumber num1, num2, sum;
   cout << "Enter real part of Complex Number 1: " << endl;
   cin >> num1.real;
   cout << "Enter imaginary part of Complex Number 1: " << endl;
   cin >> num1.imag;
   cout << "Enter real part of Complex Number 2: " << endl;
   cin >> num2.real;
   cout << "Enter imaginary part of Complex Number 2: " << endl;
   cin >> num2.imag;
   sum = addCN(num1, num2);
   if(sum.imag >= 0)
   cout << "Sum of the two complex numbers is "<< sum.real <<" + "<< sum.imag <<"i";
   else
   cout << "Sum of the two complex numbers is "<< sum.real <<" + ("<< sum.imag <<")i";
   return 0;
}

C++ add two distances in inch-feet using a structure

cpp

#include <iostream>
using namespace std;
struct DistanceFI {
   int feet;
   int inch;
};
int main() {
   struct DistanceFI distance1, distance2, distance3;
   cout << "Enter feet of Distance 1: "<<endl;
   cin >> distance1.feet;
   cout << "Enter inches of Distance 1: "<<endl;
   cin >> distance1.inch;
   cout << "Enter feet of Distance 2: "<<endl;
   cin >> distance2.feet;
   cout << "Enter inches of Distance 2: "<<endl;
   cin >> distance2.inch;
   distance3.feet = distance1.feet + distance2.feet;
   distance3.inch = distance1.inch + distance2.inch;
   if(distance3.inch > 12) {
      distance3.feet++;
      distance3.inch = distance3.inch - 12;
   }
   cout << endl << "Sum of both distances is " << distance3.feet << " feet and " <<
distance3.inch << " inches";
   return 0;
}

C++ implement variable length arrays

cpp

#include <iostream>
#include <string>
using namespace std;
int main() {
   int *array, size;
   cout<<"Enter size of array: "<<endl;
   cin>>size;
   array = new int [size];
   cout<<"Enter array elements: "<<endl;
   for (int i = 0; i < size; i++)
      cin>>array[i];
   cout<<"The array elements are: ";
   for(int i = 0; i < size; i++)
      cout<<array[i]<<" ";
   cout<<endl;
   delete []array;
   return 0;
}

C++ converts the octal numbers into binary

cpp

#include <iostream>
#include <cmath>
using namespace std;
int OctalToBinary(int octalNum) {
   int decimalNum = 0, binaryNum = 0, count = 0;
   while(octalNum != 0) {
      decimalNum += (octalNum%10) * pow(8,count);
      ++count;
      octalNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      binaryNum += (decimalNum % 2) * count;
      decimalNum /= 2;
      count *= 10;
   }
   return binaryNum;
}
int main() {
   int octalNum = 33;
   cout <<"Octal to Binary"<<endl;
   cout<<"Octal number: "<<octalNum<<endl;
   cout<<"Binary number: "<<OctalToBinary(octalNum)<<endl;
   return 0;
}

C++ binary numbers into octal and the octal numbers into binary

cpp

#include <iostream>
#include <cmath>
using namespace std;
int BinarytoOctal(int binaryNum) {
   int octalNum = 0, decimalNum = 0, count = 0;
   while(binaryNum != 0) {
      decimalNum += (binaryNum%10) * pow(2,count);
      ++count;
      binaryNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      octalNum += (decimalNum % 8) * count;
      decimalNum /= 8;
      count *= 10;
   }
   return octalNum;
}
int OctalToBinary(int octalNum) {
   int decimalNum = 0, binaryNum = 0, count = 0;
   while(octalNum != 0) {
      decimalNum += (octalNum%10) * pow(8,count);
      ++count;
      octalNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      binaryNum += (decimalNum % 2) * count;
      decimalNum /= 2;
      count *= 10;
   }
   return binaryNum;
}
int main() {
   int binaryNum = 1011, octalNum = 25;
   cout <<"Binary to Octal"<<endl;
   cout<<"Binary number: "<<binaryNum<<endl;
   cout<<"Octal number: "<<BinarytoOctal(binaryNum)<<endl;
   cout <<"Octal to Binary"<<endl;
   cout<<"Octal number: "<<octalNum<<endl;
   cout<<"Binary number: "<<OctalToBinary(octalNum)<<endl;
   return 0;
}

C++ GCD of two numbers using recursive Euclid’s algorithm

cpp

#include <iostream>
using namespace std;
int gcd(int a, int b) {
   if (b == 0)
   return a;
   return gcd(b, a % b);
}
int main() {
   int a , b;
   cout<<"Enter the values of a and b: "<<endl;
   cin>>a>>b;
   cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
   return 0;
}

C++ complex number multiplication

cpp

#include<iostream>
using namespace std;
int main(){
   int x1, y1, x2, y2, x3, y3;
   cout<<"Enter the first complex number : "<<endl;
   cin>> x1 >> y1;
   
   cout<<"\nEnter second complex number : "<<endl;
   cin>> x2 >> y2;
   x3 = x1 * x2 - y1 * y2;
   y3 = x1 * y2 + y1 * x2;
   cout<<"The value after multiplication is: "<<x3<<" + "<<y3<<" i ";
   return 0;
}

C++ Program to Solve any Linear Equation

cpp

#include<iostream>
using namespace std;
int main() {
   float a, b, c, d, X;
   cout<<"The form of the linear equation in one variable is: aX + b = cX + d"<<endl;
   cout<<"Enter the values of a, b, c, d : "<<endl;
   cin>>a>>b>>c>>d;
   cout<<"The equation is "<<a<<"X + "<<b<<" = "<<c<<"X + "<<d<<endl;
   if(a==c && b==d)
      cout<<"There are infinite solutions possible for this equation"<<endl;
   else if(a==c)
      cout<<"This is a wrong equation"<<endl;
   else {
      X = (d-b)/(a-c);
      cout<<"The value of X = "<< X <<endl;
   }
}

atof() in C++ language

cpp

#include <bits/stdc++.h> 
using namespace std;
int main() {
   double a;
   char s[20] = "3492.42974200";
   a = atof(s);
   cout << "Converted string into floating point value : " << a << endl; 
   return 0;
}

1 2 3 4 5 6 7 ... 42 Next
Advertisements
Loading...