简体   繁体   English

如何将数字变成两位数

[英]How to double digits in number

I am having a problem on how to double digits of any number.我在如何将任何数字的两位数加倍时遇到问题。

For example, the number: 12345 output would be 1122334455 using functions and loops.例如,数字:12345 使用函数和循环输出将是 1122334455。

#include <stdio.h>
int main() {
int num;
printf("Please Enter a number");
scanf("%d",&num);
for(int i=0;i<=num%10;i++) {
       if(i==num%10)
             newNum+=i;
for(int i=1;i<=num%10;i++) {
       if(i==num/10%10)
             newNum+=i;

I am assuming that you do not have to store the value with duplicated digits, as storing it as int will quickly overflow.我假设您不必存储具有重复数字的值,因为将其存储为 int 会很快溢出。 If you have to, you can use long long or an array.如果必须,您可以使用 long long 或数组。

Your for loop does not make sense.你的 for 循环没有意义。 You have to loop until all intergers have been duplicated.你必须循环直到所有的整数都被复制。 To do so, determine the ones place with mod 10, then divide number by 10. It will loop until number is 0. Try this.为此,请使用 mod 10 确定个位,然后将 number 除以 10。它将循环直到 number 为 0。试试这个。

#include <stdio.h>

int main(void) {

    int number;
    int temp;

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

    while(number) {
        temp = number % 10;
        printf("%d%d", temp, temp);
        number /= 10;
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM