You must Sign In to post a response.
  • Category: Programming

    Write a C++ program to print the required output using for loop.


    Preparing for Object Oriented Programming exam? Looking out for the correct solution for solving a problem using for loop? On this page ISC experts shall provide you with a program solution and detailed explanation to the problem.

    Following is a question that was asked in the question paper of B.E. E&E(December 2016)conducted by Bangalore University, for subject Object Oriented Programming(2K11 Scheme)(EE-305), Semester-III

    This question is asked for 10 marks and hence requires some explanation. Please let me know the solution.

    Q) Differentiate between break and continue statements. Write a program to print the following output using for loop. [10 Marks]

    Output required
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
  • Answers

    2 Answers found.
  • Below is the program to prepare required output. Please include iostream.h and conio.h header files before declaration of class.
    //PROGRAM TO PREPARE REQUIRED OUTPUT USING FOR LOOP.
    #include
    #include
    class sample
    {
    private:
    int n,r,c,d;
    public:
    void get(int t)
    {
    n=t;
    d=1;
    }
    void display(void)
    {
    cout<<"\nRequired output for "< for(r=1;r<=n;r++)
    {
    d=1;
    for(c=1;c<=r;c++)
    {
    cout< d++;
    }
    cout<<"\n";
    }
    }
    };
    void main(void)
    {
    class sample s;
    clrscr();
    int x;
    cout<<"\nEnter number of lines you needed\n";
    cin>>x;
    s.get(x);
    s.display();
    getch();
    }

    output-of-required-output-using-for-loop.docx

    Delete Attachment

  • Break and Continue statement is basically use for the change the flow of a program. Below is detail description about these both statement:

    Break Statement -: Break statement is use in that case when you want to stop the further execution of the program. It is use with conditional statement(if..else). Below is the small exmple of Break statement.
    #include
    int main(void) {
    // your code goes here
    for(int i = 1; i <= 10; i++)
    {
    if(i > 5)
    {
    break;
    }
    else
    {
    printf("%d\n", i);
    }
    }
    return 0;
    }
    Note: As you can see in the above example, if i greater than 5 only in that case it will terminate the program but if i is 5 or less than 5 then it will process the code.

    Continue Statement -: Continue statement is use in that case when you want to skip some statement in program.It is also use with conditional statement(if..else). Below is the small exmple of Continue statement.
    #include

    int main(void) {
    // your code goes here
    for(int i = 1; i <= 10; i++)
    {
    if(i == 5 || i == 7)
    {
    continue;
    }
    else
    {
    printf("%d\n", i);
    }
    }
    return 0;
    }
    Note: As you can see in the above example, if i is 5 or 7 then this program skip these both from output and continue with next iterations.


    Below is the program for print the required output:

    #include
    using namespace std;
    int main() {
    // your code goes here
    for(int i = 1; i <= 5; i++)
    {
    for(int j = 1; j <= i; j++)
    {
    cout< }
    cout<<"\n";
    }
    return 0;
    }


  • Sign In to post your comments