Answer to Question #192479 in C++ for Madeshwaran

Question #192479

Implement a C++ program to define function named as ext, to extract the digits that located at even and odd position of five digit number and show the result.


Runtime Input :

00156


Output :

016

05


1
Expert's answer
2021-05-12T14:56:41-0400
#include <iostream>
#include <string>
using namespace std;
void ext(string s){
    int len=s.length();
    string sEven;
    string sOdd;
    for(int i=0;i<len;i++){
        if ((i+1)%2==0){
            sEven=sEven+s[i];
        }
        else{
            sOdd=sOdd+s[i];
        }
    }
    cout<<"\nDigits at odd positions are: "<<sOdd<<endl;
    cout<<"\nDigits at even positions are: "<<sEven<<endl;

}
int main(){
    string str1;
    cout<<"Enter a five digit number: ";
    cin>>str1;
    ext(str1);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment