Setting a Limit on loops and calculating palindromes in C programming

时光怂恿深爱的人放手 提交于 2019-12-25 03:56:36

问题


So the goal of this code is to convert a user number into a palindrome. If the number is not a palindrome, calculate the number+reversed number until it becomes a palindrome. If still not a palindrome, take the sum of the last number and it's reverse and add that together, for a maximum of 10 tries.

int main()
{
    int n;
    int reverse = 0;    //initial value will be 0
    int temp;           //temporary variable

//Check if number is a palindrome===============================

    printf("Enter an integer: ");
    scanf_s("%d", &n);
    temp = n;           //Make input number have a temporary variable

    while (temp != 0)
    {
        reverse = reverse * 10;
        reverse = reverse + temp % 10;
        temp = temp / 10;
    }

//Check if number entered by user and it's reverse number is equal
if (reverse == n)
    printf("%d\t1, reverse is %d\n", n, reverse);    //Is a palindrome
else
    printf("%d\t0, reverse is %d\n", n, reverse);   //Not a palindrome
//==========================================================================

//Keep adding numbers until it reaches a palindrome
    int sum;

    while (n /= reverse)
    {
        sum = n + reverse;
        n++;
    }

if (reverse == sum)
    printf("%d it works", sum, reverse);
else
    ("%d didn't work", sum, reverse);

I haven't worked on the limit yet. But my question is how would I go about adding the reverse and user's number, then doing the sum + its reverse? Also what kind of loop is best for this kind of question?


回答1:


What you can do is write the while loop for generating the reverse of number(consider as loop2) in another while loop(consider as loop 1) which breaks(terminates) when the the number is a palindrome.If the given number is not palindrome then you change the number as number=number+reverse.If you want to try for 10 tries then you can add a another condition for loop 1 by taking a counter. Below is the code that I wrote.

#include<stdio.h>
int main()
{
    int n;
    int count=0;         //taking counter for number of trials
    int reverse=0;      //initial value will be 0
    int temp;           //temporary variable

    printf("Enter an integer: ");
    scanf("%d", &n);

    while (count<10)
    //count less than 10 condition makes the max. no. of trials equal to 10   
    {   
        temp=n;   //assigning a temporary variable to n
        reverse=0; 
        //Finding the reverse of the number n===============================
        while (temp != 0)    
        {
            reverse = reverse * 10;
            reverse = reverse + temp % 10;
            temp = temp / 10;
        }

        //Check if number entered by user and it's reverse number is equal
        if (reverse == n){
            printf("\n%d\t1, reverse is %d\n", n, reverse);
            //Is a palindrome
            break; 
            /*break condition makes the loop 1 to terminate if the number 'n' is palindrome*/  
        }
        else
            printf("\n%d\t0, reverse is %d\n", n, reverse);   
            //Not a palindrome
        //================================================================

        //Keep adding numbers until it reaches a palindrome
        n=n+ reverse;  
        count++;   
         }
 }


来源:https://stackoverflow.com/questions/33747756/setting-a-limit-on-loops-and-calculating-palindromes-in-c-programming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!