Example.1
#include <iostream.h>
void main(void)
{
int numb;
cout <<”enter a number: ”;
cin>>numb;
cout<<”numb<10 is “<<(numb<10)<<endl;
cout<<”numb>10 is “<<(numb>10)<<endl;
cout<<”numb==10 is “<<(numb==10)<<endl;
}
Output
Enter number: 20 //20
is the assume number in the example
numb<10 is 0
numb<10 is 1
numb<10 is 0
E
xample.2
#include <iostream.h>
void main(void)
{
int j ;
for(j=0; j<15; j++)
cout <<j*j <<endl;
}
Output
0 1 4
9 16 25
36 49 64
81 100 121
144 169 196
Example.3
#include <iostream.h>
#include <iomanip.h>
void main(void)
{
int j;
for (j==0;j<=10;j++)
cout << setw(4)<< j;
int cube =j*j*j;
cout <<setw(6)<<cube<<endl;
}
Output
1
1
2
8
3
27
4
64
5
125
6
216
7
343
8
512
9
729
10
1000
Example.4
#include <iostream.h>
void main(void)
{
unsigned int
numb;
unsigned long
fact=1;
cout <<”Enter a number:” ;
cin>>numb;
for(int j=numb; j>0; j--)
fact =fact*j;
cout<<”factorial is ”<<fact<<endl;
}
Output
Enter a number 10 //10 is the
assume number in this example
Factorial is 3628800
Example.5
#include <iostream.h>
void main(void)
{
int n=99;
while(n !=0)
cin>>n;
cout <<endl;
}
Output
1
27
33
144
9
0
Example.6
#include <iostream.h>
void main(void)
{
int pow=1;
int numb=1;
while(pow<1000)
{
cout <<setw(2)<<numb;
cout<<setw(5)<<pow<<endl;
++numb;
power=numb*numb*numb*numb;
}
cout<<endl;
}
Output
1 1
2 16
3 81
4 256
5 625
6 1296
7 2401
8 4096
9 6561
Example.7
#include <iostream.h>
void main(void)
{
const
unsigned long limit=4294967295;
unsigned long
next=6;
unsigned
long last=1;
while(next <limit/2)
}
cout <<last<< “ “;
long sum=next+last;
next =last;
last= sum;
}
cout<<endl;
}
Output
See the book
Example.8
#include <iostream.h>
void main(void)
{
long dividend, divisor;
char ch;
do
{
cout <<”Enter dividend: “;cin>>dividend;
cout <<”Enter divisor: “;cin>>divisor;
cout <<”Quotient is“ <<dividend/divisor;
cout <<”.remainder is “ << dividend % divisor;
cout
<<”\n Do an other? (y/n): “;
cin >> ch;
}
while (ch != ‘n’);
}
Output
See the book
Example.9
#include <iostream.h>
void main(void)
{
int x;
cout<<”Enter a
numer:”;
cin>>x;
if(x>100)
cout<<”that number is greater than 100\n”;
}
Output
Enter a number:2000
That number is greater than 100
Example.10
#include
<iostream.h>
void main(void)
{
int x;
unsigned long
n,j;
cout
<<”Enter a number:”;
cin
>>n;
for (j=2;j<=n/2;j++)
if (n%j==0)
{
cout <<”It is not prime; divisible by
“<<j<<endl;
exit(0);
}
cout <<”It is prime\n”;
}
Output
See
the book
Example.11
#include <iostream.h>
void main(void)
{
int x;
cout<<”Enter a
numer:”;
cin>>x;
if(x>100)
cout<<”that number is greater than 100\n”;
else
cout<<”that number is greater than 100\n”;
}
Output
See the book
Example.12
#include <iostream.h>
#include <conio.h>
void main(void)
{
int chcount=0;
int wdcount=1;
char ch= ‘a’;
cout <<”Enter a phrase :“;
while(ch!=’\r’)
{
ch= getche();
if (ch==’ ‘)
wdcount++;
}
cout”\nwords=”
<<wdcount<<endl;
<<”Letters=’’<<(chcount-1)<<endl;
}
Output
See the book
Example.13
#include <iostream.h>
#include <conio.h>
void main(void)
{
char dir=’a’;
int x=10,y=10;
cout<<”Type enter to quite\n”;
while(dir!= ‘\r’)
{
cout<<”\nyour location is “<<x<<”,”<<y;
cout<<”\nyour location is “<<x<<”,”<<y;
cout <<”\npress directiion key (n,s,e,w):”;
dir=getche();
if(dir==’n’)
y--;
else
if(dir==’s’)
y++;
else
if(dir==’e’)
x++;
else
if(dir==”w’)
x--;
}
}
Output
See the book
Example.14
#include <iostream.h>
void main(void)
{
int a,b,c;
cout<<”Enter three numbers,a,b,c:\n;
cin>>a>>b>>c;
if(a==b)
if (b==c)
cout<<”a,b,c are the same \n”;
else
cout<<”a ,b are different \n”;
}
Output
See
the book
Example.15
#include <iostream.h>
#include <conio.h>
void main(void)
{
int speed ;
cout<<”\nEnter33,45,or 78:”;
cin>>speed ;
switch(speed)
{
case 33;
cout<<:LP
album\n;
break;
case 45;
cout<<”single selection\n”;
break;
case
78;
cout <<”obsolete format\n”;
break;
}
}
Example.16
#include <iostream.h>
#include <conio.h>
void main(void)
{
char dir=’a’;
int x=10,y= 10;
while (dir!=’\r’)
cout <<”\nyour location is “
<<x<<”,”<<y;
cout <<” \ nEnter direction (n,s,e,w):”;
dir=getche();
switch (dir)
{
case ‘n’: y--;break;
case ‘s’:y++;break;
case ‘e’:x++;break;
case’w’ :x--:break;
case’\r’:cout <<”Exiting \n”;break;
default:
cout
<<”try again\n”;
}
}
}
Example.17
#include <iostream.h>
#include <process.h>
#include <conio.h>
void main(void)
{
char dir=’a’;
intx=10,y=10;
while (dir!=’\r’)
cout <<”\nyour location is “
<<x<<”,”<<y;
cout <<” \ nEnter direction (n,s,e,w):”;
dir=getche();
switch (dir)
{
case ‘n’: y--;break;
case ‘s’:y++;break;
case ‘e’:x++;break;
case’w’ :x--:break;
}
if(x==7&&y==11)
{
cout <<”\nyou found the treasure!\n;
exit(0);
}
}
}
Example.18
#include <iostream.h>
#include <process.h>
#include <conio.h>
void main(void)
{
char dir=’a’;
int x=10,y=10;
while (dir!=’\r’)
{
cout <<”\nyour location is “
<<x<<”,”<<y;
if(x<5llx>15)
cout <<”\nBeware: dragons lurk
here”;
cout <<” \ nEnter direction (n,s,e,w): ”;
dir=getche();
switch (dir)
{
case ‘n’: y--;break;
case ‘s’:y++;break;
case ‘e’:x++;break;
case’w’ :x--:break;
}
}
}
Example.19
#include <iostream.h>
#include <conio.h>
void main(void)
{
const unsigned char WHITE=219;
const unsigned char GRAY;
unsigned char ch;
for(int count =0;count<80*25-1; count++)
{
fh=WHITE;
For (int
j=2; j<count; j++)
if(count%j==0)
{
ch=GRAY;
break;
}
cout <<ch;
}
getch();
}
Example.20
#include <iostream.h>
void main(void)
{
long dividend ,divisor;
char ch;
do
{
cout<<”Enter dividend:”;cin>> dividend;
cout <<”Enter divisor: ”; cin >>divisor;
if (divisor==0)
{
cout<<”111egal divisor\n”;
continue ;
}
cout<<”quotient is “<<dividend/divisor;
cout <<”remainder is “ << dividend
%divisor;
cout <<”n\doanother?(y/n): ”;
cin>>ch;
}
while (ch!=’n’);
}
No comments:
Post a Comment