Connecting Tech Pros Worldwide Help | Site Map

Print number as a sum of sequential numbers

Member
 
Join Date: Jan 2009
Posts: 36
#1: Aug 9 '09
why the print loop is not alright ?
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main()
  3. {
  4.     int n, i, X, temp;
  5.     printf("N=?\n");
  6.     scanf("%d",&n);
  7.     for(i=1; i<n; i++)
  8.     {
  9.         temp=i;
  10.         X=0;
  11.         for(i; i<n; i++)
  12.         {
  13.             X=X+i;
  14.             if(X==n)
  15.             {
  16.                 i=temp;
  17.                 X=0;
  18.                 for(i; X<n; i++)
  19.                 {//loop for printin the sum
  20.                     X=X+i;
  21.                     printf("%+d",X);
  22.                 }
  23.             }
  24.  
  25.         }
  26.         i=temp;
  27.         X=0;
  28.     }
  29. getchar();
  30. }
  31.  
  32.  
adding a a simple (program the 1th option) .
Attached Files
File Type: zip exe.zip (14.9 KB, 6 views)
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#2: Aug 10 '09

re: Print number as a sum of sequential numbers


In what way is the loop not alright?

The difference between what you expect and what the program actually does is usually a significant clue to what is wrong. Don't withhold that clue from us.
Member
 
Join Date: Jan 2009
Posts: 36
#3: Aug 10 '09

re: Print number as a sum of sequential numbers


actually the probles is that the loop dosnt doin what expected .

for example:
if the input 9 should print 2+3+4
4+5

for 21 should print:

1+2+3+4+5+6
6+7+8
10+11

but for some reasons theres a problem just in the print loop.
because if ichange the program like this :
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main()
  3. {
  4.     int n, i, X, temp;
  5.     printf("N=?\n");
  6.     scanf("%d",&n);
  7.     for(i=1; i<n; i++)
  8.     {
  9.         temp=i;
  10.         X=0;
  11.         for(i; i<n; i++)
  12.         {
  13.             X=X+i;
  14.             if(X==n)
  15.             printf("%d", i);
  16.         }
  17.         i=temp;
  18.         X=0;
  19.     }
  20. getchar();
  21. }
  22.  
  23.  
the condition is good .
cause the program print the last of sum .
for example :
for 9 the print :
4 5
for 21;
6 8 11

so the problem iguss is in the loop that should print the sum
(lines 15-22) in the first program .

TNX ....
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,158
#4: Aug 10 '09

re: Print number as a sum of sequential numbers


X<n should be i<n maybe?

"%+d" should be "+%d" maybe?

In general what you are doing, reusing calculation variables to print results in the middle of a calculation often leads to errors. Why not try writing a function that outputs the result when you have found it. That would separate the calculation from the output which is always a good thing. Better yet write 2 functions 1 that returns results and one that prints results and just keep calling them until there are no more results.


As an aside in all you code in all you posts you always use

void main()

Now a few compilers support this as an extension but the C language standard states that this is undefined behaviour. Undefined behaviour is bad, it is literally undefined in the standard and the compiler can do anything (or indeed nothing) including apparently working correctly in some situations and not in others.

main should return int so get in the habit of writing

int main()

now!
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Aug 10 '09

re: Print number as a sum of sequential numbers


Do these numbers have to be consecutive? If so an easy linear relation holds: the sum can be written as (X+0)+(X+1)+(X+2) ... (X+n) == (n+1)*X + (n+1)*n/2 == (n+1)*(X+n/2). So n+1 has to be a factor of S (the sum). For any positive X satisfying the equation you have found your solution. No need for loops.

kind regards,

Jos
Member
 
Join Date: Jan 2009
Posts: 36
#6: Aug 10 '09

re: Print number as a sum of sequential numbers


ok... , TNX .
appreciate your great work ! (-:
Reply