samedi 24 novembre 2018

the sum of digits in c not including the repeating digit

As i mentiond the goal is to sum all digits in a number not including the repeating one for instance : 975444579 is 25 and not 54 .i have come this far, the problem with this code is that its checking evrey digit how can i make the computer not to check a digit more than once, how can i make the computer check if a varible was ever equal to its value before?

#include <stdlib.h>
#include <stdio.h>

//i looked at my lesson 5 task 9 HW
#define ULTIMATE_10 10

int main()
{
    int sum = 0, check = 0, num = 0, i = 0, try = 0, sSum = 0, flag = 0, pNum = 0, shimon = 0, test2 = 0;

    printf("Enter a no. to reverse \n");
    scanf("%d", &num);
    if (num <= 0)
    {
        printf("this is not a vaild no pls rnter another one \n");
        scanf("%d", &num);
    }
    const int test = num;
    printf("the orginal no is : %d \n", num);
    while (num != 0)
    {
        //test = num;

        check = num % ULTIMATE_10;
        sSum = sSum + check;


        for (flag = 0, try = 0, i = 0, shimon = test; flag <= sSum; flag++)
        {

            i = shimon % ULTIMATE_10;
            //printf("%d, %d \n", i, check);


            if (check == i)
            {
                try = try + i;
                printf("%d, %d \n", try, check);

            }
            shimon = shimon / ULTIMATE_10;
            //printf("%d \n",num);
        }

        if (try > check)
        {
            sum = sum - try + check;
            printf("%d,\n", sum);
        }

        num = num / ULTIMATE_10;

        //printf("%d \n", sSum);
        //printf("%d \n",num);
    }

    printf("the sum is:%d", sSum + sum);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire