在C编程中设置循环限制并计算回文率 [英] Setting a Limit on loops and calculating palindromes in C programming

查看:92
本文介绍了在C编程中设置循环限制并计算回文率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,此代码的目标是将用户编号转换为回文.如果该数字不是回文,请计算该数字+倒数,直到它成为回文.如果仍然不是回文,请取最后一个数字的和,然后取反,然后加起来,最多10次.

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?

推荐答案

您可以做的是在另一个while循环(视为循环1)中编写while循环以生成数字的倒数(视为循环2).终止)),如果给定的数字不是回文,则将数字更改为number = number + reverse.如果要尝试10次,则可以为循环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++;   
         }
 }

这篇关于在C编程中设置循环限制并计算回文率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆