using for loop

Discussion in 'C' started by necko, Mar 10, 2012.

  1. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hello Guys I am beginner in turbo c programming please help me in this program advance thank you for those who help me :))


    This are the problem:


    1.Given an input n assumed one digit, display a pattern.
    Example : if n=5,display

    1_2_3_4_5
    1_2_3_4
    1_2_3
    1_2
    1

    ^PYRAMID^


    2.Write a program that find the highest odd integer among the values entered by the user . Stop asking values when a values less than 1 have been entered. If no odd integer is entered, display "NO ODD INTEGER

    example: if the values entered were 3 8 1 6 9 4 -5, then display 9
    if the values entered were 2 4 6 8 10 -5, then display NO ODD INTEGER.


    using for loop :)

    Help me please Thank You ^_^
     
  2. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Re: using for loop/c program

    Hello Guys I am beginner in turbo c programming please help me in this program advance thank you for those who help me :))


    This are the problem:


    1.Given an input n assumed one digit, display a pattern.
    Example : if n=5,display
    1_2_3_4_5
    1_2_3_4
    1_2_3
    1_2
    1​
    [/CENTER]
    ^PYRAMID^


    2.Write a program that find the highest odd integer among the values entered by the user . Stop asking values when a values less than 1 have been entered. If no odd integer is entered, display "NO ODD INTEGER

    example: if the values entered were 3 8 1 6 9 4 -5, then display 9
    if the values entered were 2 4 6 8 10 -5, then display NO ODD INTEGER.


    using for loop :)

    Help me please Thank You ^_^
     
  3. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    the first one needs nested loops. the outer loop should start at N and run while it's greater than 0.

    The inner loops can be tricky:

    1) you need to output spaces to center the pyramid, so if you create a loop that starts at N - outer loop's value and run while it's greater than 0, you should be able to center each line.
    2) you need an underscore after each digit so long as it isn't the last thing printed. you can do that using "%s" and the ternary operator (?). As you loop through to write the sequence, check the loop control against the outer loop's control var. If it's less, output the underscore... otherwise, write an empty string "".

    Code:
    for(outer_var starting at N; outer_var > 0; --outer_var) {
       for(inner_var starting at N - outer_var; inner_var > 0; --inner_var)
          write a space
       for(inner_var starting at 1; inner_var <= outer_var; ++inner_var)
          write out sequence
       put a new line
    }
    for the second one, create a couple of vars - one for input and the other for the highest odd value. The odd variable should be set to -1 or such. the only tricky part here is checking for the input being >= 0 and if it is, checking whether or not it's even or odd. if it's odd, check your odd variable for being less than the current input's value.

    Code:
    ... get input
    if(input >= 0 && input % 2 != 0 && input > odds_current_value)
       store input in odd variable
    
    ... check result and show appropriate message
    
    give it a whirl and post if you have problems.
    hope that helps
     
  4. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    thank you Hobbyist it help a lot :))

    can you help me again in this program

    Code:
    #include<stdio.h>
    
    main()
    {
       int n, c, k, space;
    
    
       printf("Enter Number of row:");
       scanf("%d", &n);
    
    
       space = 0;
    
       for ( k = n ; k >= 1 ; k-- )
       {
           for ( c = 1 ; c <= space ; c++ )
           printf(" ");
    
           space++;
    
           for ( c = 1 ; c <= k ; c++)
          printf("%d_", c);
    
           printf("\n");
       }
    
       return 0;
    }
    
    when i run this program the output is like this

    1_2_3_4_5_
    1_2_3_4_
    1_2_3_
    1_2_
    1_

    how can i remove the excess underscore ?

    but i want to make is like this

    1_2_3_4_5
    1_2_3_4
    1_2_3
    1_2
    1
     
  5. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Glad to help if and where I can. :)

    Code:
    printf("%d%s", c, c < k ? "_" : "");
    
    The only stickler there is if n is greater than 9; if it is, you'll have to play around with the format modifiers to keep everything centered.

    Good job!
     
  6. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    thank you very much hobbyist very awesome



    can you help me again in this program ahaha

    this is the code


    #include <stdio.h>
    void main()
    {
    int a,i,highest;
    clrscr();
    printf("Enter 10 Number\n\n");
    for(i=0;i<a;i++)
    scanf("%d",&a);
    highest=a[0];
    for(i=0;i<a;i++)
    {
    if(a>highest)
    highest=a;
    else

    }
    printf("\nHighest Number is=%d",highest);
    getch();
    }


    I get the highest integers but i want to stop the program when I input negative numbers and print out the highest number. and also when I input 2,4,6,8,10 -1 the output is NO ODD NUMBERS and print the highest value

    where I can put the condition or what is the condition to do that ?
     
  7. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Is this the same program as part 2 or is it something different? If it's the same, then all you'd really need is something like

    Code:
    int number, highest = -1;
    
    for(; ((scanf("%d", &number)) == 1) && number > 0; )
       // if you're looking for an odd integer being highest
       if(number % 2 !=0 && number > highest)
          highest = number;
       // if you're just looking for the highest number
       if(number > highest)
          highest = number;
    
    so long as the user enters valid integers separated by spaces, scanf("%d", &number) == 1 will read as many numbers as entered until one of them is less than or equal to 0. && number > 0 will exit the loop when that is the case.

    if the user enters an alpha or punc character, scanf will fail; at that point the stream is in an error state and may run loopy.

    If you're looking for the highest odd and/or no odds being entered, just compare highest with -1 after the input loop exits. if its value is still -1, then no odds were entered. If you're looking for both the highest value and the highest odd value, then add another variable... one for the highest number and one for the highest odd value.

    hope that helps.
     
  8. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    thank you for helping me Hobbyist. Good Job :) It help a lot and I learn :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice