SeqSum: Sum of Consecutive Ints Equal to n

  • Thread starter Dr.Brain
  • Start date
  • Tags
    Sum
In summary, the conversation discusses a program called seqsum that reads an integer n from the keyboard and prints all sequences of consecutive integers whose sum is equal to n. Example output for an input of 30 would be 4,5,6,7,8, 6,7,8,9, and 9,10,11. It also mentions a code that the speaker wrote, but it does not seem to be functioning properly. Some suggestions are given, such as using code tags and checking the return value of scanf.
  • #1
Dr.Brain
538
2
Write a program called seqsum (source code: seqsum.c) that reads an integer n from the keyboard, and that prints all sequences of consecutive integers whose sum is equal to n. For example, if the input was 30, the program should print out
4,5,6,7,8
6,7,8,9
9,10,11

--------------------------------------------------------

This is the code i wrote...and it gives no errors...but after running it when i input the number 'n' and press enter..it does nothing... :(

main()
{
int i,j,k,m,n;
printf("Input number : \n");
scanf("%d" , &n);
for(i=1;i<=(n/2);i++)
{ k=0;
for(j=i;j<=(n/2+1);j++)
{ k+=j;
if(k==n)
{ for(m=i;m<=j;m++)
printf("%f \t ",m); }
printf("\n");
}
}
}
 
Technology news on Phys.org
  • #2
The code tag is your friend...
Code:
main() {
   int i,j,k,m,n;
   printf("Input number : \n");
   scanf("%d" , &n);
   for(i=1;i<=(n/2);i++) {
      k=0;
      for(j=i;j<=(n/2+1);j++) {
         k+=j;
         if(k==n) {  
            for(m=i;m<=j;m++)
               printf("%f \t ",m);
         }
         printf("\n");
      }
   }
}

Just for fun, you might want to check whether scanf is returning 0.
Why are you using %f in printf when m is an integer?
 
  • #3


--------------------------------------------------------

Your code looks mostly correct, but there are a few issues that could be causing it to not run as expected.

1. The first issue is that you are using the wrong format specifier in your printf statement. Since you are printing integers, you should use %d instead of %f.

2. Another issue is that you are using a floating-point variable (m) in your for loop instead of an integer. This could cause unexpected results and potentially an infinite loop.

3. Lastly, your code is missing a main function declaration. It should be declared as "int main()" instead of just "main()".

After making these changes, your code should run properly. Additionally, you may want to add some error handling in case the user inputs a negative number or zero, as well as a message if no sequences are found for a given number. Overall, good job on attempting to solve the problem and I hope this helps!
 

FAQ: SeqSum: Sum of Consecutive Ints Equal to n

1. What is SeqSum?

SeqSum is a mathematical operation that involves finding the sum of consecutive integers that add up to a given number, n.

2. How is SeqSum calculated?

SeqSum is calculated by dividing the given number, n, by 2 and then multiplying it by the number of consecutive integers. For example, if n = 10, the sum of consecutive integers would be 10/2 * 2 = 10.

3. What is the purpose of SeqSum?

The purpose of SeqSum is to find the number of consecutive integers that add up to a given number, which can be useful in a variety of mathematical and scientific calculations.

4. Can SeqSum be used to find the sum of any consecutive integers?

Yes, SeqSum can be used to find the sum of any consecutive integers, as long as the given number, n, is a positive integer.

5. Are there any limitations to using SeqSum?

One limitation of SeqSum is that it can only be used with positive integers. Additionally, it may not be the most efficient method for finding the sum of consecutive integers in certain situations.

Back
Top