Tuesday, October 18, 2011

BCS-1A

GOOD LUCK BS(CS)-1A
For Your lab Sectional
Be Prepared


Regards: Ammar Afzal khan & M. Kashif Ali

Monday, October 17, 2011

How to print spaces using nested loop in C++

C++ Program:-



#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
for(int num1=1;num1<=10;num1++)
{
for(int num2=1;num2<=num1;num2++)
{
cout<<" ";
}
cout<<num1<<endl;
}

getch();
}

OUTPUT:-

 1
  2
   3
    4
     5
      6
       7
        8
         9
          10

NOTE:-
            In this program as you can see number of spaces in each line is increasing according to the number. lets discus about the logic of this program.
As you can see we use the first loop i.e  for(int num1=1;num1<=10;num1++) In this loop num1 is starting from 1 and ending at point 10 with the increment of 1. We use loop for following three purposes.
  1. For the printing the value of num1 i.e 1 ,2 ,3, 4.............. 10 . For this purpose we can simply use this statement Cout<<num1; so this line will simply print the number that will save to num1 each time. 
  2. For printing new lines 10 time.This can be done using this statement  Cout<<endl; so this line will always take the cursor to the new line with each value of loop.
  3. For printing the spaces that are increasing according to the number means 5 spaces for number 5 and similarly 10 spaces for number 10. This operation is done by using this nested loop.      for(int num2=1;num2<=num1;num2++)
    {
    cout<<" ";
    }                                       This loop is depending on the value of upper loop mean if the value of num1 is 5 then this loop will run 5 time because it is less than equal to num1.

Saturday, October 15, 2011

A C++ program that use nested loop to print lines 1 to 50

C++ Program :-



#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
int num1,num2,num3=1;
for(num1=1;num1<=5;num1++)
{
cout<<endl;
for(num2=num3;num2<num3+10;num2++)
{
cout<<"  "<<num2;
}


num3+=10;
}
getch();
}

OUTPUT:-

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15..... 20
21..... ....................30
31...........................40
41...........................50

A C++ program that use nested loop to print numbers

C++ Program :-



#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
int num1,num2;
for(num1=1;num1<=5;num1++)
{
for(num2=5;num2>=num1;num2--)
{
 cout<<num2;

}
cout<<endl;
}

getch();
}

OUTPUT :-

                The output of this program will be as following.....
54321
5432
543
54
5

Note:- 
        you can make any changes in the output according to your output requirements by simply changing the values in the loop.

Thursday, October 13, 2011

Assignment for BS(CS)-1 & BS(TN)-2

Assignment # 4
Note:- This is very important assignment follow the following instructions carefully.
  • In Copy/ paste case both students will be assigned zero mark in assignment as well as in lab-sectional. 
  • Accurate and neat assignment will be considered in lab-sectional marking.
  • The assignment will be checked on your next lab during lab-sectional.
  • After lab, no assignment will be acceptable.
  • All these programs should must be made in Turbo-c don't make it in Ms-word.
  • Questions may be asked about your assignment in lab- Viva about your assignment so make sure you know what you make.



Q#1. Make a program that can print the following output with the help of Nested for Loop.


*
***
*****
*******
*****
***
*


Q#2.Make a program that uses loops to convert temperature from centigrade to Fahrenheit from 1 to 20 and the output should be in the following way.

Fahrenheit              Centigrade
1                      .........
2                      .........

Q#3.Use the Nested loop concept to print the following output.


**
*  *
**    **
***      ***
****        ****
*****          *****
******            ******
*****          *****
****        ****
***      ***
**    **
*  *
**

Q#4.Make a program using nested loops that print the following the output.

10
99
888
7777
66666
555555
4444444
33333333
222222222
1111111111

Q#5. Make a program using the concept of nested loop that will print the following output.

1 1
2  2
3   3
4    4
5     5
6      6
7       7
8        8
9         9


Best of Luck!

The Concept of nested loop in C++

Nested Loop :-
                        A nested loop means a loop inside a loop doesn't matter which type of loop you are using but if you use a loop inside a loop then that loop will be called nested loop.
For Example..

#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
for(int num=1;num<=10;num++)
             {
              for(int num2=1;num2<=10;num2++)
                            {


                             cout<<"\nALLAH";
                             }

            }
 getch();
}


NOTE:-
              This program will print "Allah"   (10 X 10) 100 times because as you can see for each value of first loop the next loop will execute ten times so in this way "Allah" will be printed 100 times.