简体   繁体   中英

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.

#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. If you have to, you can use long long or an array.

Your for loop does not make sense. 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.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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