SeqSum: Sum of Consecutive Ints Equal to n

  • Thread starter Thread starter Dr.Brain
  • Start date Start date
  • Tags Tags
    Sum
AI Thread Summary
The discussion revolves around creating a C program named seqsum that identifies sequences of consecutive integers summing to a given integer n. The initial code provided compiles without errors but fails to produce output upon execution. Key issues identified include the use of the incorrect format specifier %f in printf, which should be %d for integers, and a suggestion to check if scanf is successfully reading the input. The code logic involves nested loops to calculate sums of sequences, but adjustments are necessary to ensure proper output and functionality.
Dr.Brain
Messages
537
Reaction score
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
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?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top