Register to reply

C program...help

by Dr.Brain
Tags: programhelp
Share this thread:
Dr.Brain
#1
May25-06, 11:46 AM
P: 541
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");
}
}
}
Phys.Org News Partner Science news on Phys.org
An interesting glimpse into how future state-of-the-art electronics might work
Tissue regeneration using anti-inflammatory nanomolecules
C2D2 fighting corrosion
NateTG
#2
May25-06, 12:27 PM
Sci Advisor
HW Helper
P: 2,537
The code tag is your friend...
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?


Register to reply

Related Discussions
Running a Fortran 77 program in a C++ enviorment/program Programming & Computer Science 2
How To Program? Programming & Computer Science 7
Help with a C Program Programming & Computer Science 2
About program a OS ? Programming & Computer Science 5
Astro simulation program problem - program bugs? Astronomy & Astrophysics 4