8. #includeiostream.h
void main ()
{
int flag=0,n;
cinn;
for(int i=2;inflag==0;i++)
if (n%i==0)
flag=1;
if(flag==1)
coutprimeendl;
else
cout not primeendl;
}
9. #includeiostream.h
void main ()
{
int flag=0,n;
cinn;
for(int i=0;in;i++)
{
if (n%i==0)
flag=1;
}
if(flag==0)
coutprimeendl;
else
cout not primeendl;
}
10. #include iostream.h
int sum(int);
void main ()
{
int x ;
cout sumion is :- sum (x) ;
}
int sum (int a)
{
int su =0 ;
for(int i=1 ; i=50 ;i++)
su+=i ;
return su ;
}
#includeiostream.h
void main ()
{
int sum=0;
for(int i=0; i=50; i++)
{
sum+=i;
}
cout the sum of number from 0
to 50 is endl;
coutsumendl;
}
11. #include iostream.h
void main ()
{
int x ;
coutEnter one number 't';
cinx ;
for (int i=1 ;i=x ;i++)
{
for (int j=1 ;j=x ;j++)
couti*j = i*j
endl ;
cout endl ;
}
coutendl ;
cout-------------- ;
}
ن*ن ت جيكه دي ضربي جدول
#include iostream.h
int po (int ,int) ;
int main ()
{
int x ,y ;
coutEnter x ,y ;
cinxy ;
coutnx to po y=po(x,y)endl ;
return 0 ;
}
int po (int a , int b )
{
int p= 1 ;
for (int i=1 ; i=b ;i++)
p=p*a ;
return p ;
}
Enter x,y 5 6
X to po y =15625
12. #include iostream.h
void main ( )
}
int x;
cin x;
cout x endl;
cout x *2endl;
cout x *3endl;
cout x *4endl;
cout x *5endl;
cout x *6endl;
cout x *7endl;
cout x *8endl;
cout x *9endl;
{
13. #include iostream.h
void main ()
{
int x ;
coutEnter number x endl ;
cinx ;
if ( x%2 == 0)
coutNumber is even:- x ;
else if ( x%2 != 0)
coutNumber is odd :- x ;
}
14. #include iostream.h
void main ( )
{
// an integer number
int Num,i;
// numebr of numbers
int Num_Num;
// total of numbers
int Sum = 0;
cout Enter the number of numbers : ;
// get the number of numbers from user
cin Num_Num;
// loop from 1 to Num_Num.
for (int Counter = 1; Counter = Num_Num ; i++ )
{
cout Enter number ( Counter ) :;
// get the number from user
cin Num;
// put the number in sum
Sum += Num;
} // end for
// print the result
cout The Result is : Sum ;
} // end main
15. #include iostream.h
int Pluse ( int, int );
int Minus ( int, int );
int Div ( int, int );
int Mult ( int, int );
void main ( )
{
int Fir_Num;
int Sec_Num;
char Ope;
cout Enter the first number : ;
cin Fir_Num;
cout Enter the second number : ;
cin Sec_Num;
cout Enter ur operation : ;
cin Ope;
switch (Ope)
{
case ' + ' :
cout The result is : ;
break;
case ' - ' :
cout The result is :
Minus ( Fir_Num, Sec_Num );
break;
case '*' :
cout The result is :
Mult ( Fir_Num, Sec_Num );
case ' / ' :
cout The result is :
Div ( Fir_Num , Sec_Num );
break ;
default :
cout Sorry ur operation is wrong ;
}
}
int Pluse ( int x, int y )
{
return x + y ;
}
int Minus ( int x, int y )
{
return x - y;
}
int Div ( int x, int y )
{
if ( y == 0 )
{
cout Error division by zero ;
return 0;
}
else
return x / y;
}
int Mult ( int x, int y )
{
return x * y ;
}
16. #include iostream.h
void main( )
{
int Fir_Num;
int Sec_Num;
cout Enter the first number : ;
cin Fir_Num ;
cout Enter the second number :
;
cin Sec_Num;
cout The Result is :
Fir_Num + Sec_Num ;
}
17. #include iostream.h
void main ( )
{
int Num_Of_Sub;
int Grade;
int Total = 0;
int Average;
cout Plz enter number of subjects : ;
cin Num_Of_Sub;
for (int Counter = 1; Counter = Num_Of_Sub; Counter++)
{
cout Enter grade number ( Counter ) : ;
cin Grade;
if (Grade = 50 Grade = 100)
Total += Grade;
else
{
cout Sorry ...grade is out of range !!!!! try againn;
Counter--;
}
}
Average = Total / Num_Of_Sub ;
cout The average is : Average ;
}
18. #includeiostream.h
void main()
{
long d1 , d2;
char let;
do{
coutenter the first numbern;
cind1;
coutenter the second numbern;
cind2;
cout the result of multiple two
numbers is :n;
coutd1*d2;
coutn if you want to do again press
any letter or number unless a n;
cinlet;
coutn;
}while(let!='a');
}
B
y
t
h
is
y
ou
c
a
n
(n
u
m
*n
u
m
)
a
n
d
y
ou
a
g
a
in
a
n
y
t
im
e
in
t
e
r
(c
h
,n
u
m
)
19. #includeiostream.h
void main ()
{
int k;
cout enter your number : n ;
cin k;
if ( k50)
cout the number is less than 50
;
else
coutava darkatendl;
}
If and else
20. #include iostream.h
void main()
{
int z,y;
cout Inter your longendl;
cin z ;
cout Inter friend's longendl;
cin y;
cout --------------------endl;
if(zy)
{
cout You longerendl;
cout Your long is zendl;
coutendl;
cout The average longs are ;
cout (z+y)/2;
}
else
{
cout Your friend is longerendl;
cout He's long is yendl;
cout y*2 = y*2endl;
cout y/2 = y/2;
}
}
23. #include iostream.h
void main()
{
int x;
cin x;
switch (x)
{
case 1:
cout welcomeendl;
break;
case 2:
cout hello studentendl;
break;
case 3:
cout introdaction to c++endl;
break;
default:
cout bye bye;
}
}
24. cout its a Saturnendl;
break;
case 7:
cout its a URANUSendl;
break;
case 8:
cout its a NEPTUNEendl;
break;
case 9:
cout its a PLUTOendl;
break;
case 10:
cout its a senda endl;
break;
default:
cout err no plant to resemble this
numberendl;
break;
}
}
#include iostream.h
void main()
{
int x;
cout inter number a planet endl;
cin x;
switch (x)
{
case 1:
cout its a Mercuryendl;
break;
case 2:
cout its a Venusendl;
break;
case 3:
cout its a earthendl;
break;
case 4:
cout its a Marsendl;
break;
case 5:
cout its a Jupiterendl;
break;
case 6:
About switch
25. #includeiostream.h
main()
{
int x;
coutenter the decimal number:;
cin x;
while(x=12)
{switch(x)
{
case 1:
{coutIendl;
coutenter the decimal number:;}
break;
case 2:
{coutIIendl;
coutenter the decimal number:;}
break;
case 3:
{coutIIIendl;
coutenter the decimal number:;}
break;
case 4:
{coutIVendl;
cout enter the decimal number:;}
break;
case 5:
{coutVendl;
coutenter the decimal number:;}
break;
case 6:
{coutVIendl;
coutenter the decimal number:;}
case 8:
{coutVIIIendl;
coutenter the decimal number;}
break;
case 9:
{coutIXendl;
coutenter the decimal number;}
break;
case 10:
{coutXIendl;
coutenter the decimal number;}
break;
case 11:
{coutXIIendl;
coutenter the decimal number;}
break;
case 12:
{coutXIIIendl;
coutenter the decimal number:;}
break;
default:
coutNOOOO;
break;
}
cinx;
}
}
break;
case 7:
{coutVIIendl;
coutenter the decimal number:;}
break;
27. #include iostream.h
void main ()
{
for (char x=65;x=90;x++)
cout x;
}
a……..z
#include iostream.h
void main ()
{
for (int x='A' ;x='Z' ;x++)
cout x;
}
65……..90
#include iostream.h
void main ()
{
float L, M;
char ch;
do
{
cout nEnter L: ;
cin L;
cout Enter M: ;
cin M;
cout nL+M= L+M;
cout nL-M= L-M;
cout nL*M= L*M;
if (M != 0)
cout nL/M= L/M;
cout n Do Again (y/n): ;
cin ch;
}
while (ch != 'n');
}
Other more programming
W
hile for again
28. #include iostream.h
void main ( )
{
for (int i = 0; i 15; i++)
cout i * i ;
}
#include iostream.h
void main ()
{
int x = 10;
cout x = x endl;
cout n ++x = ++x endl;
cout x++ = x++ endl;
cout n x = x endl ;
cout n --x = --x endl;
cout x-- = x-- endl;
cout n x = x endl ;
}
For information
29. #include iostream.h
#include math.h
void main ()
{
int x, y, P ;
cout Enter a number: ;
cin x ;
cout Enter a power : ;
cin y ;
P = pow (x,y) ;
cout The result is : P
endl ;
}
#include iostream.h
#include math.h
void main ( )
{
float num, ans;
cout Enter a number: ;
cin num;
ans = sqrt(num) ;
cout square root is: ans endl;
}
Calculus example
30. #include iostream.h
int sqr (int n)
{
int r;
r=n*n;
return r;
}
void main()
{
int k;
cink;
coutsqr(k)endl;
}
Function
#include iostream.h
int sqr (int);
void main()
{
int k;
cink;
coutsqr(k)endl;
}
int sqr (int n)
{
int r;
r=n*n;
return r;
}