FOR Loops




Manual # 5




Topic:

FOR Loops





Subject:
Introduction to Computing





Author:
Sunawar Khan Ahsan









------------------------------------------------------------------------------------------------------------
Note: Please don’t read these manual just for reading purpose, read these manuals until it make some sense.
------------------------------------------------------------------------------------------------------------

#include<iostream.h>

int main()
{
    float g;

    g=3/2;

    cout<<g;

    cout<<"\n\n";

    return 0;
}

Output of this program would be 1 not 0.5, this is because of the reason that you declared g as float and you are doing the division in integers like (3/2) so for right calculation one of the digit should be float like 3.0/2 to make them float.

But, if you will use a float variable with integer variable and stores its information in float variable, then it will work alright as in calculation one of the variable i.e. f is float.

#include<iostream.h>

int main()
{
    int i;
    float f,j;

    i=7;
    f=2;

    j=f/i;

    cout<<j;
    cout<<"\n\n";

    return 0;
}

Similarly, you can see the behavior of program by changing the data types of variable like see the output using this configuration int i,f; float j, & also int i,j & float f.
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
This program will display the numbers from 1 to 10 using for loop.

#include<iostream.h>

int main()
{
    int i;

    for(i=0;i<=10;i++)
    {
        cout<<i<<endl;
    }

    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
This program will display the numbers from 10 to 1 using for loop.

#include<iostream.h>

int main()
{
    int i;

    for(i=10;i>0;i--)
    {
        cout<<i<<endl;
    }

    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will take the number from the user and then display the sequence of number from 0 to the number entered by the user.

#include<iostream.h>

int main()
{
    int i,j;

    cout<<"Enter the Number: ";
    cin>>j;

    for(i=0;i<=j;i++)
    {
        cout<<i<<endl;
    }

    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
Extra Command: I am now introducing you with the command that you’ll not find in any of your text book, it is basically used in professional programming, but I think that this command is very helpful in understanding of these loops and other programs so it is better if you get familiar with this command.

You have to include the header files: #include<stdlib.h>
And command is: system(“pause”);

Now it is clear from the name of the command it is used for the pause purpose. So, you can pause your program at any time.
See the behavior of the following code with the use of this command.

#include<iostream.h>
#include<stdlib.h>   //you have to include this header file for pause command.

int main()
{
    int i;

    for(i=0;i<=10;i++)
    {
        cout<<i<<endl;
        system(“pause”);  // At this stage I want to pause my program
    }

    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<stdlib.h>   //you have to include this header file for pause command.

int main()
{
    int i=0;

    for(;i++<10;)
    {
        cout<<i<<endl;
        system("pause");  // At this stage I want to pause my program
    }

    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<stdlib.h>   //you have to include this header file for pause command.

int main()
{
    int i=0;

    for(;++i<10;)
    {
        cout<<i<<endl;
        system("pause");  // At this stage I want to pause my program
    }

    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will find the factorial of any Non-Negative Integer.
Formula of Factorial:
   Factorial of ‘n’ be: n!= n x (n-1) x (n-2) x (n-3) x …….. x (n-(n-1))
             = n x (n-1) x (n-2) x (n-3) x …….. x 1
   Factorial of ‘7’ be: 7!= 7 x (7-1) x (7-2) x (7-3) x (7-4) x (7-5) x (7-6)
             =  7 x 6 x 5 x 4 x 3 x 2 x 1
                                     = 5040

Program:

#include <iostream.h>

int main()
{
    int x,i,f;

    f=1;
   
    cout<<"Enter the Non-Negative No to find its Factorial: ";
    cin>>x;

    for(i=1;i<=x;i++)
    {
        f=f*i;
    }

    cout<<"The factorial of No: "<<x<<" = "<<f;
   
    cout<<"\n\n";

    return 0;
}
------------------------------------------------------------------------------------------------------------
Q1: Write a program that estimates the value of the mathematical constant e by using the formula:
e = 1 +   1  +  1  +  1  +  1  + ……..
                                                          1!     2!     3!     4!
Prompt the user for the desired accuracy of e (i.e. the number of terms in the summation).
------------------------------------------------------------------------------------------------------------
Q2: Write a program that computes the value of  e (i.e. e^x) by using the formula:
e = 1 +  x  +  x^2  +  x^3  + ……..
                                                            1!     2!         3!    
Prompt the user for the desired accuracy of e (i.e. the number of terms in the summation).
------------------------------------------------------------------------------------------------------------
Q3: Write a program which will take any number consisting of more than one digit from the user, and program tells that whether the number entered is palindrome or not.
------------------------------------------------------------------------------------------------------------
Previous Post Next Post