jeudi 22 novembre 2018

sum of numbers without the repeating one in c

hi i need to sum the no of digits of number i chose in c languge. in addition if the number has twice or more the same digit i gonna need to sum it once. for example the number: 2324 the sum = 9 or 5555 the sum is 5. i wrote this code can u help me fix it?

#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, pNum =0, counter = 0, i = 0,try=0, sSum = 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);
    }
    printf("the orginal no is : %d \n",num);
    while (num!=0)
    {




        check = num%ULTIMATE_10;


        while (num!=0)
        {

            i = num%ULTIMATE_10;


            if (check==i)
            {
                try=+i;
            }
            num = num/ULTIMATE_10;
            if(try>check)
            {
                sum=sum-try+i;
            }
        }



        sSum =sum+ check ;




        num = num/ULTIMATE_10;
        printf("%d, %d \n", sum, sSum);
        try = 0;
    }

    printf("the sum is:%d",sSum);


    return 0;

}

Aucun commentaire:

Enregistrer un commentaire