Thread: Simple C Program Loops

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Simple C Program Loops

    I'm unable to find the correct way to write these loops. Could anyone help?

    5. Write a program that reads a sequence of pairs of positive integer values. For each pair, print all the
    integer values starting with the first and ending with the second on a single line. If the two values are
    the same, terminate your program. For example, if the input is
    8 12
    9 2
    -2 5
    0 -4
    8 8
    then print:
    8 9 10 11 12
    9 8 7 6 5 4 3 2
    -2 -1 0 1 2 3 4 5
    0 -1 -2 -3 -4

    3. Write a program that reads a sequence of positive integers and prints out their sum, except that if the
    same number occurs several times consecutively, ignore all but the first. Assume an input of 0 marks
    the end of the input. For example, if the input is 3 8 5 5 4 9 1 1 1 1 8 0 then you should print 38 (i.e.,
    ignore one of the 5's and three of the 1's).

    1. Write a program that reads a sequence of positive integer values from the user and prints the single
    line “the numbers are increasing” if each value entered is strictly greater than the previous one
    entered. If the numbers are not increasing, print the single line “the numbers are not increasing”.
    When the user enters the number 0 that indicates the end of the input and not considered w.r.t.
    whether the numbers are increasing or not.
    For example, if the input is 4 7 9 10 14 0, then your program should print: the numbers
    are increasing. If the input is 1 2 2 3 0, then your program should print: the numbers
    are not increasing. If the input is 1 7 4 9 3 0, then your program should print the:
    numbers are not increasing. You must read all the numbers before you print anything.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    What have you tried so far? You can post code even if it's not working...

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    for #5 this is what I have so far.
    it is not working.

    Code:
    # include <stdio.h>
    int main(){
    int a, b, i;
    scanf("%d",&a);
    scanf("%d",&b);
    while(a != b){
        printf("%d %d",a,b);
        scanf("%d",&a);
        scanf("%d",&b);
        for(i=a; i<=b; i+=1){
            printf("%d",i);
        }
    }
    }

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Does the position where you get another two numbers seem correct to you? I.e. the part I've commented as /* Get another two numbers (a and b) */

    After you get that fixed, don't forget to account for cases where b < a

    Code:
    # include <stdio.h>
    
    int main()
    {
        int a, b, i;
        
        /* This is getting two integers, which is the range. If a == b
         * then finish
         */
        scanf("%d",&a);
        scanf("%d",&b);
        
        while(a != b) {               /* While not finished, do */
            
            printf("%d %d",a,b);        
            
            scanf("%d",&a);           /* Get another two numbers (a and b) */
            scanf("%d",&b);
            
            for(i=a; i<=b; i+=1) {    /* Print each number from a to b inclusive */       
                printf("%d",i);
            }
        }   
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    I'm not quite sure how to do that. I'm new to C. Could you show me what you're talking about?
    Thanks

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    Code:
    void foo()
    {
        int a, b, i;
        scanf("%d", &a);
        scanf("%d", &b);
        while (a != b)
        {
            if(a<b)
            for (i = a; i <= b; i += 1)
                printf("%d ", i);
            else
            for (i = a; i >= b; i -= 1)
                printf("%d ", i);
            
    
            printf("\n");
            scanf("%d", &a);
            scanf("%d", &b);//<--- notice the change in posistioning of scan statements
        }
    }

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    Thank you!
    Last edited by sgk1498; 02-27-2014 at 07:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making simple patters from loops
    By ryanrox in forum C Programming
    Replies: 12
    Last Post: 01-28-2012, 10:35 PM
  2. Simple question on while loops
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 07-22-2011, 09:29 AM
  3. Simple C Program [for loops?]
    By mas0 in forum C Programming
    Replies: 6
    Last Post: 11-18-2006, 05:53 PM
  4. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 09:47 AM
  5. simple program on loops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-11-2002, 10:24 AM

Tags for this Thread