Tuesday, November 29, 2011

A C++ program to find out the Prime Factors


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int fact[20],n,num,c=0,j,k;
cout<<"enter the number: ";
cin>>num;
for(n=1;n<=num/2;n++)
{
if(num%n==0)
{
fact[c]=n;
c++;
}
}
cout<<"The Prime Factores Are As Follow"<<endl;
for(n=0;n<c;n++)
{
k=0;
for(j=1;j<=fact[n];j++)
{
if(fact[n]%j==0)
k++;
}
if(k==2)
cout<<fact[n]<<endl;
}
getch();
}

Note:-
            This program is only for few peoples so don't......................... :-)

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.