Home > Enterprise >  Why this function gives me first sums correct and then prints bad sums
Why this function gives me first sums correct and then prints bad sums

Time:12-08

Write a program that prints the sum of digits for the entered interval limits. To calculate the sum of digits form the corresponding function.

#include <stdio.h>

void  suma(int a ,int  b ){
    int s= 0,i;
    
    for(i=a;i<=b;i  ){
        while(i != 0 ){
            int br = i % 10;  
            s =br ;
            i = i/10;  
        }
        
        printf("%d\n",s);

    } 
}

int main(void){
    int a,b; 
    printf("enter the lower limit of the interval: "); scanf("%d",&a);
    printf("enter the upper limit of the interval: "); scanf("%d",&b);
    suma(a,b);
    return 0; 
    
}

when i set a to be 11 and b to be 13 program does first 3 sums but after that it doesent stop.why doesn't it stop. But if i set a to 3 digit number program gives me first sum but then gives me random sums

CodePudding user response:

The reason why your code is not working is because in your while-loop, you are changing the value of i, but i is also used in the for-loop. This results in undefined behaviour. In order to fix this, I would suggest breaking the problem up in two functions. One for calculating the sum of a the digits of a number, and one function that adds these sums in a particular range.

int sumNumber(int number) {
  int sum = 0;
  while(number != 0) {
      sum  = number % 10;
      number /= 10;  
  }

  return sum;
}

int suma(int a ,int  b){
    int totalSum = 0;
    
    for(int i=a;i<=b;i  ){
        int sum = sumNumber(i);
        
        totalSum  = sum;
    } 
    return totalSum;
}

This way, you are not modifying i in the while-loop.

CodePudding user response:

You are mixing up the two loop variables. As arguments are passed by value just a instead of introducing an unnecessary variable. Minimize scope of variables. Check the return value from scanf() otherwise you may be operating on uninitialized variables.

#include <stdio.h>

void suma(int a, int b) {
    for(; a <= b; a  ) {
        int s = 0;
        for(int i = a; i; i /= 10) {
            s  = i % 10;
        }
        printf("%d\n", s);
    }
}

int main(void){
    printf("enter the lower limit of the interval: ");
    int a;
    if(scanf("%d",&a) != 1) {
        printf("scanf failed\n");
        return 1;
    }

    printf("enter the upper limit of the interval: ");
    int b;
    if(scanf("%d",&b) != 1) {
       printf("scanf failed\n");
       return 1;
    }

    suma(a,b);
}

and example run:

enter the lower limit of the interval: 10  
enter the upper limit of the interval: 13
1
2
3
4

I was unreasonably annoyed by how the code was formatted. Extra white space for no reason including at end of line, missing white space between some operations, variables lumped together on one line.

It's a really good idea to separate i/o from logic as in @mennoschipper's answer. My answer is as close to original code as possible.

CodePudding user response:

i did function like this and it works now

void  suma(int a ,int  b ){
int s= 0,i;
int x  ;

for(i=a;i<=b;i  ){
    
    x = i; 
    while(x != 0 ){
        int br = x % 10;  
        s =br ;
        x = x/10;  
    }
    
    printf("%d\n",s);
    s = 0;

} }
  •  Tags:  
  • csum
  • Related