1

(Purpose of the code is write in the title) my code work only if i put the same number once and in the end like - 123455 but if i write 12345566 is dosent work or 11234 it dosent wort to someone know why? i have been trying for a few days and i faild agine and agine.

while(num)
{
    dig = num % 10   // dig is the digit in the number                          
    num /= 10        // num is the number the user enter
    while(num2)      // num2 = num
    {
        num2 /= 10
        dig2 = num2 % 10   // dig2 is is the one digit next to dig  
        num2 /= 10
    
        if(dig2 == dig)    // here I check if I got the same digit twice to 
                           // not include him

        {
            dig2 = 0
            dig = 0
        }
    }
    sum = sum + dig + hold
}
printf("%d", sum)
MDXZ
  • 160
  • 12
Roee Attias
  • 45
  • 1
  • 7
  • thay are all integers so thay dont ger the division – Roee Attias Dec 28 '20 at 08:32
  • 2
    Perhaps you can find some ideas [here](https://stackoverflow.com/a/65459705/2979617). – M Oehm Dec 28 '20 at 09:07
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Dec 28 '20 at 11:19

3 Answers3

0

Well your code's almost correct but there's are somethings wrong ,

When you declared num2 to be equal to num1 it was out of the loop so as soon as one full loop execution is done , num2 still remains to be less than zero or to be zero if it was a unsigned int, so according to the condition the second loop wont execute after its first run.

So mind adding ,

num2 = num1 

inside your first loop .

Also your updating num2 twice which i think you wont need to do after the first change .

Full code which i tried

#include<stdio.h>

int main(void)
{
    int num;
    int num2;
    int sum = 0;
    int dig, dig2;
    scanf_s("%d", &num);
  
    while (num>0)
    {
        dig = num % 10;  //dig is the digit in the number                           
        num /= 10;  
        num2 = num;// num is the number the user enter
            while (num2>0)    // num2 = num
            {
                dig2 = num2 % 10; // dig2 is is the one digit next to dig  

                    if(dig2 == dig)   // here i check if i got the same digit twice to                                       //not include him
                    {
                            dig = 0;
                    }
                    num2 /= 10;
            }
            sum = sum + dig ;
    }
    printf("%d", sum);
    return 0;
}
yugsharma1711
  • 71
  • 1
  • 9
0

O(n)

    #include <stdio.h>
    int main () {

        unsigned int input = 0;
        printf("Enter data : ");
        scanf("%u", &input);
        int sum = 0;
        int dig = 0;
        int check[10] = {0};
        printf("Data input: %u\n", input);
        while(input) {
           dig = 0;
           dig = input%10;
           input = input/10;
           if (check[dig] == 0) {
               check[dig]++;
               sum += dig;
           }
        }
        printf("Sum of digits : %d\n", sum);

        return 0;
    }
Nishad C M
  • 142
  • 6
0

My program uses a character array (string) for storing an integer. We convert its every character into an integer and I remove all integers repeated and I calculate the sume of integers without repetition

My code :

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

int remove_occurences(int size,int arr[size])
{int s=0;
   for (int i = 0; i < size; i++)
   {
       for(int p=i+1;p<size;p++)
       {
           if (arr[i]==arr[p])
           {
              for (int j = i+1; j < size ;j++)
              {
                 arr[j-1] = arr[j];
              }
              size--;
              p--;
           }
       }
  }

  for(int i=0;i<size;i++)
  {
       s=s+arr[i];
  }
  return s;

}

int main()
{
   int i=0, sum=0,p=0;
   char n[1000];
   printf("Input an integer\n");
   scanf("%s", n);
   int T[strlen(n)];
   while (n[i] != '\0')
   {
        T[p]=n[i] - '0'; // Converting character to integer and make it into the array
        p++;i++;
   }

   printf("\nThe sum of digits of this number :%d\n",remove_occurences(strlen(n),T));

  return 0;

}

Example :

Input : 1111111111 Output : 1

Input : 12345566 Output : 21

Or ,you can use this solution :

#include <stdio.h>
#include <stdbool.h>

bool Exist(int *,int,int);

int main ()
{
    unsigned int n = 0;
    int m=0,s=0;
    printf("Add a number please :");
    scanf("%u", &n);
    int T[(int)floor(log10(abs(n)))+1];
    // to calculate the number of digits use : (int)floor(log10(abs(n)))+1
    printf("\nThe length of this number is : %d\n",(int)floor(log10(abs(n)))+1);
    int p=0;
    while(n!=0)
    {
         m=n%10;
         if(Exist(T,p,m)==true)
         {
               T[p]=m;
               p++;
         }
         n=n/10;
    }
    for(int i=0;i<p;i++)
    {
          s=s+T[i];
    }
    printf("\nSum of digits : %d\n", s);
    return 0;
}

 bool Exist(int *T,int k,int c)
 {
    for(int i=0;i<k;i++)
    {
        if(T[i]==c)
        {
            return false;
        }
    }
    return true;
 }
MED LDN
  • 684
  • 1
  • 5
  • 10