Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C program help

  1. May 25, 2006 #1
    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");
    }
    }
    }
     
  2. jcsd
  3. May 25, 2006 #2

    NateTG

    User Avatar
    Science Advisor
    Homework Helper

    The code tag is your friend...
    Code (Text):

    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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C program help
  1. Help with a C Program (Replies: 2)

  2. C program help (Replies: 31)

  3. C program help (Replies: 3)

  4. Help in C programming (Replies: 1)

Loading...