How to add comma in long number in c++ program? Coding Interview Question.

solution of How to add comma in long number in c++ program? Calculate Hamming distance between two Integers in C++? C++ Coding Interview Question.

How to add comma in long number in c++ program?

 
#include <iostream>
#include <string>
using namespace std;
string MathChallenge(int num1, int num2) {
  
  // code goes here 
  int test=num1/num2;
  if(test>999){
    auto t=to_string(test);
    int s =t.length()-3;
    while(s>0){
      t.insert(s,",");
      s-=3;
    }
    return t;
  }

  return to_string(test);

}

int main(void) { 
   
  // keep this function call here
  
  cout << MathChallenge(564889,80);
  return 0;
    
}

Calculate Hamming distance between two Integers in C++

 
#include <iostream>
#include <string>
using namespace std;

string ArrayChallenge(string strArr[], int arrLength) {
  
  // code goes here  
  int no=0;
  int sum=0;
      for (int i=0 ; i<strArr[0].length();i++)
      {        
        if( strArr[0][i]!=strArr[1][i])
        {
          no+=1;         
        }
      }

return to_string( no);
}

int main(void) { 
   
  // keep this function call here
  string A[] = {"10110","10100"};
  int arrLength = sizeof(A) / sizeof(*A);
   cout << ArrayChallenge(A, arrLength);
  return 0;
    
}

C++ Coding Interview Question.

 
#include <iostream>
#include <string>
using namespace std;

// Function initilization.
string ArrayChallenge(int arr[], int arrLength) 
{
  int flag=0;
  int flag2=0;
  int diff=arr[0]-arr[1];
  // code goes here 
  for(int i=0; i<arrLength-1;i++){
    if( diff==arr[i]-arr[i+1]){
      flag=1;
    }else{
      flag=0;
    }
    if (arr[i+1]%arr[i]==0){
      flag2=1;
    }else{
      flag2=0;
    }
  } 
  if(flag==1){
    return "Arithmetic";
    }
    else if(flag2==1){
      return "Geometric";
    }
    else{  
    }
    return to_string(-1);
}

int main(void) { 
   
  // keep this function call here
  // Condition 1: if number has common Difference so it will printed "Arithmetic". 
  // Condition 2: if number has common Multiplaction so it will printed "Geometric". 

  int A[] = {5,10,15,20}; // case 1: Difference of 5
  int B[] = {2,6,18,54};  // case 2: Multiple of 3
  int arrLength1 = sizeof(A) / sizeof(*A);
  int arrLength2 = sizeof(B) / sizeof(*B);

  cout << ArrayChallenge(A, arrLength1) <<"\n\n";
  cout << ArrayChallenge(B, arrLength2);

  return 0;
    
}

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment