WSQ11

#<—-notpalindrome

This program was created for the user to give a value and see if it is a palindrome or not.

the program first asks for a number entered by the user, after this, we called a function that converts the integer to a string value. After that, we invert the string so the number looks backwards and we convert it to “int” again. After this new int is created, we compare the original one with the new one (which is backwards) and see if they are the same. If they are indeed de same, then we tell the program to print a message that sais “the number is a palindrome”. To make it look even better, we asked the user for two numbers and then we printed the ones that were palindromes using the first steps, if it wasn’t a palindrome, then we asked the user if he wanted to see if it could be transformed to palindrome. To do this, we created another function that inverts the integer and adds it to its original, (in order to create the new palindrome) and we printed it to the user. If not even like that was the number able to transform into a palindrome, then  we printed a message that said it couldn’t be transformed.

here is the code with notes to be understanded:

<iostream>

<sstream>

<stdio.h>

“string.h”

<algorithm>

using namespace std;

 

int main (){

  int a, b, range, pal;

  string answer;

  int counter = 0;

  cout <<“please enter a value “;

  cin >> (a);

  cout <<“please enter the upper value”<<endl;

  cin >> (b);

 

  range = (b-a);//range of numbers between the ones given by the user

 

  cout <<“the range of the numbers given is  “<< range <<”  n”;

 

 

   for (int i=a; i<=b; i++) {

     ostringstream ostr; //output string stream

     ostr << i; //use the string stream just like cout,

     //except the stream prints not to stdout but to a string.

     std::string textnumber = ostr.str();

     string textnumber2 = textnumber;

     reverse(textnumber.begin(),textnumber.end());

      if (textnumber == textnumber2){

        cout <<” this number is a palindrome!n”<<textnumber<<endl;

      }else {

        cout <<“this number isnt a natural palindrome! n”<<textnumber2<<endl;

 

              //here is where the rest of the code needs to go

                   string str = textnumber;

                   std::stringstream s_str( str );//converts string to int value

                   int r;//new int value asigned for str

                   s_str >> r;//gives r the value of str in int

 

                   int newpalindrome =(r + i);// sums the original digit with its inverse

                   cout <<” “<<newpalindrome << ” “;

                   for (int i=a; i<=b; i++) {

                     ostringstream ostr; //output string stream

                     ostr << newpalindrome; //use the string stream just like cout,

                     //except the stream prints not to stdout but to a string.

                     std::string newpalindrome1 = ostr.str();//creates the string variable that will include the value

                     //                                        of “newpalindrome”

                     string newpalindrome2 = newpalindrome1;// saves the original text to be able to compare both

                     reverse(newpalindrome1.begin(),newpalindrome1.end());

                      if (newpalindrome1 == newpalindrome2){

                        cout <<” this number became a palindrome due to the sum of the original nonpalindrome with its inverse!n”<<newpalindrome1<<endl;

 

 

                    }

                    else {

                      cout <<“sorry, this number is a natural lycherel”<<newpalindrome1 <<endl;

                    }

                    counter = counter + 1;

                  }while (counter < 1);

                  }

 

 

 

             }

 

 

 

 

             return 0;

        }

CC BY 4.0 WSQ11 by carlos green is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.