Computer Programming Lab 5

Lab 04: Loops

Exercise 1:
Write a C++ program to print the following pattern on the screen. Ask user to enter a integer value from 1 to 10. Use while loop.

Source Code:
#include <iostream>
using namespace std;
int main()
{
            int i,j,n;
            cout<<"enter a integer value from 1 to 10: ";
            cin>>n;
            i=0;
            while(n>=i)
            {
                        j=n;
                        while(j>=i)
                        {
                        j--;
                        cout<<"X";
}
  i++;
            cout<<endl;
            }
           
            return 0;
           
}

Exercise 2:
By applying for loop, write a C++ program that prints
a)    A sequence of numbers in ascending order from 1 to 100 (inclusive). 
b)    Modify your program in part (a) to make it prints odd numbers within 1 to 100. 
c)    Write a C++ Program that receives a positive integer (N) and prints a series of numbers from 1 to N (inclusive) in ascending order.
d)    Write a C++ Program that displays a series of alphabets in descending order from ‘Z’ to ‘A’.
e)    Modify your program in part (d) so that the program will display consonants only, no vowels.
f)     Write a C++ program that receives start value and end value. Then, your program will display a series of numbers from the start value to the end value inclusive.
Source Code:
#include <iostream>
using namespace std;
int main()
{
            int choice;
            cout<<"enter 1 for numbers in ascending order from 1 to 100 "<<endl;
            cout<<"enter 2 to  prints odd numbers within 1 to 100 "<<endl;
            cout<<"enter 3 to prints a series of numbers from 1 to N (inclusive) in ascending order "<<endl;
            cout<<"enter 4 to display a series of alphabets in descending order from 'Z' to 'A' "<<endl;
            cout<<"enter 5 to display consonants only, no vowels in Alphabets"<<endl;
            cout<<"enter 6 to display a series of numbers from the start value to the end value inclusive"<<endl;
            cout<<"Your choice  ";
            cin>>choice;
            if(choice==1)
            {
            for(int i=1;i<=100;i++)
            {
                        cout<<i<<" ";
                       
            }
}
                        if(choice==2)
                        {
                                    for(int i=1;i<=100;i++)
                                    {
                                                if(i%2!=0)
                                                cout<<i<<" ";
                                    }
                        }
                        if(choice==3)
                        {
                                    int n;
                                    cout<<"enter a number: ";
                                    cin>>n;
                                    for(int i=1;i<=n;i++)
                                    {
                                                cout<<i<<" ";
                                    }
                                   
                        }
            if(choice==4)
           
                        for(char i='Z';i>='A';i--)
            {
                        cout<<i<<" ";
            }
           
            if (choice==5)
            {
    cout<<"list of consonants"<<endl;
            for(char i='Z';i>='A';i--)
            {
            if(i=='A'||i=='E'||i=='I'||i=='O'||i=='U')
            {
            cout<<" * ";
            }
            else
            {
                        cout<<" "<<i;
            }
            }}
            if(choice==6)
            {
                        int S_value,E_value;
                        cout<<"enter start value: ";
                        cin>>S_value;
                        cout<<"enter end value: ";
                        cin>>E_value;
                        for(int i=S_value;S_value<=E_value;S_value++)
                        {
                                    cout<<S_value<<" ";
                        }
                       
                       
            }
            if(choice>=7)
                        {
                                    cout<<"Error";
                        }
                        return 0;
}

                   



Exercise3:
Write a C++ program that take two numbers from user, min and max. Use do-while to
Make sure that the user enters the smaller value first. Then your program will compute the sum of all integer numbers from min to max. Use do While loop

SourceCode:
#include <iostream>
using namespace std;
int main()
{
            int i=0,x,y;
            int sum=0;
            cout<<"enter min and max value: ";
            cin>>x>>y;
            if(x>y)
            cout<<"Try again";
            else{
                        do{
                       
                        sum=sum+x;
                        x++;
                        }
           
                        while(x<=y);
                        {
                                    cout<<"sum of min and max number is "<<sum<<endl;
                                    cout<<"Thank you! ";
                                   
                        }
            }
            return 0;
}

Exercise 4:
Write a C++ program that takes a positive integer from user and store it in variable posNumber. Follow these conditions;
If the number is less than 1, print wrong input.
If it is 1, print its value.
If value is greater than 1, check the value is it even or Odd.
If it is Even, half it and print.
If it is Odd, multiply it by 3 and print result.
Repeat the whole processes until user enter 1.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int n,posNumber;
            cout<<"Enter a positive integer: ";
            cin>>n;
            while(n!=1)
            {
           
                        posNumber=n;
                        if(posNumber>=0 && posNumber%2==0)
                        {
                                    posNumber= (posNumber/2);
                                    cout<<posNumber<<endl;
                        }

else if(posNumber>=0 && posNumber%2!=0)
{
            posNumber=posNumber*3;
            cout<<posNumber<<endl;
           
}
else
{
            cout<<"invalid output, ";
}
cout<<"Enter a positive integer: ";
            cin>>n;

           
}
            cout<<n;
}
Result:

Exercise 5:
#include<iostream>
using namespace std;
int main()
{

                                                    int n,a;
                                                    cout<<"enter length of real numbers:";
                                                    cin>>n;
                                                    cout<<"enter number :";
    cin>>a;
                                                    int min=a,max=a;
                                                    if(n<0)
                                                    cout<<"Sorry, you have entered an invalid input. ";
                                                    if(n==0)
                                                    cout<<"Opps, you don't have any number for me to process." <<"\n"<< "Thank you.";
                                                    if(n>0)
                                                    {
                                                            for(int j=1;j<=n;j++)
                                                            {
                                                                        cout<<"enter number "<<j<<":";
                                                                        cin>>a;
                                                                       
                                                                        if(n>max)
                                                                        {
                                                                        max=a;
                                                                        }
                                                            if(n<min)
                                                                        {
                                                                                    min=a;
                                                                                   
                                                                        }
                                                            }
                                                                        cout<<"The highest number is "<<max<<" "<<"The lowest number is "<<min;
                                                   
                                                            }
                                                            return 0;
                                                    }




Comments

Popular posts from this blog

Computer Programming lab 11

Computer Programming Lab 4